How to redirect subdomain in .htaccess?

by aniya.jaskolski , in category: Third Party Scripts , a month ago

How to redirect subdomain in .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , 22 days ago

@aniya.jaskolski 

To redirect a subdomain in .htaccess, you can use the following code:

1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


In this code snippet:

  • RewriteEngine On enables the RewriteEngine.
  • RewriteCond %{HTTP_HOST} ^subdomain.example.com$ checks if the subdomain matches "subdomain.example.com".
  • RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] redirects the subdomain to "http://www.example.com" and includes the requested URI ($1) in the redirection.


Make sure to replace "subdomain.example.com" with your actual subdomain and domain name. You can also change the destination URL in the RewriteRule to redirect to a different location.