htaccess 显示不同 ip 的不同根

发布于 2025-01-15 10:15:19 字数 278 浏览 1 评论 0原文

我有以下 .htaccess 文件:

RewriteEngine On 

RewriteCond ${ipmap:%{REMOTE_ADDR}} ^allow$            [NC]

RewriteRule ^ - [F,L]

我想知道是否可以将 IP 从列表重定向到另一个文件夹,但它们仍然看到相同的 URL (example.com) 而不是 example.com/folder

I have the following .htaccess file:

RewriteEngine On 

RewriteCond ${ipmap:%{REMOTE_ADDR}} ^allow$            [NC]

RewriteRule ^ - [F,L]

I would like to know if it's possible to redirect the IP's from the list to another folder but they still see the same URL (example.com) and not example.com/folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小鸟爱天空丶 2025-01-22 10:15:19

您可以执行以下操作:

RewriteEngine On 

# Rewrite requests to "/folder" for specific IPs
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^allow$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^folder/ folder%{REQUEST_URI} [L]

您不需要在与重写映射匹配的规则上使用 NC。 (假设您的地图始终使用allow,而不是任何其他混合大小写版本?)

如果所有内容都将重写到/folder并且您没有引用 /folder 之外的任何静态资源,那么您也许可以删除执行文件系统检查的两个条件(以测试请求是否尚未映射到文件或目录)。


作为额外的好处,您可以阻止用户直接访问 /folder 并使用类似以下上述重写之后的内容重定向回根目录(紧接在 >RewriteEngine 指令):(

# Redirect direct requests to "/folder" back to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^folder(?:$|/(.*)) /$1 [R=301,L]

注意:这假设您在 /folder 子目录中没有另一个包含 mod_rewrite 指令的 .htaccess 文件。如果您那么你需要而是在那个 .htaccess 文件中执行此重定向。)

You could do something like this:

RewriteEngine On 

# Rewrite requests to "/folder" for specific IPs
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^allow$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^folder/ folder%{REQUEST_URI} [L]

You shouldn't need the NC on the rule that matches from your rewrite map. (Assuming your map is consistently using allow and not any other mixed case version of this?)

If everything is to be rewritten to /folder and you aren't referencing any static resources outside of /folder then you can perhaps remove the two conditions that perform the filesystem checks (to test whether the request does not already map to a file or directory).


As an added bonus, you can prevent direct user access to the /folder and redirect back to the root with something like the following before the above rewrite (immediately after the RewriteEngine directive):

# Redirect direct requests to "/folder" back to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^folder(?:$|/(.*)) /$1 [R=301,L]

(NB: This does assume you don't have another .htaccess file in the /folder subdirectory that contains mod_rewrite directives. If you do then you would need to preform this redirect in that .htaccess file instead.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文