的.htaccess:力WWW除子文件夹 [英] .htaccess: force www except subfolder

查看:131
本文介绍了的.htaccess:力WWW除子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在为止我已经使用这个code从重定向请求 example.com/url/to/page www.example.com/url/to/page

Until now I have used this code to redirect request from example.com/url/to/page to www.example.com/url/to/page

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

最近我喜欢添加的 example.com/x/abc shortlinks到我的网页 这些链接不​​应该在一开始有WWW

Recently I added shortlinks like example.com/x/abc to my page These links should not have "www" at the beginning

我试过

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

但这不起作用。萤火虫显示2重定向:非www到www,然后shortlink到longlink,而不是仅仅1重定向shortlink到longlink。我还清空缓存每次我改变的.htaccess时间

but this does not work. Firebug shows 2 redirects: non-www to www and then shortlink to longlink instead of just 1 redirect shortlink to longlink. I also empty cache each time I change .htaccess

如何才能做到正确的重定向?

How can I achieve the correct redirection?

推荐答案

这应该工作:

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

%{REQUEST_URI} 开始从一个斜线开始匹配。

%{REQUEST_URI} starts matching from a slash at the start.

这篇关于的.htaccess:力WWW除子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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