【问题标题】:htaccess, URL rewriting and redirect 301htaccess,URL 重写和重定向 301
【发布时间】:2016-09-28 20:20:50
【问题描述】:

我在堆栈溢出中寻找这个但没有找到任何相关的答案:

我有一个 htaccess 文件:

RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.mywebsite.com/$1 [L,R,NE]

RewriteRule ^home$  /index.php [L]
RewriteRule ^home$  /index.php?content=home [L]
RewriteRule ^accueil$  /index.php?content=home&lang=fr [L]
RewriteRule ^home$  /index.php?content=home&lang=en [L]

RewriteRule ^acceuil-message-1$  /index.php?content=home&val=1&lang=fr [L]
RewriteRule ^home-notice-1$  /index.php?content=home&val=1&lang=en [L]
RewriteRule ^acceuil-message-2$  /index.php?content=home&val=2&lang=fr [L]
RewriteRule ^home-notice-2$  /index.php?content=home&val=2&lang=en [L]

RewriteRule ^news-a-la-une$  /index.php?content=news&mode=featured&lang=fr [L]
RewriteRule ^featured-news$  /index.php?content=news&mode=featured&lang=en [L]

RewriteRule ^news-recentes-([0-9]+)$  /index.php?content=news&mode=list&page=$1&lang=fr [L]
RewriteRule ^latest-news-([0-9]+)$  /index.php?content=news&mode=list&page=$1&lang=en [L]
...

效果很好。

但我需要设置 301 重定向。 所以,我只是将每个 [L] 更改为 [R=301,L]

但是当我这样做时,我的 URL 又像以前在浏览器上一样回来了。

我认为重定向被应用了两次,这可以解释为什么我没有任何错误消息同时仍然显示旧网址,但我不知道在哪里应用 [R=301,L]以及在哪里不让它发挥作用。

感谢您的帮助。

【问题讨论】:

  • 您尝试混合使用重定向和重写。如果您只是重写 URL,则不需要 301。 “长”网址将不可见。如果您将页面移动到另一个位置,301 很有用,但我不认为这是您在这里尝试做的事情。
  • 谢谢索尼克。事实上,我想重写 URL,但我还需要重定向,因为我需要 Google 搜索不再指向网站的先前 URL。我几乎在所有地方都读到 301 重定向是解决这个问题的方法。那么,我应该怎么做才能让这个文件做我需要做的事情呢?谢谢。
  • 那么之前的网址是什么? index.php?....
  • 很抱歉,我不明白你的问题。这些以前的 url 在上面的 htaccess 示例中清晰可见,不是吗 (index.php?content=...)?

标签: .htaccess url url-rewriting url-redirection http-status-code-301


【解决方案1】:

因此,除了 RewriteRules 之外,您还必须将 301 重定向如下:

RedirectMatch 301 ^/index.php$ /home [L]
RedirectMatch 301 ^/index.php?content=home&val=([0-9]+)&lang=fr /acceuil-message-1$ [L]

PS:acceuil 中有一个类型,它应该是 accueil

参考:https://httpd.apache.org/docs/current/mod/mod_alias.html#redirectmatch

参考:http://www.larousse.fr/dictionnaires/francais/accueil/569

【讨论】:

猜你喜欢
  • 2013-01-27
  • 2013-12-21
  • 2015-03-04
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
相关资源
最近更新 更多