【问题标题】:URL rewrite with PHP and .htaccess使用 PHP 和 .htaccess 重写 URL
【发布时间】:2016-12-05 01:10:37
【问题描述】:

谁能告诉我如何重写以下网址:

www.mydomain.com/products.php?product=product-name

目前,它工作正常(我使用 $_GET 来获取唯一的产品名称并在我的页面上使用它)但我希望能够使用以下 URL 格式来获得相同的结果:

www.mydomain.com/products/product-name

我在这里看到了一些类似的例子,但无法让它们适应我的情况。

【问题讨论】:

  • 这个比较简单,有没有试过什么规则?

标签: .htaccess url-rewriting friendly-url


【解决方案1】:

这就是您的 .htaccess 的样子,

RewriteEngine On
RewriteRule    ^products/([A-Za-z0-9-]+)    /products.php?product=$1    [NC,L]

RewriteEngine On这是用来开启重写引擎的。

^products/([A-Za-z0-9-]+) 匹配诸如 (www.mydomain.com/products.php?product=product-name) 之类的 URL,其中产品名称可以是字母、数字和连字符的任意组合中的一个或多个。

并在/products.php?product=$1 中使用该字母组合,其中$1 表示产品名称。

【讨论】:

    猜你喜欢
    • 2012-04-29
    • 2012-08-25
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多