Apache .htaccess 301重定向规则大全,赶紧收藏吧

Apache .htaccess 301重定向规则大全,赶紧收藏吧

V+变量
2025-01-19 / 5 评论 / 9 阅读 / 正在检测是否收录...

本文适用于Apache或基于Apache的第三方Web Server软件如LiteSpeed等,需要支持.htaccess文件的环境。Nginx下的301见Nginx常用的301重定向规则

1单文件的重定向

适用于网站结构调整,而又完成URL权重的传递。

同域名下将example.com/oldfile.htm重定向至 example.com/newfile.htm

Redirect 301 /oldfile.htm /newfile.htm

2将文件重定向至不同域名下,如 example.com/oldfile.htm 重定向至 example.net/newfile.htm:

Redirect 301 /oldfile.htm example.net/newfile.htm

给站点换域名

适用于给站点更换域名,比如站点弃用域名example.com,打算启用新域名example.net ,这时候我们需要做301重定向让搜索引擎知道我们启用了新域名来完成整站权重的转移。

在站点根目录.htaccess文件中添加以下规则:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301,NC]
full domain 301 redirect

强制使用www访问

强制使用www来访问,将example.com重定向至www.example.com

了解点SEO的朋友都知道搜索引擎喜欢URL唯一化,如 example.com 和 www.example.com 访问内容完全一样的时候比较妥善的做法是统一URL,我们知道网站访客对页面内容有足够兴趣的时候他们可能会在互联网上传播你的内容,如果两个域名未做统一标准化URL处理的的时候会出现这种情形:访客A分享了内容 example.com/1.html,访客B分享了内容 www.example.com/1.html,这样一来显然对SEO非常不友好的。

如何解决?

建立统一地标准URL,你如果想全部使用 www.example.com作为主域的话可以在.htaccess文件中添加以下规则来强制example.com重定向至带有www的域名访问:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
force www 301 redirect

强制不使用www访问

强制删除www来访问,将www.example.com重定向至example.com

该功能和上一段完全相反,其实移除www访问让域名变得更简短倒也是一种不错的选择:使得域名更简短,特别在手机上如果手工访问站点的话会方便得多。规则如下

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
force non www 301 redirect

更换文件扩展名

适用于文件后缀名的更换,如example.com/file.php 替换为 example.com/file.htm

RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]
file extension 301 redirect

强制https访问

适用于开启了ssl的站点,需要统一使用https访问

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

强制http访问

处于某种原因如证书到期了又不想续了,需要关闭ssl将URL由https重定向至http

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
0

评论 (5)

取消
  1. 头像

    [...]仅适用于Nginx, Apache请移步: Apache .htaccess 301重定向规则大全,赶紧收藏吧注意: 所有代码作用范围都在server配置区域中:server {[...]

    回复
  2. 头像
    marketing strategy
    MacOS · Google Chrome

    Great goods from you, man. I've consider your stuff prior
    to and you're just too magnificent. I really like
    what you've bought here, certainly like what
    you're saying and the way in which during which
    you assert it. You're making it enjoyable and you continue to care for to keep
    it wise. I cant wait to learn far more from you. That is actually a great web site.

    回复
  3. 头像

    Hi there all, here every person is sharing such know-how, so it's fastidious to read this web site, and I used to pay a quick visit
    this webpage every day.

    回复
  4. 头像
    automotive consulting
    Windows 10 · MicroSoft Edge

    Great blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple tweeks would really make my blog jump out.
    Please let me know where you got your theme. Cheers

    回复
  5. 头像
    raja dunia togel sd
    Linux · FireFox

    Hi, Neat post. There is a problem together with your site in web explorer, would test this?

    IE still is the market leader and a big section of other folks will miss your wonderful writing due
    to this problem.

    回复