Rewrite all traffic to https with port - .htaccess

Got a vServer and plesk installed on it (Apache backend, Nginx reverse proxy), plesk is listening on port 8443.
I use the hostname as a domain for easier accesss and SSL certificate domain. So I would like to redirect ALL traffic to specific one:
What I got so far is this rule
return 301 https://www.DOMAIN.com:8443/;
which leads everything to the Plesk Panel URL but if someone is using this
https://DOMAIN.com:8443
he won't be redirected to
https://www.DOMAIN.com:8443
and the SSL certificate won't work (domain based). How can I force every traffic (http and https with the ports 80 and 8443) to
https://www.DOMAIN.com:8443
Thanks in advance

You could HTACCESS to do it:
The Code
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI}:8443 [R=301,L]
What this does is check if the user is trying to access the site without using www and if so redirect to www on the specific port that you wanted.
If you already have your port 80 traffic redirecting to port 8443 and you just want to redirect the rule for www, what I have listed above will work. You can also have mod rewrite redirect based on the port used.
It would look like this:
The Code
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{SERVER_PORT} ^80$
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI}:8443 [R=301,L]
You would just need to make sure that each virtual host you are using has the mod rewrite rule in place. You might have to create a virtual host for the extra ports you want to use. The only thing these virtual hosts would have is the redirect rule. Otherwise you might get 404 errors.

Related

How to redirect a domain to a different IP if accessed by a specific port?

I was wondering how I could redirect a sub domain I own (such as mydomain.dx.am) to another external IP address it is accessed by a specific port.
in other words, if you type in this domain, it takes you to the hosted website that is directly attached to the domain (the host is AwardSpace). However, if you access this domain through a specific port (such as 25565 through minecraft), it redirects you to a different external IP (XX.XX.XX.XX). I have tried other services (like NO-IP and free ones), but they don't work for SUBDOMAINS that you already OWN and for redirection only on SPECIFIC PORTS.
I don't want all ports of the subdomain to redirect to a diffferent external IP address, just 1 specific port
Is there any online service or specific way of doing this? What could I do to the .htaccess file to create this redirect?
I have access to the files of the site/host, and to the .htaccess (directly on the file or automated through the host). I can't seem to find acccess to the DNS Zone file and changing the nameservers is really glitchy and fails most of the time for an unaquired reason...if there is some way to do this with what I have avaliable then please let me know! Thanks!
Here is my current .htaccess file (has no problems):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This will allow you to redirect based on the incoming port, but assumes that apache is listening on all ports (replace {PORT} with the port you want to have redirected):
RewriteEngine On
RewriteCond %{SERVER_PORT} ^{PORT}$
RewriteRule ^(.*)$ http://www.redirect.com [L, R=301]
Answer partially pulled from .htaccess redirect to error page if port is not 80

mod_rewrite http to https ends up in loop

I've got the problem that I want to redirect my HTTP requests to a HTTPS using a shared SSL certificate. For some stupid reason of my host I have to attach the SSL Port on the request url.
Basically I want to redirect this:
http://sub.domain.com
to
https://sub.domain.com:12345
my .htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://%{HTTP_HOST}:12345%{REQUEST_URI} [L,R]
By this results in a redirect loop. I have no idea why though. Is this maybe because I use a shared SSL certificate which is not made for the domain and therefore the RewriteCond is always off?
thanks in advance!
EDIT:
I've checked the response header and saw that the location field still points to the non https url. But if I use another domain (or even a subdomain) it works. So why doesn't work the redirect to the same domain?
Try changing your condition to:
RewriteCond %{SERVER_PORT} 12345

Limit access to subdirectory via specific SERVER_PORT using htaccess

I've been trying to limit access to a specific subdomain via the server port, e.g. it can only be accessed from subdomain.domain.com:8443 and no other ports.
I'm currently using hostgator for my webhost, and it's already been setup such that subdomain.domain.com points to the correct subdirectory.
Now in the htaccess file, I'm currently trying this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /subdomain
RewriteCond %{SERVER_PORT} !^8443$
RewriteRule ^ - [F]
RewriteCond %{SERVER_PORT} ^8443$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/subdomain/$1 [R=301,L]
As far as the blocking of other ports goes, it seems to work since accessing either subdomain.domain.com or www.domain.com/subdomain, I get a 403 forbidden page. But I can't get it to load the normal content correctly when I do access it via subdomain.domain.com:8443 or www.domain.com:8443/subdomain. Am I doing the rewrite conditions and rules correctly?
Thanks!
The R=301 tells the server to do a redirect, not a (silent) rewrite. So, when you load via 8443, the user is redirected to http://%{HTTP_HOST}/subdomain/$1 without the port 8443 specification and they are then blocked by the first rule. If you do a curl -I on subdomain.domain.com:8443 you should see the 301 redirect code rather than 200.
Remove the R=301 and remove the full domain specification in the final RewriteRule to leave:
RewriteRule ^(.*)$ subdomain/$1 [L]
This should do a silent rewrite of the content.

Redirecting Subdir to HTTPS

I have a site, mydomain.com/secure which I only want to be accessible via HTTPs. This is mapped as a virtual directory like this:
-wwwroot
--secure (Virtual directory, bound on port 443 only) - https://mydomain.com:443/secure
--externalApp (Mapped as a different site on port 80) - http://mydomain.com/
Currently requests to mydomain.com/secure are served, but I want to force them to redirect to HTTPs. Despite trying with rewrite rules I can't get this to work properly. Is it possible or just not doable?
Try this:
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(example|secure|test) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You can add any directory you want in the example|secure|test area.
Example with comments.

HTTPS rewrites .htaccess

I have just had an SSL certificate installed to my site.
It was installed to the subdomain.
I have a rewrite on the sub domain so http://orderonline.mydomain.com.au uses web servers from the same server but in another account. This rewrite work fine.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^orderonline/?$ http://111.67.13.129/~dsoft/themes/RIV00/
However I can't seem it work with the https. The https is directed to the subfolder 'orderoline'.
How do I do a rewrite for the https to point to http://111.67.13.129/~dsoft/themes/RIV00/ but still maintain https//orderonline.mydomain.com.au.
Thanks in advance
Daniel
I worked it out.
I needed to change
RewriteCond %{SERVER_PORT} 80
to
RewriteCond %{SERVER_PORT} 443
However is it possible to do both?
d

Resources