2
votes

If want to redirect all non-www requests to my site to the www version. All I need to do is add the following code to my .htaccess file.

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

The problem is that when I write for example mydomain.com/products-1 (hidden URL for mydomain.com/products?category=1), all parameters become visible, even though they are specified on the .htaccess file, and I get an output url (after the redirect) of www.mydomain.com/products-1?category=1

How can I fix this? Is there any kind of problems with the .htaccess code above?

1
In your title, you say one subdomain but in your code you have a directory listed? Is this a subdomain issue (sub.example.com) or a directory issue. example.com/sub? - jeffatrackaid
It is a subdirectory issue (example.com/sub). Sorry if I wasn't clear enought. - RenatoB
Is this your complete .htaccess OR your have more rules in there? - anubhava
It has a lot more rules in it. It has over 60 lines. - RenatoB
Yes I suspected that. Move this rule to top of your .htaccess just below RewriteEngine On line. - anubhava

1 Answers

0
votes

Try Changing your RewriteRule:

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

I prefer this because it will catch all *.domain.com. If that is not what you want, then use your original HTTP_HOST rule.

If my logic is working this morning, this rule should rewrite any requests that do not match:

www.example.com and do not contain

/subfolder

to www.domain.com/URI