htaccess HTTPS redirect excluding HTTPS subdomains - .htaccess

I know it has been asked many times here, but can't find the right configuration for my setup.
Requirements:
http://www.example.com & https://www.example.com (& example.com) have to redirect to https://test.example.com . (Notice it is a subdomain)
https://sub_1.example.com is to be EXCLUDED and not redirected. (Notice the subdomain has an 'underscore')
What I Have:
RewriteEngine On
RewriteBase /
# EXCLUDE following sub-domains.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^sub_1\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
# REDIRECT to
RewriteRule (.*) https://test.example.com/$1 [R=301,L]
>> ISSUE:
My https://sub_1.example.com is in fact an API that feeds a mobile app. When I use the above code, the mobile fails. So it seems that the sub-domain is not properly excluded and protected from the re-direct.
Would appreciate some help in cleaning it up.

FIXED !!
Needed to add an exception before the existing rules, since I needed to exclude https://sub_1.example.com/api
RewriteCond %{HTTP_HOST} ^sub_1\.example\.com [NC]
RewriteRule ^api - [L]

Related

Too many redirects?

I've used the 'Redirect mapper' tool by Varvy.com, and it's highlighted an issue with my www redirect. I'd like there to be only one redirect, but the tool shows two redirects. See screenshot below.
This is what I'm using in my htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
As the screenshot shows, http://www.d3creative.uk 302 redirects to https://www.d3creative.uk/, then 301 redirects to https://d3creative.uk/
I'd like to http://www.d3creative.uk/ to 301 redirect to https://d3creative.uk/, skipping the www redirect.
Any help would be much appreciated!
UPDATE
I use ServerPilot to manage my server, and I've setup SSL using ServerPilot.
I've removed the original RewriteCond and RewriteRule lines from my htaccess file
This clears up the multiple redirects (see screenshot below), but I still need a way to redirect www to non-www
I've contacted ServerPilot support, I'll post back here with any answers.
Fixed!
Within the ServerPilot control panel there is an option to force a HTTPS redirect. This was conflicting with any redirects I was using in my htaccess file.
Turn 'Redirect to HTTPS' off and use the ruleset below in your htaccess
ServerPilot support kindly provided this ruleset, which works great.
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
All versions of my domain now redirect (once) to my preferred version of the domain.

htaccess redirect subdomain to directory

I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.

RewriteRule for HTTP to HTTPS and WWW ISAPI Rewrite

I've trawled many forums and tried many solutions. None work correctly. I am using ISAPI Rewrite 3 for IIS.
I need to change all requests to our website to WWW and HTTPS.
For example:
https://example.com/a-page-here/
http://example.com/a-page-here/
http://www.example.com/a-page-here/
www.example.com/a-page-here/
example.com/a-page-here/
to all change to:
https://www.example.com/a-page-here/
I've used http://htaccess.madewithlove.be, which may be buggy because I'm getting seemingly incorrect results for so-called working solutions. I don't want to be testing umpteen things on the live site.
This supposedly correct example (one of many) I found gives incorrect results:
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !443
# Extract non-www portion of HTTP_HOST
RewriteCond %{HTTP_HOST} ^(www\.)?(.*) [NC]
# Redirect to HTTPS with www
RewriteRule (.*) https://www.%2/$1 [R=301]
Example tests:
example.com/a-page-here/ = https://www./example.com/a-page-here
www.example.com/a-page-here/ = https://www./www.example.com/a-page-here/
Can anyone give me a set of rules that will cleanly and reliably turn any non www request to our website to the correct https://www version, and not add invalid slashes etc?
Try this one:
RewriteEngine On
# non-www to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https\://www.example.com/$1 [R=301]
# HTTP to HTTPS
RewriteCond %{HTTPS} off [NC]
RewriteRule (.*) https\://www.example.com/$1 [R=301]

Redirect non-www to www excluding subdomain

First of all, please don't mark this question as duplicate because I've tried all other answers and no one works for me. I have a subdomain called account.domain.com and this is the code in my .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I want to redirect visits from domain.com to www.domain.com, this works fine. The problem comes when I access the subdomain account.subdomain.com and it redirects to www.account.subdomain.com/account/.php... I don't know why.
I've tested a lot of different codes but I get the same result. My site is hosted in a free account in Hostinger.es.
If you want to target only main domain then use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

Optimize htaccess Wildcard Subdomain Code

I have Wildcard Subdomains working perfectly on my server, but would like to optimize the .htaccess code to make it more portable. I'm not sure how to do it.
I would like to replace the specific domain name and extension (DOMAIN and .com in the code below) with a regex match so that when using the wildcard code for other domains it is easy to port. So it should be a drop in replacement for domain.com and domain2.net. Here's the code I use:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
It's a little different than the usual version you see, because I've added the exception for the www. + subdomain condition. It many cases people enter www.sub.domain.com which breaks on the server. So I check for the www. and remove it before redirecting.
Thanks for your help!
The way you currently have it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://%3.%4/$1?a=%2 [R=301,QSA,L]
If you just want to strip the www from the domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]

Resources