Howto change the port in an URL using .htaccess

6,060

This was answered over at Stack Overflow:

https://stackoverflow.com/questions/23027847/htaccess-specific-url-to-different-port

I'm going to modify the answer to fit your situation. It says "CONDITION: If not 443 then rewrite to https. This should strip out that port data. Your previous condition was protocol based and not port based.

    # redirect to 443 if current port is not 443 
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://example.com/$1 [R=301,L]

WARNING: I would not rely on these rules if you are trying to force https. I would turn off port 80 altogether. That way if they get around your rules they will see a 404 error.

Share:
6,060

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin almost 2 years

    Using .htaccess, I need to change any URL of this kind https://example.com:80/webtrees/anything_else by exactly the same, but with 443 instead of 80

    Note that I already have this in my .htaccess (it is there to change from http to https, it should stay there, and I do not understand this syntax): RewriteEngine on RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule (.*) https://example.com/$1 [R=301,L]

    Thanks!

    • Admin
      Admin almost 7 years
      Port 443 is used by HTTPS connection
    • Admin
      Admin almost 7 years
      Just stop serving content on port 80. You can't rely on a redirect. Then they either get the redirected page or nothing.