.htaccess mod_rewrite基于IP的重定向:如何将所有流量重定向到特定的子目录,除了我的IP?

时间:2022-11-25 11:18:29

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.

我也希望这适用于除我的IP地址以外的所有人。

So the structure is as follows:

所以结构如下:

mysite.com/blog // visitor access allowed

mysite.com/blog //允许访问者访问

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?

你有关于如何通过.htaccess mod_rewrite这样做的建议吗?

2 个解决方案

#1


18  

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]

#2


3  

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

您应该创建一个处理特定重定向的页面,然后编辑您的htaccess文件,如下所示:

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.

rediret_page.html可以有js或服务器端重定向来处理它们最终的位置,但它会强制所有未通过处理重定向的特定页面来到你的IP地址的人。

#1


18  

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]

#2


3  

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

您应该创建一个处理特定重定向的页面,然后编辑您的htaccess文件,如下所示:

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.

rediret_page.html可以有js或服务器端重定向来处理它们最终的位置,但它会强制所有未通过处理重定向的特定页面来到你的IP地址的人。