Forum Moderators: phranque
#NON-WWW to WWW
RewriteCond %{HTTP_HOST} ^somedomain.com
RewriteRule (.*) [somedomain.com...] [R=301,L]
#REDIRECT INDEXes to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ [somedomain.com...] [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ [somedomain.com...] [R=301,L]
##########
#ADD TRAILING SLASH WHEN ITS NOT THERE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}¦/)$
RewriteRule (.*)$ [somedomain.com...] [R=301,L]
What I want to do is avoid having to change each "somedomain.com" in every .htaccess file that I have. There are many sites that have the exact same rules so it's just a pain in the butt to create one for each domain.
I have thought about coding a php script to retrieve the current domain name from the url but am not sure how to implement this script in php.
I am playing with a line in htaccess similar to this :
RewriteRule (.*) /php/get_domain_name.php
I can get the script to execute but am not sure how to "parse" the retrieved domain name into a line such as this :
RewriteRule ^(.*)index\.php$ [somedomain.com...] [R=301,L]
Any help or critique on my htaccess file would be greatly appreciated.
RewriteCond %{HTTP_HOST} [b]!^w[/b]ww\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Jim
[edited by: jdMorgan at 12:52 pm (utc) on June 23, 2009]
.
The index filename redirects can be optimised by changing the (.*) pattern to something like:
# Redirect Index requests to strip filename and force www (more efficient code to capture the folder path)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(s?html?Šphp[45]?)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(s?html?Šphp[45]?)$ http://www.example.com/$1 [R=301,L] .
Your non-www to www redirect does not fix www requests with appended period or appended port number. Try this:
# Redirect host name not exactly "www.example.com" to www.example.com (also fixes requests with appended port numbers, etc)
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L] .
I didn't see Jim's reply. Go with his code, but do be aware of the issues I raised here. They do catch a lot of people out.