Nikunj Bhatt
Nikunj Bhatt

Reputation: 197

add www in URL if no subdomain but exception with some folders

I want to add "www." to URLs if it doesn't contain "www." after "http://"; but it shouldn't get added if any file is requested from some specific folders.

Here is what I have tried.

RewriteEngine On
RewriteCond %{PATH_INFO} ^/exception_dir(.)*$
RewriteRule ^(.*)$ - [L]

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

With this code, presently, the "exception_dir" is NOT being exempted from redirection. What I am missing?

And how to write RewriteCond and/or RewriteRule if there are multiple directories to be exempted? Do I need to write both RewriteCond and RewriteRule for each of the directories separately?

Upvotes: 0

Views: 91

Answers (1)

Panama Jack
Panama Jack

Reputation: 24468

You can do your rules like this. In this example I also excluded 2 directories. You can do 1 or several directories.

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

Upvotes: 1

Related Questions