【问题标题】:.htaccess redirect issue - www redirect working as expected, but index.html not redirecting to root.htaccess 重定向问题 - www 重定向按预期工作,但 index.html 未重定向到根目录
【发布时间】:2014-10-09 10:53:54
【问题描述】:

我看到整个网络上都有很多类似的问题,但目前我找不到解决我特定的 .htaccess 问题的问题!在我的 .htaccess 文件中,我有规则将 a) 非 www 重定向到 www 和 b) index.html 到根目录。满足非 www 到 www 条件并成功重定向。但是,index.html 文件没有重定向到根目录。

我还用http://htaccess.madewithlove.be 进行了测试。那里的调试日志指示“不支持此变量:%{THE_REQUEST}”,随后,不满足 index.html 重定向的重写条件,但我无法推测为什么或该变量是否是真正的罪魁祸首。我还将提到这个特定域是另一个域的子域/附加域。下面描述的 .htaccess 文件位于子域的根目录中。主域级别的.htaccess文件目前为空白。

这是我现有的 .htaccess 内容:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

感谢您的帮助!

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect


    【解决方案1】:

    在根 .htaccess 中保留这样的规则:

    DirectoryIndex index.html
    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
    
    RewriteCond %{THE_REQUEST} /index\.html [NC]
    RewriteRule ^(.*?)index\.html$ /$1 [L,R=302,NC,NE]
    

    在新浏览器中测试。

    【讨论】:

    • DirectoryIndex 不是必需的,如果 AllowOverride 是限制性的,也可能会导致问题。
    • DirectoryIndex 在这里非常需要,因为 OP 正在从路径中删除 index.htmlDirectoryIndex 没有设置,那么它只会列出目录内容。
    • DIrectoryIndex 也可能在 Directory 或 VHost 上下文中设置 - 我想它已经为 OP 设置了
    • 有时它存在于 Apache 配置中,有时它不存在。
    【解决方案2】:

    删除第一个 RewriteCond。它不需要满足您的需求。单独的第一个 RewriteRule 将解决您的问题。

    也许您还想更新您的第一个RewriteRule。在当前形式中,它将重定向(不管使用的域)到http://www.example.com/。如果您想“保留”使用的子域,请将重定向设为相对:

    RewriteRule ^(.*)index\.html?$ /$1 [R=301,L]
    

    【讨论】:

    • 糟糕...但是,它不是必需的。更新答案。
    • 嗨@MrTux,非常感谢您的回复和anubhava。不幸的是,两者都没有解决 index.html 重定向。我清除了缓存以仔细检查。请参阅hindivers.com/index.html - 由于某种原因没有发生重定向。我的 .htaccess 代码现在如下所示。我也尝试过使用和不使用 DirectoryIndex。 Options +FollowSymLinks RewriteEngine On RewriteRule ^(.*)index\.html?$ /$1 [R=301,L] RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]如果您对这种怪异有进一步的了解,将不胜感激!
    • 您似乎正在运行一个 nginx 服务器(在hindivers.com 上),而不是一个 apache httpd。 .htaccess 在那里不起作用。
    • 嗨 MrTux!我的服务器统计数据表明它正在运行 Apache 版本 2.2.26
    • 好的,然后尝试使用RewriteLog "/tmp/rewrite.log" (httpd.apache.org/docs/2.2/mod/mod_rewrite.html) 并检查你得到了什么。
    猜你喜欢
    • 1970-01-01
    • 2015-03-15
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多