Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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?

like image 997
pepe Avatar asked Dec 31 '11 20:12

pepe


People also ask

What is rewrite rule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.


1 Answers

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]
like image 182
Jordon Bedwell Avatar answered Sep 29 '22 20:09

Jordon Bedwell