【问题标题】:Apache Redirect problem in .htaccess.htaccess 中的 Apache 重定向问题
【发布时间】:2009-02-08 16:22:11
【问题描述】:

我无法让简单的重定向声明在我的 Godaddy 帐户上生效。我的 .htaccess 文件中有以下语句:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.mydomain.net$ [NC]
RewriteRule ^(.*)$ http://mydomain.net/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^/lists/$ / [R=301]

RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^/blog/$ http://myotherdomain.net/ [R=301]

第一次重定向总是有效的。然而,第二和第三个,永远不会工作。我刚从服务器得到一个404。 Apache 日志没有显示任何有用的信息 - 只是 404。

任何想法,任何人? 对你的帮助表示感谢。 谢谢

【问题讨论】:

    标签: apache mod-rewrite redirect


    【解决方案1】:

    按目录重写
    .htaccess 文件中使用重写引擎时,每个目录的前缀(对于特定目录始终相同)会自动删除以进行模式匹配并自动添加替换完成后。
    http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule

    因此,只需将前导斜线留在模式之外。

    【讨论】:

      【解决方案2】:

      对于这样的简单重定向,最好使用简单的RedirectMatch 指令:

      RedirectMatch 301 ^/lists/$ http://mydomain.net/
      RedirectMatch 301 ^/blog/$ http://myotherdomain.net/
      

      如果您坚持使用重写,请确保将 L 标志添加到您的规则中。

      Apache mod_rewrite Flags 说:

      您几乎总是希望将 [R] 与 [L] 结合使用(即使用 [R,L]),因为 [R] 标志本身会将 http://thishost[:thisport] 附加到 URI,但随后将此传递给规则集中的下一条规则,这通常会导致“请求中的 URI 无效”警告。

      【讨论】:

        【解决方案3】:

        只需删除开头的斜线即可。使末尾的斜线可选也可能很有用。

        RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
        RewriteRule ^lists/{0,1}$ / [R=301]
        
        RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
        RewriteRule ^blog/{0,1}$ http://myotherdomain.net/ [R=301]
        

        【讨论】:

          【解决方案4】:

          把第一个放在最后。一旦遇到重定向匹配,它就会运行它并忽略其余部分。

          【讨论】:

            猜你喜欢
            • 2019-09-25
            • 1970-01-01
            • 2018-09-09
            • 2021-09-25
            • 2015-08-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多