Search code examples
.htaccesshttp-redirectmod-rewrite

htaccess redirect to certain subdirectory if none given


I am trying to redirect base URL to certain subdirectory, only if the base URL is requested. E.g. Redirect www.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?


Solution

  • Using mod_rewrite at the top of the root .htaccess file to redirect the root-directory-only to /en:

    RewriteEngine On
    
    RewriteRule ^$ /en [R=302,L]
    

    But note that if /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.