After several hours of research, I give up!
When I type this: https://scan.example.com
(note HTTPS
) it works fine.
But when I type this in browser: http://scan.example.com
it goes to: https://example.com/scan
(it puts the subdomain at the end)
How do I prevent this and make sure it goes to https://scan.example.com
?
Here is my current .htaccess
(Which works perfectly for me for everything else besides the issue above):
Options -MultiViews
RewriteEngine On
# HTTP to HTTPS canonical redirect
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://example.com/$1 [R=301,L]
# Abort early if the request already maps to (or looks like) a file or directory
RewriteCond %{REQUEST_URI} \.\w{2,4}$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 3. Extensionless URLs for other requests
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
*PS: The reason I have example.com/$1
is because I want to be able to access for example account.php
in root like this:
https://example.com/account
.
CodePudding user response:
I assume your .htaccess
file is located in the root directory of the example.com
host, in which /scan
is a subdirectory (that the subdomain presumably points to)? And this .htaccess
file should apply to both the domain apex and the subdomain?
In which case you'll need to use the REQUEST_URI
server variable instead of the $1
backreference in the substitution string. Since the $1
backreference will contain the /scan
subdirectory when you request the subdomain.
Try the following instead:
# HTTP to HTTPS canonical redirect
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(. ?)\.?$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
Note sure why you have the first condition, unless you are hosting multiple domains?
The above will remove the www
subdomain, but will preserve any other subdomain, including scan
. However, this does not specifically canonicalize www to non-www, since it only redirects HTTP.