.htaccess 作用域
I have a website. But I would like use the www.example.com instead of example.com.
我有一个网站。 但我想使用www.example.com而不是example.com。
How to redirect non-www domain to www domain on my server? I am using apache2 (httpd).
如何将非www 域重定向到服务器上的www域? 我正在使用apache2( httpd )。
You can add a piece of code into the top .htaccess in your site:
您可以在网站的顶部 .htaccess中添加一段代码:
具体方法:将example.com域重定向到www.example.com域 (Specific method: redirect example.com domain to www.example.com domain)
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
通用方法:将非www域重定向到www域 (General method: redirect non-www domains to www domains)
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Bonus:
奖金:
将www域重定向到非www域 (Redirect www domains to non-www domains)
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-redirect-non-www-domain-to-www-domain-in-htaccess/
.htaccess 作用域