Welcome to WebmasterWorld Guest from 107.20.54.98

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)

Junior Member

10+ Year Member

joined:May 1, 2004
posts: 100
votes: 0


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 July 23, 2010 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


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 July 26, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 1, 2004
posts:100
votes: 0


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 July 26, 2010 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


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