4

I've installed another theme on my site and I need to improve it. How can I change the index file only for my IP address?

My current htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I need to change the index file to index.WP.php for the IP address 203.0.113.111. Is it possible?

kasperd
  • 30,455
  • 17
  • 76
  • 124
Rasvet
  • 53
  • 1
  • 5

2 Answers2

4

You could just repeat the front controller part and include a condition for your IP address. For example:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

### ALTERNATIVE FRONT CONTROLLER FOR SPECIFIC IP ADDRESS
RewriteRule ^index\.WP\.php$ - [L]

# Special case for document root to override DirectoryIndex
RewriteCond %{REMOTE_ADDR} =203.0.113.111
RewriteRule ^$ /index.WP.php [L]

# Route all other requests for specific IP address
RewriteCond %{REMOTE_ADDR} =203.0.113.111
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.WP.php [L]

### DEFAULT FRONT CONTROLLER FOR ALL OTHER USERS
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This will only route the user with IP address 203.0.113.111 to index.WP.php. All other visitors get passed through to the default front controller. This still allows any user to directly request index.WP.php if they want to (if they know it exists).

UPDATE: Note that I've changed the RewriteRule pattern on the first front controller rule block from . to ^ - this allows it to match the document root, ie. http://www.example.com/, so it can override the default DirectoryIndex.

UPDATE: For the "alternative" front controller (via the specific IP address), a special case is required for the document root in order to override the DirectoryIndex (ie. index.php). (The previous update wouldn't have been enough since the document root is obviously a directory, so the preceding condition (!-d) still fails.)

RewriteRule (.*) http;//www.example.com/$1 [R=301,L]

Note that you have an erroneous semicolon (;) after the scheme on your canonical non-www to www redirect.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • Yes it is working correctly, but not work only for main page. How can I change main page of my site too. – Rasvet Jan 01 '17 at 10:30
  • Ah yes, you'll need to change the `RewriteRule` _pattern_ so that it matches _anything_, rather than _something_. I've updated my answer. – MrWhite Jan 01 '17 at 10:47
  • Actually, the previous update was not enough. The directive would have still failed to rewrite the document root since the preceding condition that ensures it's not a directory would have still failed. I think a special case is required for the document root in order to override the `DirectoryIndex`. I've updated my answer. – MrWhite Jan 01 '17 at 16:32
0

You can put below code in your index.php file. And change 203.0.113.111 to your public / static IP and change Location URL. It will resolve your concern

if($_SERVER['REMOTE_ADDR']=='203.0.113.111' ||  $_SERVER['HTTP_X_FORWARDED_FOR']=='203.0.113.111')
{
    header("Location: http://www.example.com/yourindex.php");
} else {
    header("Location: http://www.example.com/index.php");
}
kasperd
  • 30,455
  • 17
  • 76
  • 124
Rakesh C
  • 24
  • 3
  • No need to redirect to `index.php`, since you are already there. However, you can (and should) avoid an external redirect here. – MrWhite Dec 31 '16 at 11:37