Severe Torture
Severe Torture

Reputation: 319

How can I redirect subdomain in .htaccess

Hi on my webpage I need redirect if user enter via „https://darkzin.cz/“ to „https://www.darkzin.cz/“. For this someone help me with ".htaccess" code:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.darkzin.cz/$1 [L,R=301,NE]

It works, but now i need subdomain (https://pamatnicek.darkzin.cz/). And here is my problem.

How can I solved this?

Upvotes: 1

Views: 33

Answers (1)

anubhava
anubhava

Reputation: 786021

Instead of hardcoding domain name, match it from URL itself using RewriteCond %{HTTP_HOST} and use a back-reference later in rule:

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

%1 is value we are capturing from capture group in RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] and using %1 later in RewriteRule.

Upvotes: 1

Related Questions