![](/img/trans.png)
[英]Why is htaccess redirect not working when URL variable is NOT included?
[英]Why is my .htaccess redirect url not working
當有人訪問我的網站時,我試圖更改我的網址,因此非 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]
來源:
您可以在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.