How to redirect subdomain in .htaccess?

by aniya.jaskolski , in category: Third Party Scripts , 8 months ago

How to redirect subdomain in .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by dalton_moen , 7 months 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.

Related Threads:

How to redirect a subdomain with .htaccess?
How to disallow access to a subdomain using .htaccess?
How to redirect ip to subdomain?
How to redirect subdomain from https to http?
How to force redirect a subdomain to 'https://www.'?
How to redirect subdomain www to non-www on nuxt.js?