Redirect subdomain and folder by htaccess

Viewed 97

I want to redirect it.example.com/it to https://example.com/it in .htaccess

How would I do it? I used this

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

but it didn't work.

enter image description here

I use another .htaccess file in it directory. its content is

  RewriteBase /it/
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]]
2 Answers

HTTP_HOST only matches host/domain name. It doesn't match URI.

You may use:

RewriteCond %{HTTP_HOST} ^it\.(example\.com)$ [NC]
RewriteRule ^it(?:/|$) https://%1%{REQUEST_URI} [R=301,L,NE.NC]

You are trying to match URI in host variable, you need to remove your uri part from it. Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^it\.(example\.com)$ [NC]
RewriteRule ^(.*)/?$ http://%1/$1 [R=301,NE,L]
Related