【问题标题】:How can I rewrite rules for subfolders?如何重写子文件夹的规则?
【发布时间】:2021-02-13 17:57:05
【问题描述】:

我对 htaccess 中的简单重写规则有疑问...

我的 htaccess 是这样的:

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^company$                              /company.php?lang=en [QSA]
RewriteRule ^company/about$                        /article.php?lang=en [QSA]

在本地,它可以工作。 但是在网上是不行的。 如果我访问 URL “www....com/company/about”,我有 company.php 页面... 你能向我解释我的问题是什么吗? 非常感谢

【问题讨论】:

    标签: apache .htaccess url mod-rewrite url-rewriting


    【解决方案1】:

    这样吧:

    Options -MultiViews
    RewriteEngine On
    RewriteBase /
    
    ## Unless directory, remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^(.+)/+$
    RewriteRule ^ %1 [R=301,NE,L]
    
    RewriteRule ^company$ company.php?lang=en [L,NC,QSA]
    
    RewriteRule ^company/about$ article.php?lang=en [L,QSA,NC]
    

    重要的变化是禁用选项MultiViews。选项MultiViews(参见http://httpd.apache.org/docs/2.4/content-negotiation.html)由运行before mod_rewriteApache's content negotiation module 使用,并使Apache 服务器匹配文件的扩展名。因此,如果 /file 是 URL,那么 Apache 将提供 /file.php(如果您的系统中存在)。

    我还在您的两条规则中添加了L(最后一个)和NC(忽略大小写)标志。

    【讨论】:

      猜你喜欢
      • 2012-11-20
      • 2012-11-22
      • 1970-01-01
      • 2023-03-13
      • 2016-08-16
      • 2012-02-24
      • 1970-01-01
      • 2012-08-20
      • 2014-04-08
      相关资源
      最近更新 更多