创建一个*简单* .htaccess文件,可以重定向Apache中的某些页面



我在使用.htaccess文件重定向来自旧站点的流量时遇到问题。 大部分内容已移动。一些静态内容保留在旧位置,因此我想重定向其中一些旧路径。 我们曾经有一个可以处理不区分大小写的 CMS。 现在它消失了,我想用 .htaccess 文件来处理它。

我想使用 302,因为 301 更难测试。 完成后,我将切换到 301。

这是整个.htaccess file

#
# Old site redirects
#
# /link1
Redirect 302 /link1 /file.pdf
Redirect 302 /LINK1 /file.pdf
# Everything Else
Redirect 302 / http://www.newsite.com/

如果用户转到:

  • http://www.oldsite.com/link1重定向至"http://www.newsite.com/file.pdf"
  • http://www.oldsite.com/LINK1重定向至"http://www.newsite.com/file.pdf"
  • http://www.oldsite.com/lINk1重定向至"http://www.newsite.com/file.pdf"
  • http://www.oldsite.com/news重定向至"http://www.newsite.com/news"
  • http://www.oldsite.com重定向至"http://www.newsite.com/"
  • http://www.oldsite.com/重定向至"http://www.newsite.com/"

什么不是重定向:

  1. 不区分大小写的网址
  2. /news链接
  3. 最后两个测试(没有文件的纯网址)

简单

# redirects exact case-insensitive /link1 to a single file 
# if you want to redirect URL starting with /link1... to a file, remove $
RedirectMatch 302 (?i)^/link1$ http://www.newsite.com/file.pdf
# redirects any other url from the old site to the corresponding url on a new one
# so oldsite/some-URL will be transformed into newsite/some-URL
Redirect 302 / http://www.newsite.com/

如果只想从旧服务器的根重定向到新服务器的根,可以使用

RedirectMatch 302 ^/$ http://www.newsite.com

最好使用mod_rewrite规则来实现其正则表达式功能和其他功能,例如不区分大小写的处理:

RewriteEngine On
RewriteRule ^link1(/.*)?$ http://www.newsite.com/file.pdf [L,NC,R=302]
# Everything Else
RewriteRule (.+) http://www.newsite.com/$1 [L,NE,R=302]

最新更新