当前位置:首页 > 技术经验 > 正文内容

不带WWW跳转到带WWW地址Apache重写规则

6年前 (2018-08-12)技术经验1373

案例:

访问 http://benbenshen.com ,则跳转到 http://www.benbenshen.com。

访问 http://www.benbenshen.com ,则不再跳转,直接访问。

即使用 http://www.benbenshen.com作为网站的主网址。

解决方法:

第一步:Apache虚拟机配置:

<VirtualHost *:80>
ServerAdmin admin@benbenshen.com
DocumentRoot “/var/www/www.benbenshen.com/”
ServerName www.benbenshen.com
ServerAlias benbenshen.com //这句是关键,配置别名
ErrorLog “logs/dummy-host2.benbenshen.com-error.log”
CustomLog “logs/dummy-host2.benbenshen.com-access.log” common
<Directory />
AllowOverride All
Allow from all
</Directory>
</VirtualHost>

第二步:HTACCESS写法

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^benbenshen.com [NC]
RewriteRule ^(.*) http://www.benbenshen.com/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>


扫描二维码推送至手机访问。

版权声明:本文由笨笨神发布,如需转载请注明出处。

本文链接:https://www.benbenshen.com/post/8.html

标签: apache
分享给朋友:

相关文章