John
John

Reputation: 10146

Can I load htaccess settings based on ip?

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

Upvotes: 0

Views: 46

Answers (1)

Rahil Wazir
Rahil Wazir

Reputation: 10132

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

Upvotes: 1

Related Questions