当有人访问我的网站时,我正在尝试更改我的网址,因此非 www 重定向到 www ...http://example.com 到 http://www.example.com
我找到了这个代码
RewriteBase /
RewriteCond %{HTTP_HOST} ^example'.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
但是当我将其添加到我的 .htaccess 时,我收到一个错误......这是错误的代码吗?如何将其添加到我的 .htaccess 而不会出错?
我的 .htaccess 域名
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ / profile.php?username=$1
将以下行添加到您的 .htaccess 文件中;
RewriteCond %{HTTP_HOST} !^www'.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
源:
- http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/
- 将非 www 重定向到 .htaccess 中的 www
您可以在下面添加它RewriteEngine On
:
RewriteCond %{HTTP_HOST} !^www'. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
使用以下代码覆盖您的 .htacess 文件。
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www'.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ / profile.php?username=$1
更多了解 .htaccess 规则:
http://www.web-technology-experts-notes.in/2014/02/htaccess-code-snippets-example.html