【问题标题】:.htaccess rewrite friendly URLs.htaccess 重写友好的 URL
【发布时间】:2011-09-25 03:03:50
【问题描述】:

我不确定这是否可行,但我希望将以下 URL 变得更友好:

http://www.myurl.com/content.php?id=13089093057550&slug=this-is-a-nice-url

我希望它看起来像这样:

http://www.myurl.com/this-is-a-nice-url

我该怎么做?

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    您可以将slug 放入查询字符串中,但是如果不在某个地方提供id,Apache 就不可能提供它。希望您有办法仅使用 URL slug 参数从数据库中获取 id。

    RewriteEngine On
    # We don't know the id
    RewriteRule (.*)$ /content.php?slug=$1 [L,QSA]
    

    【讨论】:

      【解决方案2】:

      您需要将 id 传递给您的 PHP 代码才能显示我认为的内容。例如,查看此问题的 Stack Overflow URL:http://stackoverflow.com/questions/6520671/htaccess-rewrite-friendly-urls,它同时传递了 idslug。所以你可以有友好的 URL,比如:

      http://www.myurl.com/13089093057550/this-is-a-nice-url
      

      如果你决定按照我的建议去做,那么这里是你在 .htaccess 中需要的代码:

      Options +FollowSymlinks -MultiViews
      RewriteEngine On
      
      RewriteRule ^([0-9]*)/(.*)/?$ /content.php?id=$1&slug=$2 [L,QSA]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-24
        • 2021-07-06
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-24
        相关资源
        最近更新 更多