Rewrite any subdomain to https - apache

Currently I am running a little web server with apache on my LAMP system. I already created a https certificate for my domain. Now I would like to rewrite any subdomain and directory of subdomain to https without www. I already have a code in my .htaccess file and it rewrites the www version to non www and to https.
Here's the code in my .htaccess file (located in the root apache directory):
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
I already tried to just exchange the "www" with "(.*)" but I am not sure what "%1" and "$1" does because I found that code in the internet and they didn't explained it. I researched about regular expressions but I can't find anything.

RewriteEngine On
RewriteBase /
# domain starts with www (change yourdomain to your domain name)
# adding the domain name will ensure its not trying to capture a subdomain with www
# For example: ^www\.(.*)$ will redirect www.subdomain.domain.com to https.
RewriteCond %{HTTP_HOST} ^www\.(yourdomain) [NC]
# then redirect to HTTPS
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
# check if HTTPS is not being used
RewriteCond %{HTTPS} off
# and because we already redirect www. we exclude it
RewriteCond %{HTTP_HOST} !^www\. [NC]
# and now we test to see if its a subdomain (change yourdomain to your domain name)
RewriteCond %{HTTP_HOST} ^[^\.]+\.yourdomain [NC]
# now we finally redirect it
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
Once you confirm the subdomain redirection is working, you change R=302 to R=301, the reason behind this, is to avoid your browser from caching the redirection until you can ensure its working.
If you do a mistake while using R=301 the redirection will be cached and results may not be reliable in your browse until it clears up or you use a different browser.
%1 is used when you're capturing something from the RewriteCond whereas $1 is captured from the RewriteRule.
The number represent the captured value index, if you have anything resulting in multiple values, as you can see below with $1 and $2.
When using # for comments ensure they are always in a new line and not at the end of the rule or it will fail to work.

Related

HTTP to HTTPS redirect exposes subdirectory instead of redirecting to subdomain

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.
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.

Redirect only HTTP subdomain to HTTPS subdomain in htaccess

How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This would create a redirect loop (if executed at all). In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop).
These directives would need to go in the .htaccess in the document root of your subdomain. Or at the top of your root (WordPress) .htaccess file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.
This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This checks that HTTPS does not contain "on" and the subdomain is being requested before redirecting to HTTPS on the same host.
Although if WP is simply redirecting the main domain, then you could do all this in .htaccess and simply remove the condition that checks against HTTP_HOST. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example:
RewriteCond %{HTTP_HOST} ^((subdomain|www)\.)?example\.com [NC]
Please try one of the followings:
you need to edit
Redirect "/" "https://yourwebsite.com/"
OR
you dont need to edit following
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]

.htaccess settings for multiple websites with one document root

My Hoster allows multiple Website but just with one DocumentRoot for all domains.
What I want are site with different content:
https://www.site1.com
https://www.site2.com
What I have:
Directory Structure
/html/ <- Document root
/html/site1.com/ <- Symlink to /pages/site1.com/web
/html/site2.com/ <- Symlink to /pages/site2.com/web
How it should work:
Browser: https://www.site1.com/folder/
<- this will internally gets internally redirected to /pages/site1.com/web/folder/
The current .htaccess solution:
RewriteCond %{REQUEST_URI} !^/site1.com/.*$
RewriteCond %{HTTP_HOST} ^site1.com$ [NC]
RewriteRule ^(.*)$ /site1.com/$1 [L,NC]
It IS working, if I visit https://www.site1.com/folder/.
But the problem, what I dont want:
1.) If I visit site1.com/folder (without last slash), the webserver automatically redirects to https://www.site1.com/site1.com/folder/ instead of https://www.site1.com/folder/.
I think this is due to the auto trailing slash feature of directories from Apache (Shared Web Hoster with limited ssh account).
One possible solution would be to redirect 301 all web browser https://site/sitefolder.com/ request uri's to the non /site1.com/ request uri's.
But every htaccess rule i tested failed.
2.) I dont want to make the http://www.site1.com/site1.com & https://www.site1.com/site2.com urls accessable due to duplicated content.
How can I fix this problems and have one document root with multiple domains as subdirectories without duplicated content / redirects to the subfolders INSIDE the url?
This could not be done in 3 lines, so here is what I would do:
RewriteEngine On
# First we check if we have a direct access to our folder and prevent it with a 404 Error
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/?site1\.com(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} ^/?site2\.com(/.*)?$ [NC]
RewriteRule ^.*$ - [R=404,L]
# Optional you can redirect all non-www to www, http or https stay the same
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^/?(.*)$ %{REQUEST_SCHEME}://www.%1/$1 [R=301,L]
# Now we rewriting all request based on the requesting host works with www and non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/?(.*)$ /%1/$1 [L]
The answer assume that the folder is called the same as the host but without leading www., as you have it in your question. So for http://www.example.com the folder have to be example.com.

.htaccess redirect rules to www and htpps but to exclude subdomain

After reading all relevant answers here regarding .htaccess and redirects, and some experimentation with .htaccess rewrite conditions and rules my problem persists.
I managed to force www and https for my Magento site. Here is what I have at the moment:
RewriteCond %{HTTPS} off
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
After creating the subdomain test.example.com and a test environment at public_html/test/ I want to exclude it from the above rules since the subdomain will neither have a www of a https.
I tried to put this exception in the rules above but with no success.
RewriteCond %{HTTP_HOST} !^test\.example\.com$
For example, when I type http://test.example.com/admin/ to enter the Magento admin, it redirects me to https://www.example.com/admin/ Do I have to also edit the public_html/test/.htaccess file ?
Thank you in advance
Yes, this is happening because of your Rules, the 2nd rule checks if the host value doesn't start with www the redirect the host to www.example.com this rule also redirects any non-www http host to the main domain with www.
To fix this, you can use a single rule to redirect http to https:www excluding the subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} !^test\.example\.com$
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

.htaccess Rewrite Engine setting exemption to rule shared SSL

I currently have multiple domains pointing to the same web space ie domain.com domain.co.uk ect...
and i use .htaccess to force all requests to domain.com, which works great however i need to use a shared SSL provided by my host which points to my web space on the server, and i access it using the shared ssl domain. ( http://shared_ssl.com/domain.co.uk ).
The problem i have is .htaccess keep re-writing the domain to http://www.domain.com, when i try an access the shared ssl.
Is there a way to have an exception to the re-write rule?
here is my .htaccess file
RewriteEngine on
# Rewrite domain.org-> domain.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
Don't know if I understand your problem exactly, but this is how would do it:
RewriteEngine on
# Rewrite domain.org-> domain.com
#RewriteCond %{HTTP_HOST} . # <- Not necessary
# (you can have the whole shared_ssl.com if you want, but not necessary)
RewriteCond %{HTTP_HOST} !^shared_ssl [NC]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
# (and this is the preferable way to write it - I think)
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [QSA,R=301,L]