【问题标题】:URL rewriting-code for rewritingURL重写-重写代码
【发布时间】:2010-10-22 20:16:03
【问题描述】:

我正在为一个网站工作,它在 php 中.....我想重写 url

e.g www.3idiots.co.in/stories.php?id=17

如果我想改写成

www.3idiots.co.in/stories/17.html

谁能告诉我在 .htaccess 文件中写入的代码?

【问题讨论】:

  • 不是一个真正的答案,但你的网站在我的笔记本电脑上看起来很糟糕,分辨率为 1280x800

标签: php url-rewriting


【解决方案1】:

我假设您正在使用带有 mod_rewrite 的 Apache。类似的东西

RewriteEngine On
RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1

应该可以解决问题。当然,您需要确保该目录中允许使用 RewriteRule。请参阅this wiki page 了解更多信息。

【讨论】:

  • 你在“stories”之后忘记了一个“/”:RewriteRule ^/stories/([0-9]+)\.html /stories.php?id=$1
【解决方案2】:

mod_rewrite 只能重写/重定向请求的 URI,而不是 HTML 文档中的那些。因此,您应该首先确保您的 PHP 应用程序正在打印正确的 URI,因此是 /stories/17.html 而不是 /stories.php?id=17

之后,您可以使用 José Basilio 建议的规则:

RewriteRule ^stories/([0-9]+)\.html$ stories.php?id=$1

虽然将/stories.php?id=17 的请求从外部重定向到/stories/17.html,然后在内部返回到/stories.php?id=17 是可能的,但这不是一个好的做法,因为这会导致请求数量增加一倍。但这里的规则是:

RewriteCond %{THE_REQUEST} ^GET\ /stories\.php[?\s]
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([0-9]+)&*([^&].*)?$
RewriteRule ^stories\.php$ /stories/%3.html?%1%4 [L,R=301]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2012-01-17
    相关资源
    最近更新 更多