Question

I have this in my htaccess file for my live site:

Options +MultiViews FollowSymLinks
RewriteEngine On
RewriteBase /

#301 redirect for all pages to use www
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

However when Im testing on my local server I have to manually change it to this:

#Options +MultiViews FollowSymLinks
RewriteEngine On
RewriteBase /

#301 redirect for all pages to use www
#RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Is there an automated way via htaccess that I can turn things on and off based on current server ip? My test server is 127.0.0.1

Was it helpful?

Solution

Well you can use Rewrite Conditions based on %{REMOTE_ADDR}:

#301 redirect for all pages to use www
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This will redirect only if IP is not equal to 127.0.0.1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top