http to www while preserving subfolder?

流过昼夜 提交于 2019-12-12 18:10:55

问题


I have the following rule in .htaccess to redirect from domain.tld to www.domain.tld and it's working fine.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

However, when trying to access domain.tld/forum , I get redirected to www.domain.tld

How can I redirect all non-www to www while preserving whatever subfolder the visitor is in?


回答1:


Use REQUEST_URI variable:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

%{REQUEST_URI} will have full request URI rather than relative path (from current directory) captured in $1



来源:https://stackoverflow.com/questions/34181921/http-to-www-while-preserving-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!