简体   繁体   中英

Redirect all non-www to www except one subdirectory using htaccess.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM