【问题标题】:How to create mod_rewrite for 1 or 2 parameter如何为 1 或 2 参数创建 mod_rewrite
【发布时间】:2014-06-02 19:08:39
【问题描述】:

我的代码

www.test.com/index.php?A=1&B=123

我在 .htaccess 中使用此代码

RewriteRule ^(.*)/(.*).html index.php?A=$1&B=$2 

输出:www.test.com/1/123.html

确定它在 2 个参数上工作

问题:有时我的网站有 1 个参数

www.test.com/index.php?A=1

我想输出这个:www.test.com/1.html

如何为 1 或 2 参数创建 RewriteRule ?

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    您可以使用 2 种不同的规则:

    RewriteRule ^([^./]+)\.html$ index.php?A=$1 [L,QSA,NC]
    
    RewriteRule ^([^/]+)/([^.]+)\.html$ index.php?A=$1&B=$2 [L,QSA,NC]
    

    【讨论】:

    • RewriteRule column1 工作,但 RewriteRule column2 不工作
    • 我测试 echo $_GET A 和 B。结果如果 1 个参数:A=1 B="" 正常工作,如果 2 个参数:A=1/123 B=""它不工作跨度>
    • 很高兴知道,您能否通过单击我的答案左上角的勾号将答案标记为已接受。
    猜你喜欢
    • 2011-11-21
    • 2023-02-26
    • 2020-01-26
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2013-11-19
    相关资源
    最近更新 更多