Welcome to WebmasterWorld Guest from 54.197.116.116

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite conditional on server

only rewrite rules if specific server

   
3:40 pm on Jul 21, 2010 (gmt 0)

10+ Year Member



I develop sites on a Windows machine running LAMP under WampServer. The final sites run on a Linux server. Obviously I want my development environment to match the live environment as closely as possible.

One of the problems I have is that I have been unable to use the same .htaccess file because of the following rule:

### re-direct non-www to www
RewriteCond %{http_host} !^www.mydomain.com$ [nc]
RewriteRule ^(.*)$ [mydomain.com...] [r=301,nc,L]
####

Not being able to use the same .htaccess file is causing me severe problems because of other issues. Is there ANY way to make the above rule conditional on the server type?

The URL of the development site is of format [localhost...] I basically don't want to run the above rewrite if the URL includes /localhost/ (or if the server type is Windows, I guess).

Any help would be much appreciated.
7:00 am on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



How about:

# externally redirect non-blank non-canonical hostname to canonical hostname
# (also exclude "localhost" and entire non-routable loopback IP address range)
RewriteCond %{HTTP_HOST} !^(www\.example\.com|localhost|127(\.[0-9]{1,3}){3})?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Jim
11:18 am on Jul 26, 2010 (gmt 0)

10+ Year Member



Thanks Jim, but sadly doesn't seem to work. It is correctly excluding the rewrite when the site is accessed from the Windows development site, but it not rewriting mydomain.com to www.mydomain.com.

Any thoughts please?
1:53 pm on Jul 26, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I'm not sure how you tested, so I can't really interpret your results...

But make sure that the "www\.example\.com" pattern in the RewriteCond and the "www.example.com" in the RewriteRule substitution URL match exactly, except for the escaping required in the RewriteCond pattern, and be sure to always delete your browser cache before testing any new server-side code.

Jim