【问题标题】:redirect into subdirectory AND out of subdirectory重定向到子目录和子目录
【发布时间】:2010-03-30 05:56:25
【问题描述】:

我有一个我无法弄清楚的 mod_rewrite 重定向问题。

来自特定域的所有请求都会“静默”地重写到指定的子目录中。例如www.mydomain.net/hello.html 检索 /net/hello.html 中的文件。以下 .htaccess (放置在我的托管根目录中) 完美地实现了这一点:

RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post.
RewriteRule .* - [L]
rewriteCond %{HTTP_HOST} ^www.mydomain.net$
rewriteCond %{REQUEST_URI} !^/net.*$
rewriteRule (.*) /net/$1 [L]

然而,直接进入该目录的 URL 应该以 301 明显地重定向到 没有 该子目录的 URL。例如www.mydomain.net/net/hello.html 应该重定向到www.mydomain.net/hello.html(它仍然会在/net/hello.html 中检索文件)。我的 .htacces 文件(放在 /net 不幸的是不起作用:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(.*) /$1 [R=301,L]    

尽管根 .htaccess 文件中有 RewriteCond %{ENV:REDIRECT_STATUS} 200 块,但我得到了一个不定式重定向循环......所以出了什么问题?

顺便说一句,我必须使用 mod_rewrite,因为该站点是外部托管的,我无权访问 apache 配置。

非常感谢您的任何指点。

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect infinite-loop


    【解决方案1】:

    改为检查 THE_REQUEST 中的 HTTP request line

    RewriteCond %{THE_REQUEST} ^GET\ /net[/? ]
    RewriteRule ^net($|/(.*)) /$2 [L,R=301]
    

    【讨论】:

    • 谢谢秋葵!使用 THE_REQUEST 绕过循环。但是,我不得不将 RewriteRule 行更改为“RewriteRule ^(.*) /$1 [L,R=301]”,因为 .htaccess 文件是从子目录“net”执行的。
    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多