.htaccess mod_rewrite不适用于友好的URL

时间:2013-10-11 16:37:44

标签: php .htaccess mod-rewrite

我已完成了多次搜索并遵循了教程,但我似乎无法完成以下工作。我想使用.htaccess mod_rewrite创建SEO友好URL。我的例子是展示新闻故事。目前的网址结构如下:

http://www.website.com/news/details/?newsID=40

我想将其改为:

http://www.website.com/news/name-of-news-article/

以下是我目前用于重写的.htaccess文件:

RewriteEngine on
RewriteRule ^news/([^/]+)/?$ /news/details/?newsID=$1 [L]

在.htaccess文件中只有这个,URL上没有任何反应,它只是保持原来的状态。我知道该网站可以读取.htaccess文件。

我猜想在那里得到实际的文章名称,我必须传递一个包含该名称的参数,但即使在当前的例子中,也没有发生任何事情。任何援助将不胜感激!谢谢。

1 个答案:

答案 0 :(得分:0)

从匹配模式中删除前导斜杠。

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/news/details/ [NC]
RewriteRule ^news/([^/]+)/?$ /news/details/?newsID=$1 [L]

原因: URI中的.htaccess内部斜杠内部不匹配。

了解更多信息:Apache mod_rewrite Introduction

PS:虽然请记住重写的URI将是/news/details/?newsID=name-of-news-article

修改

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+test\.htm[?\s?] [NC]
RewriteRule ^ /hello? [R=302,L]