【问题标题】:Rewrite URL dynamic with title and id用标题和 ID 重写 URL 动态
【发布时间】:2016-10-03 15:01:37
【问题描述】:

我需要用 title 和 id 动态重写 url。我想动态制作网址

http://dev.example.com/beweb/iphone-8.html

代替

http://dev.example.com/beweb/newsdetails.php?id=8&title=iphone

当我运行上面的 URL 时,它会动态生成我需要的内容.. 但出现错误

" Not Found
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The requested URL /beweb/iphone-8.html was not found on this server."

我使用了以下代码:

RewriteEngine On
RewriteCond %{THE_REQUEST} /beweb/newsdetails.php\?id=([^&\s]+)&title=([^&\s]+) [NC]  
RewriteRule ^ /beweb/%2-%1\.html? [NC,R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^%2-([^.]+)\.html$ /beweb/newsdetails.php?id=$1&title=$2 [QSA,L,NC]

请帮帮我。

【问题讨论】:

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


    【解决方案1】:

    规则几乎是正确的。您只是错误配置了最后一条语句,应该是:

    RewriteRule ^([^-]+)-([^.]+)\.html$ /beweb/newsdetails.php?id=$2&title=$1 [QSA,L,NC]
    

    【讨论】:

    • 我已经输入了代码并且它正在工作..谢谢:)。但是会产生其他问题,因为我想从 URL(获取参数)中获取 id。现在出现错误。请您帮帮我。
    • @Jeff 只需将上面的$1$2 互换即可。
    • 我需要帮助你..你能做到吗
    • @Jeff 我已经用 $1 和 $2 的变化更新了我的答案。
    • 我有一页:dev.example.com/beweb/news.php。所以在这个页面中,一个阅读更多的链接..这就是所谓的dev.example.com/beweb/newsdetails.php?id=8&title=iphone..所以每当我运行这个url所以我需要设置错误404找不到页面..我已经按照你提供的规则设置了
    【解决方案2】:

    通过这些行,您可以检查不是服务器上真正的静态文件或目录的路径,如果是否定的,则处理文件名以服务您的用户:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^beweb/([a-z]+)\-([0-9]+).html$ /beweb/index.php?id=$2&title=$1 [QSA,L,NC]
    

    QSA 表示将查询字符串添加到新的 URL

    【讨论】:

    • 只有代码的答案通常是不够的。添加一些解释为什么您的代码有效,以及为什么 OP 的代码无效。否则你的答案只能帮助那些只会复制粘贴你的答案的 OP - 这不是 SO 的使用方式。
    • @Al.G.是的,你是对的,我会尽快添加解释。
    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多