Home > Blockchain >  Domain and subdomain .htaccess redirect
Domain and subdomain .htaccess redirect

Time:12-13

I need make two different redirects in .htaccess.

On one website: example.com is created alias user.example.com (same code for two domains).

I need to write two redirects

user.example.com -> user.example.com/login

and second one

example.com -> www.example.com (versions without www to version with www) including subpages (excluding user.example.com - not www version)

all redirects with https protocol

only one redirect works fine, but with the second one together it fails.

RewriteEngine On

## RewriteRule ^user.example.com https://user.example.com/login [R,L]

RewriteCond %{HTTP_HOST} ^user\.example\.com/ [NC]
RewriteRule ^(.*) https://user.example.com/login [L,R]

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,QSA,L]

CodePudding user response:

RewriteCond %{HTTP_HOST} ^user\.example\.com/ [NC]
RewriteRule ^(.*) https://user.example.com/login [L,R]

This will result in a redirect-loop, since it redirects everything (including /login itself) to /login.

If you only want to redirect the document root, then you should change the RewriteRule pattern from ^(.*) to ^$ (an empty URL-path).

The other redirect is "OK".

You should clear your browser cache before testing.

  • Related