开发者

How to use .htaccess to redirect non-www pages to www pages with the exception of a single sub-folder

I'm trying to redirect all non www. urls leading to my site to www. urls - however, there's one subfolder on my site I don't want changed. The code I've been working with is:

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

Is there a wa开发者_如何学运维y to tell this piece of code to ignore domain.com/specialsubfolder?

Thanks a lot, tried searching for this, but couldn't quite find what I'm looking for.


The common way (easy to read and understand) is to add one more condition to ignore this rule if URL starts with /specialsubfolder :

Options +FollowSymlinks
RewriteEngine On

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

Alternatively, add such condition into matching pattern (more difficult to read but a tiny bit faster):

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteRule ^(?!specialsubfolder).* http://www.domain.com%{REQUEST_URI} [R=301,L]

P.S. You can add [NC] flags if case-insensitivity is required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜