Forum Moderators: phranque
# ----------------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
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]
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
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
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]
Jim