In this post, we will see how to resolve htaccess redirect to certain subdirectory if none given
Question:
I am trying to redirect base URL to certain subdirectory, only if the base URL is requested. E.g. Redirectwww.example.com
to www.example.com/en
. But if the URL already contains a subdirectory (eg. www.example.com/de
), no redirection should take place.How can this be done via
.htaccess
?Best Answer:
Using mod_rewrite at the top of the root.htaccess
file to redirect the root-directory-only to /en
:/en
is a physical directory on the filesystem then you should be redirecting to /en/
(with a trailing slash), not /en
. Otherwise, mod_dir will trigger a second redirect to append the trailing slash.If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com