代码之家  ›  专栏  ›  技术社区  ›  Matt Blaine

在网站根目录和子目录中使用mod_rewrite in.htaccess

  •  0
  • Matt Blaine  · 技术社区  · 17 年前

    在我的网站根目录中的.htaccess中:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    

    在子目录/foo中的.htaccess中

    RewriteEngine On
    RewriteRule ^page1\.html$ /foo/page2.html [R=301]
    

    在第二个例子中,我将foo子目录中对page1.html的请求重定向到page2.html,也在该子目录中。

    在我的浏览器中,尝试访问:

    http://www.example.com/foo/page2.html <==很好,很好

    http://www.example.com/foo/page1.html 好的

    http://example.com/foo/page1.html <==重定向到 http://www.example.com/foo/page2.html 好的

    http://example.com/foo/page2.html

    ==>应重定向到:http://**www.*example.com/foo/page2.html

    如果 该子目录不包含.htaccess文件,或者它包含并指定了对该特定请求生效的重写规则。

    有人能看出我做错了什么吗? ?


    为了记录在案,我必须将站点根目录中文件中的重写规则更改为:

    RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]
    

    这很好。谢谢

    2 回复  |  直到 17 年前
        1
  •  4
  •   user3850 user3850    17 年前

    中缺少以下指令 foo/.htaccess :

    RewriteOptions inherit
    

    the documentation

        2
  •  2
  •   Owen Ryan Doherty    17 年前

    /.htaccess 你没有申请 /foo/.htaccess . 典型的 .htaccess 文件本身:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    RewriteRule ^foo/page1\.html$ /foo/page2.html [R=301]
    
    推荐文章