【问题标题】:SEO friendly URL not working using .htaccessSEO 友好 URL 无法使用 .htaccess
【发布时间】:2013-07-25 08:23:10
【问题描述】:

我正在尝试使用下面的 RewriteRule 使用 .htaccess 实现一个 SEO 友好的 URL

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

实际网址如下所示

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

但我希望它看起来像这样:

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

当我应用上面的 RewiteRule 时,它​​不会更改为所需的 URL

【问题讨论】:

  • 你启用了mod_rewrite 吗?
  • 正如 Farkie 所说,我会从 URL 中删除 title。在那里没有任何意义。

标签: php regex .htaccess url


【解决方案1】:

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

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

【讨论】:

  • 我从 URL 中删除了 title 并使用了这个 RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1 但仍然无法正常工作。
  • 这与他们的要求背道而驰。
【解决方案2】:

您必须先捕获查询字符串。您不能使用 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]

【讨论】:

  • 感谢您的回答,但仍然无法正常工作。我仍然得到正常的网址
【解决方案3】:

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

【讨论】:

  • 这与他们要求的相反。
猜你喜欢
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 2013-01-22
  • 1970-01-01
  • 2013-02-03
  • 2016-03-12
  • 2015-04-17
相关资源
最近更新 更多