【问题标题】:How can i create better urls using .htacces modrewrite如何使用 .htaccess mod_rewrite 创建更好的网址
【发布时间】:2014-11-02 02:32:27
【问题描述】:

这是我现在的产品网址

http://www.example.com/product.php?product=32723

我想实现这个

http://www.example.com/product/32723-brand-model-productname

我一直在修改我的 .htaccess,但真的不知道如何实现这一点。

【问题讨论】:

    标签: php .htaccess url mod-rewrite


    【解决方案1】:

    您必须将您的 URL 与 RewriteRule 模式匹配并将其重写为目标 URL

    RewriteRule ^/product/(\d+)- /product.php?product=$1
    

    此模式匹配以 /product/ 开头的任何 URL,并捕获以下数字 (\d+) 后跟破折号 -。替换 URL 将为 /product.php?product=,并附加捕获的数字 $1

    要捕获匹配的某些部分,请将其括在括号 (...) 中。在 Apache mod_rewrite Introduction - Regular Expressions 阅读更多关于 mod_rewrite 中使用的正则表达式。

    【讨论】:

    • 我能够使用这个 RewriteRule ^repuesto/([^/.]+)-?$ product.php?product=$1 [L] 来实现它,但现在页面链接和图像被扭曲了,我想知道为什么。谢谢!
    【解决方案2】:

    这个问题太常见了,我只是在这里用代码给出了整个答案:

    http://www.prescia.net/bb/coding/5-141018-simple_friendly-url

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-10
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      相关资源
      最近更新 更多