0

I'm trying to implement a SEO friendly URL using .htaccess by using the RewriteRule below

RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1

The actaul URL looks like this

http://localhost/n/article.php?title=this-is-the-first-news-article

but I want it to look like this :

http://localohst/n/article/this-is-the-first-news-article

When I applied the RewiteRule above it does not change to the desired URL

4

3 回答 3

2

这应该这样做。你错过了n。不知道为什么你需要这个词标题。

RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1
于 2013-07-25T12:35:40.833 回答
1

您必须先捕获查询字符串。您不能使用 RewriteRule 执行此操作,因为它们会忽略查询字符串。这里我们使用[R]重定向。如果这对您有用,并且旧 URL 有可能作为链接存储在某处,那么您可能需要指定[R=301]. 请务必从您的网站中删除所有旧式链接(包含我们正在重写的以前的链接格式),这样您就不会因不更新链接而受到惩罚。

RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} title=([^&]+)
RewriteRule ^n/article.php n/article/%1? [R,L]

如果您的网站需要格式才能从原始格式中运行,则在第一条规则之后您可能还需要此格式。此规则悄悄地将 URL 重定向回原始 URL,而不向最终用户显示:

RewriteRule ^n/article/(.*) n/article.php?title=$1 [L]
于 2013-07-25T13:47:27.233 回答
0

它将是 RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1

于 2013-07-25T12:39:29.063 回答