Home > Operation and Maintenance > Apache > Apache configures both 301 and https jumps

Apache configures both 301 and https jumps

(*-*)浩
Release: 2019-11-23 13:36:56
Original
3515 people have browsed it

Apache configures both 301 and https jumps

一般我会较多的使用WORDPRESS等程序,其在安装的时候我们如果直接用WWW打开,或者在后台设置WWW域名则默认会强制301指向WWW站点域名。        ( 推荐学习:apache入门 )

而这里有使用ZBLOG(php新版的也是支持的,后台设置固定链接即可)或者TYPECHO等其他博客程序则不会默认301跳转。理论上从用户体验,还是从搜索引擎,最好是统一要么WWW,要么不带WWW格式的网址。

所以,我准备在Apache中用301跳转强制WWW格式,这里我是用的Apache虚拟主机环境所以直接在根目录的.htaccess文件设置就可以。

第一、强制WWW跳转

RewriteEngine on
RewriteCond %{HTTP_HOST} ^cainiaoqidian\.com [NC]
RewriteRule ^(.*)$ http://www.cainiaoqidian.com/$1 [L,R=301,NC]
Copy after login

添加到伪静态文件中,立即生效。

第二、强制HTTPS格式

因为HTTPS加密格式网址也在陆续的流行和必须,所以我也在考虑和调整添加HTTPS格式的网址,毕竟免费SSL证书也比较多,添加也不复杂,刚才测试后还是可以的,唯独也需要将HTTPS强制跳转,因为HTTP和HTTPS也最好唯一。

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.cainiaoqidian.com/$1 [R,L]
Copy after login

同样的,可以在.htaccess文件中添加强制HTTPS跳转。

第三、补充301跳转,两种规则同时配置

RewriteEngine On
RewriteCond %{HTTP_HOST} !^cainiaoqidian.com$ [NC]
RewriteRule ^(.*)$ https://cainiaoqidian.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://cainiaoqidian.com/$1 [L,R=301]
Copy after login

如果我们有朋友喜欢用不带WWW的格式,所以我们也可以强制不带WWW跳转和HTTPS强制不带WWW。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^cainiaoqidian\.com [NC]
RewriteRule ^(.*)$ http://cainiaoqidian.com/$1 [L,R=301,NC]
Copy after login

The above is the detailed content of Apache configures both 301 and https jumps. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template