.htaccess Code that Starts Sub-domain without WWW
Zile Kureshi
4th July, 2024
I want my subdomain to be accessed without the "www" prefix. Additionally, I need all "www" links to automatically redirect to the non-www version of the subdomain. For example, if someone visits 'www.subdomain.example.com', they should be redirected to 'subdomain.example.com'.
I understand that this requires some specific code in the .htaccess file, but I'm not entirely sure of the correct syntax and configuration. Could someone please provide the exact .htaccess code needed to achieve this?
Comments
1 Comment
Kamal Bisht
6th July, 2024
The code provided below enables access to a subdomain without the "www" prefix and automatically redirects "www" links to the non-www version.
Canonical URL of the page will be https://subdomain.example.com/
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.subdomain.example\.com [NC]
RewriteRule (.*) https://subdomain.example.com/$1 [L,R=301]
</IfModule>
The code below enables access to the main domain with the "www" prefix and automatically redirects non-www links to the www version.
Canonical URL of the page will be https://www.example.com/
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
© AxBlaze 2024. All Rights Reserved.