HTACCESS with HTTPS and subdomain with port - .htaccess

I have spent few hours to find a answers for a simple question, but I didn't. With htaccess on apache server, I want:
1-All traffic to be HTTPS
http://example.com >> https://example.com
(edit) AND ALSO
2-All traffic to a subdomain to be redirected to a specific port
subdom.example.com >> subdom.example.com:1234
I have created this piece of crap, but doesn't work in all the cases
RewriteEngine On
RewriteCond %{SERVER_NAME} ^subdom.example.com$
RewriteCond %{SERVER_PORT} !^1234$
RewriteRule ^(.*)$ https://%{SERVER_NAME}:1234%{REQUEST_URI} [L,R=301]
# If not https, then force it
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Finally, nice to have would be to always redirect example.com to www.example.com
Thank you all for your support

Related

htaccess too many redirects

I have .htaccess in the root directory of web which should redirect all requests to https. Here is the code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^web\.php5\.sk$
RewriteRule ^$ www/ [L]
RewriteCond %{HTTP_HOST} ^web\.php5\.sk$
RewriteRule (.*) www/$1 [L]
But it creates an endless loop which redirects from http to https and vice versa. Where could be the problem? First two lines comes from server administrator as response to my request for https. They handles lot of http requests from devices which are not able to make https request... Thanks.
Hi friend What about this code
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
#replace this yourdomain.com into your domain name
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
This is a simple, clean and tested code to redirect all traffic from http to https with www (you can also remove www from your URL anytime, just remove www from url something like this https://yourdomain.com/$1).

Redirecting URL always from www to non-www with SSL/HTTPS

I have a SSL certificate for non www URL: https://example.com
I know there are many similar question, but none of them seems to solve my problem. Here is what I am trying to do:
Problem:
1) http://www.example.com -> https://example.com **www to non www DONE**
2) http://example.com -> https://example.com **http to https DONE**
So far, my .htaccess file looks like so:
# www to non www
# http to https
# SSL off
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^http\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# https://www to https://
Does anyone know the solution? With the current solution, I only get the following error message from the browser:
Corrupted Content Error
The site at http://example.com/ has experienced a network protocol
violation that cannot be repaired.
The page you are trying to view cannot be shown because an error in
the data transmission was detected.
Please contact the website owners to inform them of this problem.
Here are the solutions that I already tried:
.htaccess redirect www to non-www with SSL/HTTPS
htaccess force https and redirect www to non-www, but no other subdomains
MANY MORE
Change your rules to this:
<IfModule mod_rewrite.c>
RewriteEngine on
# https://www and http://www to https://non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://example.com/$1 [R=301,L]
# http://non-www to https://non-www
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com/$1 [R=301,L]
</IfModule>

How do I redirect all incoming requests to https?

Forgive me but I've been looking for answers all day. Trying and testing different things I can't seem to find the answer.
I have SSL for my domain and this is what my .htaccess looks like.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
myexample.com and http://myexample.com redirects perfectly fine to https. Only problem is when I type www.myexample.com it doesn't redirect to https nor does typing http://www.myexample.com
Could any one help me or point me to the right direction?
Your RewriteCond matches only hostnames starting with example.com. Try:
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
Also, www.example.com must be the ServerName or a ServerAlias of your name-based Virtual Host.

htaccess redirect to HTTPS

I've been trying to figure out a way to redirect only one domain to HTTPS but haven't found a good solution. I was able to redirect all requests to https by using the HTTPS!=on condition but I host multiple domains and only one has SSL.
This gave me some success.
RewriteCond %{HTTP_HOST} ^(127\.0\.0\.1|localhost)$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But it doesn't seem to redirect URLs like www.mydomain.com/order/ and mydomain.com/order/
Basically it works only for the main page currently at www.mydomain.com or mydomain.com.
What am I missing?
Your problem is this:
RewriteCond %{HTTP_HOST} ^(127\.0\.0\.1|localhost)$
That says, "Only use the next RewriteRule if the host is 127.0.0.1 or localhost, exactly." If you host mydomain.com on that same server, it won't match. You need to add the name of the domain you want to redirect. Example:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

htaccess https www (only) to https non-www (only)

How can I rewrite all traffic using "https" and "www" to "https" without the "www". I do not want any rewrites done on non-ssl traffic (besides the existing code below). I also already have a rewrite for clean urls going on at the same time. The SSL cert is purchased on the non-www URL which is why I am asking how to do this.
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks for your time.
[EDIT]
Ended up getting this to work:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule ^(.*)$ https://mysite.com/$1 [L,R=301]

Resources