Forum Moderators: phranque

Message Too Old, No Replies

multiple htaccess files

want to maintain only one

         

bawhitney

4:12 am on Jun 23, 2009 (gmt 0)

10+ Year Member



Here is my .htaccess file

#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.

jdMorgan

12:13 pm on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look into using %{HTTP_HOST} to get the currently-requested hostname, or %{SERVER_NAME} to get the canonical domain name as defined in the server config file. That is, you can build the redirect URL using a variable, as in:

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]

g1smd

1:51 am on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to place the index redirects before the non-www redirects, otherwise you end up with an unwanted Redirection Chain for any non-www index request.

.

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.