【问题标题】:.htaccess - redirect, if a specific url match.htaccess - 重定向,如果特定的 url 匹配
【发布时间】:2019-11-28 01:47:50
【问题描述】:

我需要将用户重定向到一个新域,但如果用户访问特定 URL,我需要将他/她重定向到另一个 URL。

让我澄清一下……

如果用户访问http://oldexample.com/postvendita/,我需要将他们重定向到http://newexample.com/assistenza

否则,对于每个其他 URL http://oldexample.com/*,我需要将它们重定向到 http://newexample.com/new-page

这是我的尝试:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)postvendita(.*)$ http://www.newexample.com/assistenza [L,R=301]
RewriteCond %{HTTP_HOST} !^oldexample\.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/new-page [R=301,L]

现在如果我访问任何个旧页面,我将被重定向到http://www.newexample.com/new-page,所以第一个规则不起作用,我应该如何更改它?

【问题讨论】:

  • 我们是否应该假设您从同一个 http 服务器为两个域提供服务?
  • 您在此处重写 当前 请求,而不是“用户来自”的任何内容。
  • @CBroe 这不是真的。域名oldexample.com可以这样看。
  • 用户来自旧网址的 com 在谷歌或其他地方点击它
  • @arkascha 是的,两个站点现在都在同一个服务器上

标签: apache .htaccess redirect mod-rewrite


【解决方案1】:

要通过.htaccess 处理此问题,您需要匹配第一个并使用一个包罗万象来重定向其他所有内容:

RewriteEngine On

RewriteRule ^postvendita/?$ http://www.newexample.com/assistenza [R=301,L,NE]
RewriteRule .*              http://www.newexample.com/new-page   [R=301,L,NE]

301 重定向是一种永久重定向,它会在 90-99% 的 将果汁(排名能力)链接到重定向页面。 301指的是 此类重定向的 HTTP 状态代码。在大多数情况下,301 重定向是在网站上实现重定向的最佳方法。

More About Redirects

或者,如果您不习惯编写 RewriteRules,可以使用以下行:

Redirect 301 /postvendita/ http://www.newexample.com/assistenza
Redirect 301 /postvendita  http://www.newexample.com/assistenza
Redirect 301 /             http://www.newexample.com/new-page

或者RedirectMatch:

RedirectMatch 301 /postvendita/? http://www.newexample.com/assistenza
RedirectMatch 301 .*             http://www.newexample.com/new-page

Common .htaccess Redirects

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多