htaccess redirect + hide subfolder in url
17,717
Try this rule:
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule !^subfolder/ /subfolder%{REQUEST_URI} [L]
Author by
Zerreth
Updated on June 27, 2022Comments
-
Zerreth almost 2 years
I've looked around and have attempted some rules and conditions that worked; while some did not work or the condition would capture too much causing other domains to also redirect. This works for me:
# Redirect (also catches www.) RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ RewriteRule ^(/)?$ subfolder [L]
resulting in if you visit www.domain.com OR domain.com being redirected to domain.com/subfolder Great, but I'dd also like the /subfolder part to be hidden from the url.
How do I achieve this ?
Note that I have multiple domains, each should redirect to its own folder. I also want each page to still be visible in its respective folder.
So www.domain.com/abc should redirect to domain.com/subfolder/abc but show domain.com/abc
-
Zerreth over 10 yearsThis works ! But I just noticed that the www. part is treated as optional, instead of being removed. While this is perfectly fine, is it possible to let it also always remove the www. ?
-
anubhava over 10 yearsActually we need a separate www removal rule since this is just internal rewrite.
-
Benubird over 8 yearsDon't forget you need
RewriteEngine on
-
yardpenalty.com almost 7 years@Zerreth. You typically want to allow www in the DNS settings by adding a CNAME record and then a simple redirect in .htaccess or http.conf without www
-
Anatol over 6 yearsanubhava can you tell how to exclude several folders like
RewriteRule !^subfolder/ OR !^subfolder2/ OR !^subfolder3/ /subfolder%{REQUEST_URI} [L]
-
anubhava over 6 years@tBook: Use:
RewriteRule !^(subfolder|subfolder1|subfolder2)/ /subfolder%{REQUEST_URI} [L,NC]