pepe
pepe

Reputation: 9909

.htaccess mod_rewrite IP-based redirect: how to redirect all traffic to a specific subdirectory, except my IP?

I want my visitors to have access only to a specific part of my website (blog). If they try to access other areas of the website, I'd like them redirected to the blog section.

I also want this to apply to everyone except to my IP address.

So the structure is as follows:

mysite.com/blog // visitor access allowed

mysite.com        // redirect to mysite.com/blog

mysite.com/forum  // redirect to mysite.com/blog

mysite.com/tools  // redirect to mysite.com/blog

etc...

Do you have a suggestion on how to do this via .htaccess mod_rewrite?

Upvotes: 9

Views: 16898

Answers (3)

Brennan James
Brennan James

Reputation: 442

Just add your IP Address in..

order deny,allow
deny from all
allow from xxx.xx.xxx

Upvotes: 0

Jordon Bedwell
Jordon Bedwell

Reputation: 3247

You should be able to redirect with the following:

RewriteCond %{REMOTE_HOST} !^123\.456\.789
RewriteCond %{REQUEST_URI} !^/blog/?
RewriteCond %{REQUEST_URI} /(.*)$
RewriteRule (.*) /blog [R=301,L]

Upvotes: 18

Ben D
Ben D

Reputation: 14479

You should create a page that handles the specific redirects, and then edit your htaccess file to be something like this:

Options +FollowSymlinks
RewriteEngine on
#not your IP
RewriteCond %{REMOTE_HOST} !^123\.123\.123\.123
#make sure the rule allows everyone to access the redirect page
RewriteCond %{REQUEST_URI} !/redirect_page\.html$
#Send them to the redirect page
RewriteRule \.html$ /redirect_page.html [R=302,L]

The rediret_page.html can have either js or server-side redirection that handles where they end up, but it will force everyone who has not come on your IP address through a specific page that handles redirection.

Upvotes: 3

Related Questions