【问题标题】:htaccess RewriteRule doesn't work properly if no trailing slash如果没有尾部斜杠,htaccess RewriteRule 将无法正常工作
【发布时间】:2016-04-21 18:14:12
【问题描述】:

我正在尝试使用主目录的 .htaccess 文件中的 RewriteRule 将一个子目录重定向到另一个子目录。

例如:http://website.com/subdir 应重写为 http://website.com/another_dir/destination_dir,但用户仍应在地址栏中看到 http://website.com/subdir

如果用户使用尾部斜杠结束 URL,这将非常有效。示例:http://website.com/subdir/(生成 200

但是,如果省略斜杠,则会生成 301 重定向,并且我们会看到不需要的目标目录。例如,http://website/subdir 将用户重定向到http://website/another_dir/destination_dir

以下是 .htaccess 的相关部分:

# URL Rewriting:
RewriteEngine On
RewriteBase /

...

# Redirect subdirectories:
RewriteRule ^subdir(.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L]

感谢任何帮助!

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以调整重写规则中的正则表达式以选择匹配斜线。

    RewriteRule ^subdir(/?.*)$ another_dir/destination_dir$1 [PT,NC,QSA,L]
    

    注意/? 就在subdir 之后。这表示您的子目录后面可能有也可能没有斜杠,因此正则表达式将匹配任何一种方式。

    【讨论】:

      【解决方案2】:

      快速解决方法是

      DirectorySlash OFF 在这个 htaccess 中,DirectorySlash ON 一个 .htaccess 在 another_dir/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2015-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-19
        • 1970-01-01
        相关资源
        最近更新 更多