How to rewrite a subdomain with .htaccess?

by kyleigh.wolff , in category: SEO , 4 months ago

How to rewrite a subdomain with .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by priscilla.langworth , 4 months ago

@kyleigh.wolff 

To rewrite a subdomain using .htaccess, you can use the following steps:

  1. Confirm that you have enabled the mod_rewrite module in your Apache server. You can do this by checking if the following line is uncommented in your server configuration file or virtual host file: LoadModule rewrite_module modules/mod_rewrite.so
  2. Create or modify the .htaccess file in the root directory of your subdomain.
  3. Add the following code to the .htaccess file:
1
2
3
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?subdomain.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/subfolder/$1 [R=301,L]


Replace "subdomain.example.com" with your actual subdomain, and "www.example.com/subfolder/" with the URL of your desired destination.

  1. Save the .htaccess file and upload it to the root directory of your subdomain.
  2. Test the rewritten subdomain URL in your browser to confirm that it is redirecting correctly.


Note: The above code uses a 301 redirect (permanent redirect) to the specified URL. If you want to use a different redirection method, you can modify the [R=301,L] part accordingly.