Forum Moderators: phranque

Message Too Old, No Replies

Redirect specific path back to root

         

itaine

1:37 am on Jan 11, 2009 (gmt 0)

10+ Year Member



This is the situation:

# ----------------INTIALIZE------------------ #
AddDefaultCharset utf-8
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#ErrorDocument 404 /self-service

# Block the viewing or downloading of template files directly #
<Files "*.tpl">
Order Allow,Deny
Deny from All
</Files>

# -------------CANONICAL ROOT---------------- #
# Ensure that www. is prepended to domain.com/ #
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ [%1...] [R=301,L]

# Redirect domain.com/index.php to domain.com/ #
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ [%{SERVER_NAME}...] [R=301,L]

# Prevent secure pages from being indexed by bots #
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots.txt$ robots-ssl.txt

#For Firefox, the MIME type for XSL has to be configured on the server (in your .htaccess file or httpd.conf):
AddType application/xml .xsl

# ------------------------------------------------------------- #
# Folders to let through (ignore) #
RewriteCond %{REQUEST_URI} "!/global/"
RewriteCond %{REQUEST_URI} "!/self-service/"
RewriteCond %{REQUEST_URI} "!/sitemaps/"
RewriteCond %{REQUEST_URI} "!/spotlight/"
# push http request to domain.com/cms/ subdirectory #
RewriteRule ^(.*) cms/$1 [L]

I have all url request pushing/forwarding to the cms folder (with noted exceptions) and all is well.
www.domainname.com -> points to contents in www.domainname.com/cms/ (but only shows www.domainname.com)

Now the issue is sometimes certain actions during normal use of the cms adds or prepends "cms" in the path. This can probably be avoided through careful config of the cms. However my concern now is possible duplicate content. How can I have it redirect to www.domainname.com/ when/if somehow .../cms/ gets added to the url path?

Thx in advance
Taine

jdMorgan

3:37 am on Jan 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... added where in the URL path? Examples are useful...

P.S. Your first two rules need reversed, and the index.php -> / rule then needs similar logic to that of your example.com->www.example.com rule. As it is now, a request for example.com/index.php will result in two chained redirects, first to example.com/ and then to www.example.com/

New first rule:


# Redirect example.com/index.php or www.example.com/index.php to www.example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[a-z]{2,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^index\.php$ http://www.%2/ [R=301,L]

The new "extension" on the hostname pattern allows for a trailing period (FQDN), trailing port number, or both -- all of which are perfectly-valid in an HTTP "Host:" header. This should be done for any hostname pattern that is end-anchored.

A good rule of thumb is: External redirects first, in order from most-specific to least specific, followed by internal rewrites, again in order from most- to least-specific.

"More specific" means "affecting fewer URLs" or "having a more-complex regex pattern" (or more-complex or additional patterns in RewriteConds).

Jim

itaine

5:04 am on Jan 11, 2009 (gmt 0)

10+ Year Member



Thx Jim! appreciate it.

I made the mod and now I have:

# ----------------INTIALIZE------------------ #
AddDefaultCharset utf-8
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#ErrorDocument 404 /self-service

# Block the viewing or downloading of template files directly #
<Files "*.tpl">
Order Allow,Deny
Deny from All
</Files>

# -------------CANONICAL ROOT---------------- #

# Redirect example.com/index.php or www.example.com/index.php to www.example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[a-z]{2,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^index\.php$ [%2...] [R=301,L]

# Ensure that www. is prepended to example.com/ #
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ [%1...] [R=301,L]

# Redirect example.com/index.php to example.com/ (NOTE: replaced by first rule - 2b deleted)#
#RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
#RewriteRule ^index\.php$ [%{SERVER_NAME}...] [R=301,L]

# Prevent secure pages from being indexed by bots #
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots.txt$ robots-ssl.txt

#For Firefox, the MIME type for XSL has to be configured on the server (in your .htaccess file or httpd.conf):
AddType application/xml .xsl

# ------------------------------------------------------------- #
# Folders to let through (ignore) - [drupal.org...] #
RewriteCond %{REQUEST_URI} "!/global/"
RewriteCond %{REQUEST_URI} "!/self-service/"
RewriteCond %{REQUEST_URI} "!/sitemaps/"
RewriteCond %{REQUEST_URI} "!/spotlight/"
# push http request to domain.com/cms/ subdirectory #
RewriteRule ^(.*) cms/$1 [L]

# Redirect of example.com/ to alternate landing page #
#RewriteRule ^$ [%{SERVER_NAME}...] [R=302,L]
--------------------------------------------------------

what about redirecting /cms/ to /

so that www.example.com/cms/yada.php redirects to www.example.com/yada.php?

Thx in advance
Taine

itaine

9:01 am on Jan 11, 2009 (gmt 0)

10+ Year Member



I would like to add for clarification that both www.example.com/cms/yada.php and www.example.com/yada.php work and point to the same content. I want to redirect it to www.example.com/yada.php

Thx
Taine

jdMorgan

5:38 am on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is almost exactly the same problem as the /index.php problem. I hope you'll apply what you've got to what you need and do a bit of experimenting...

You apparently missed my note about adding the FQDN and port matching to all end-anchored hostnames.

For clarity, here's the whole "block" of mod_rewrite redirects.


# Externally redirect direct client requests for example.com/cms/<anything>
# or www.example.com/cms/<anything> to www.example.com/<anything>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /cms/[^\ ]*\ HTTP/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[a-z]{2,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^cms/(.*)$ http:[b][b]//www.%2/$1 [R=301,L]
#
# Externally redirect direct client requests for example.com/index.php
# or www.example.com/index.php to www.example.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[a-z]{2,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^index\.php$ http:[b][b]//www.%2/ [R=301,L]
#
# Externally redirect all requests for non-canonical, non-www hostnames
# to the corresponding canonical www- hostname
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})\.?(:[0-9]+)?$ [NC]
RewriteRule ^(.*)$ http:[b][b]//www.%1/$1 [R=301,L]
#
# Internally rewrite HTTPS/SSL requests for robots.txt to the alternate robots-ssl.txt
# file which requests robots.txt-compliant crawlers not to index the pages via HTTPS
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^robots.txt$ robots-ssl.txt
#
# Internally rewrite all requests (except for excluded subdirectories) to the
# /cms subdirectory (Exclude /cms itself to prevent internal recursion)
RewriteCond $1 !^(cms¦global¦self-service¦sitemaps¦spotlight)/
RewriteRule (.*) cms/$1 [L]

Replace the broken pipe "¦" characters in the last RewriteCond with solid pipe characters before use; Posting on this forum modifies the pipe characters. Note that /cms itself is now excluded -- I am not sure how this last rule worked before, as it should have caused an internal redirection loop...

Jim