同时保留子文件夹的http到www? [英] http to www while preserving subfolder?

查看:93
本文介绍了同时保留子文件夹的http到www?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.htaccess中有以下规则,可以从domain.tld重定向到www.domain.tld,并且工作正常.

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]

但是,当尝试访问domain.tld/forum时,我被重定向到www.domain.tld

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

如何在保留访问者所在的子文件夹的同时将所有非www重定向到www?

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

推荐答案

使用REQUEST_URI变量:

RewriteEngine On

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

%{REQUEST_URI}将具有完整的请求URI,而不是$1

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

这篇关于同时保留子文件夹的http到www?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆