【问题标题】:301 redirection using Apache's RewriteRule (for a Joomla website)使用 Apache 的 RewriteRule 进行 301 重定向(用于 Joomla 网站)
【发布时间】:2012-07-09 15:09:25
【问题描述】:

我在重定向某些页面时遇到了问题。我的重写规则如下所示(示例):

RewriteRule /index.php?option=com_content&view=article&id=47:article-keyword&catid=8:position$ article-keyword.html [R=301,L]

但它不起作用,我尝试了一些不同的组合,但它仍然什么也没做。 有什么解决办法吗?

【问题讨论】:

  • 你能添加一些关于你具体想要完成的文件吗?根据您发布的规则,您似乎想将一个非常具体的 URL /index.php?option=com_content&view=article&id=47:article-keyword&catid=8:position$ 更改为 article-keyword.html 如果是这种情况,这种重定向不会发生吗?
  • 是的,我有几个特定的​​链接我想重定向

标签: apache mod-rewrite redirect http-status-code-301


【解决方案1】:

更新:下面的 RewriteRule 不正确。有关详细信息,请参阅 2012 年 7 月 9 日的更新

我已经读到 RewriteRules 总是相对的,因此请尝试:

RewriteRule ^index\.php\?option=com_content&view=article&id=47:article-keyword&catid=8:position$ article-keyword.html [R=301,L]`

这应该将/index.php?option=com_content&view=article&id=47:article-keyword&catid=8:position 重定向到article-keyword.html

^ 指定字符串的开头。有关 Apache 的 RewriteRule 的更多信息,请查看 http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html。

更新 - 2012-07-08

好的,阅读 Apache 文档后,查询字符串上的 RewriteRule 不匹配。要匹配查询字符串中的数据,您需要使用RewriteCond 和%QUERY_STRING 变量。

成功!以下对我有用!

RewriteCond %{QUERY_STRING} option=com_content&view=article&id=47:article-keyword&catid=8:position
RewriteRule ^index\.php$ article-keyword.html? [R=301,L]

这会将http://site.domain.com/index.php?option=com_content&view=article&id=47:article-keyword&catid=8:position 重定向到http://site.domain.com/article-keyword.html

这只会将请求重定向到具有相关查询字符串的 index.php。

【讨论】:

  • 感谢您的回复,但仍然无法重定向,我已经尝试过 ^ 一开始没有任何帮助。
  • 你把这个 RewriteRule 放在哪里?如果在 .htaccess 中,请发布整个 .htaccess 文件。
  • 我将它放在 RewriteEngine On 语句和 RewriteBase 之后(也尝试过不设置 RewriteBase)。
  • 我认为问题在于您的文字字符串包含需要转义的正则表达式特殊字符。请参阅此SO 文章。这些字符.^$*+?()[{\ 需要转义。我将更新我的帖子以包括需要转义('\')。经过进一步审查,? 肯定需要转义 \?。
  • 这行得通!伟大的!非常感谢您的帮助和研究!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多