【问题标题】:htaccess rewrite key/value pair params in urlhtaccess 重写 url 中的键/值对参数
【发布时间】:2023-03-19 03:14:01
【问题描述】:

我知道这是一个非常常见的问题,但我似乎找不到任何关于我的问题的解决方案。文件夹结构如下所示:

- rootProject
  - subfolder
    * .htaccess
    * index.php

普通网址参数:http://website.com/subfolder/?dynamicKey=dynamicValue。 但是我想要实现的是这样的:http://website.com/subfolder/dynamicKey/dynamicValue

但我没有这样做,我只能检索dynamicKey

这是我目前的htaccess

RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]

【问题讨论】:

  • 可能是RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]?

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


【解决方案1】:

感谢@Ar Rakin。这是解决方案。

RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]

【讨论】:

    【解决方案2】:

    使用您显示的示例和尝试的代码,请尝试遵循 htaccess 规则。我们需要在重写规则之前添加条件检查,否则以后它也会影响您现有的页面。

    确保在测试您的 URL 之前清除您的浏览器缓存。

    RewriteEngine On
    ##Added conditions if non-existing pages requested then only perform rewrite.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
    
    ##Added conditions if non-existing pages requested then only perform rewrite.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]
    

    注意:您使用的正则表达式 [a-z] 将仅匹配 uri 中的小写字母,以防它需要检查任何字母(大写或小写)然后更改 FROM @ 987654323@ TO [a-zA-Z] 也在上述规则中。

    【讨论】:

    • 这里还有一点,请把你的htaccess和index.php文件也放在同级文件夹中。
    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多