【问题标题】:htaccess two folder rewrite rulehtaccess 两个文件夹重写规则
【发布时间】:2019-01-20 04:46:06
【问题描述】:

我已经使用了这个 htaccess 规则并且它有效

RewriteRule ^community/profile/([^/]+) community/profile/profile.php?id=$1

因此,当我在个人资料页面(例如 website.com/community/profile/example-user)上时,我将能够从 id=$1 部分中提取用户。

我需要将此扩展到两个文件夹,其中 example-user 可以有 2 个子文件夹,例如 website.com/community/profile/example-user/folder1/ 和 website.com/community/profile/example-user/folder 2/

我尝试了以下规则:

RewriteRule ^community/profile/([^/]+)/([^/]+) community/profile/profile.php?id=$1&car=$2

不幸的是,这违反了整个规则并且不起作用。我从这篇帖子https://webmasters.stackexchange.com/questions/65785/rewrite-up-to-three-folders-into-three-query-string-parameters-to-a-php-script得到了这个想法

我也试过了

RewriteRule ^community/profile/../([^/]+) community/profile/profile?id=$1&car=$2

也没有用。

为了清楚起见,如果一个人只进入“第一个”文件夹,在这个例子中是/example-user/,汽车变量应该是空的。如何编写此规则,以便将第二个“文件夹”放入第二个变量中,以便稍后在我的 php 文件中提取它?

【问题讨论】:

    标签: php .htaccess url-rewriting


    【解决方案1】:
    • 确保在正则表达式中使用锚点$ 进行精确匹配
    • 在字符类中包含点以避免再次匹配

    规则:

    RewriteRule ^community/profile/([^/.]+)/?$ community/profile/profile.php?id=$1 [L,QSA,NC]
    
    RewriteRule ^community/profile/([^/.]+)/([^/.]+)/?$ community/profile/profile.php?id=$1&car=$2 [L,QSA,NC]
    

    【讨论】:

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