Reputation: 399
I need to:
add www, so user will:
https://example.com/test.html
https://www.example.com/test.html
example.com/test.html
add http -> https redirect, so user will:
http://www.example.com/test.html
https://www.example.com/test.html
example.com/test.html
add subdomain silent redirect, so user will:
https://new.example.com/test.html
https://new.example.com/test.html
example.com/new/test.html
and of course combinated, for example:
http://new.example.com/test.html
https://new.example.com/test.html
example.com/new/test.html
Is this possible via .htaccess file, eventually how?
www and https forwarding is already working fine for me, but I do not know, how to do silent forwaring with subdomains:
RewriteEngine On
# https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^new\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thank you.
Upvotes: 0
Views: 83
Reputation: 785276
You may try these rules in site root .htaccess:
DirectoryIndex index.php
RewriteEngine On
# add www for main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# add https for all domains
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# for new domain silently forward to new/ directory
RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteRule !^new/ new%{REQUEST_URI} [NC,L]
Make sure you don't have any other code except these lines in site root and you must clear browser cache completely.
Upvotes: 1
Reputation: 133538
With your shown attempts/samples, could you please try following. Please make sure to you your htaccess rules in following way and clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ new/index.php?$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/new [NC]
RewriteRule ^(.*)/?$ new/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]
Upvotes: 0