To redirect your WordPress website to SSL (HTTPS) using the .htaccess file and to include RewriteCond for server port, you can use the following code:
RewriteEngine On
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# RewriteCond for server port (example: redirect from port 80 to 443)
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Make sure to place this code in the .htaccess file in the root directory of your WordPress installation.
Here’s what the code does:
- The first block redirects all HTTP requests to HTTPS. If the request is made over HTTP (port 80), it redirects to the same URL using HTTPS (port 443).
- The second block removes the ‘www’ from the URL. If the request includes ‘www’ in the domain, it redirects to the same URL without ‘www’.
- The third block is an additional condition using RewriteCond for the server port. In this example, it redirects requests made on port 80 to HTTPS (port 443). Adjust the server port numbers as per your specific requirements.
Please note that before making any changes to your .htaccess file, it’s always a good practice to create a backup of the original file. Additionally, if you have any existing code in your .htaccess file, make sure to place this code in the appropriate location, typically after the RewriteEngine On
line.