.htaccess mod_rewrite regex for https server port - regex

My website uses an external authentication service, which directs the user away from the desired page, performs the login, and then redirect back.
The trouble is when a user is redirected from an https page, the authentication service (which I cannot modify) stupidly redirects back to http:// with a :443 appended to the end of the URL, which just throws an error from Apache.
I already have an htaccess directive to ensure that the user is always viewing the current page on https:
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
What I'm looking for is a rewrite rule that will detect a URL starting with http, and ending in :443, and then have it redirect to https:// without the port being appended.
I had tried this:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
But that doesn't appear to work.

Replace your .htaccess code with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This rule will redirect all http traffic to https irrespective of any port being used with http requests.

Related

apache rewrite rules do not work properly when url ends with slash

I wanted to redirect urls like example.com to https://www.example.com. I tried the following apache rewrite rules, but it did not work properly.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^/?(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^/?(.*)$ https://www.example.com/$1 [R=301,L]
When I entered example.com in the web browser, it correctly redirected to https://www.example.com, but if I entered example.com/, i.e. with a trailing slash, it partially redirected to https://example.com without preceding www.
So how to configure the rewrite rules?

.htaccess redirect www to non-www not working

I pretty much know nothing about about .htaccess editing or regex expressions. What I have done to try to make my site re-direct from www versions to non-www versions is I followed the answers posted here:
redirect www to non-www
However, what I get is that if I enter www.example.com, I get:
example.com/example.com/ and a 404 error on my site
This is my .htaccess file:
#Adjust default time zone
SetEnv TZ America/Toronto
#Begin redirect www to non-www
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
# Google Analytics Integration - Added by cPanel.
<IfModule mod_substitute.c>
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|(<script src='/google_analytics_auto.js'></script>)?</head>|<script src='/google_analytics_auto.js'></script></head>|i"
</IfModule>
# END Google Analytics Integration
Can someone enlighten me on what I am doing wrong?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
That is, check if the HTTP_HOST contains www and if so, redirect to non-www with the request_uri appended.

.htaccess redirect to new domain with path intact for both www and non-www as well as https and non https

Essentially I need an .htaccess file that will redirect all traffic to our new domain. It need to work in the following conditions:
http://www.olddomain.com/path/file.php => http://www.newdomain.com/path/file.php
https://www.olddomain.com/path/file.php => http://www.newdomain.com/path/file.php
(note in the above case the https redirect to http - this is not an issue)
Also:
http://olddomain.com/path/file.php => http://newdomain.com/path/file.php
https://olddomain.com/path/file.php => http://newdomain.com/path/file.php
I've almost got it working by first redirecting the https version of www.olddomin.com to http version of www.olddomain.com which then redirects to the http version of the new domain, the problem I have is with the non-www version of https://olddomain.com which redirects to http://olddomain.com and then stops.
The code I am using is:
RewriteEngine On
RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This almost works except that https://olddomain.com/path/file.php just redirects to http://olddomain/path/file.php and stops and doesn't get redirected to http://newdomain.com/path/file.php
Any help would be appreciated.
You just need this single rule in the DOCUMENT_ROOT/.htaccess file of olddomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://%1newdomain.com%{REQUEST_URI} [R=301,L,NE]
Explanation:
NC - ignore case
L - Last
R=301 - Send 301 status to browser
NE - no escaping
%1 - is the value we capture in first (...) in RewriteCond. That will be either www or blank

not able to redirect HTTP to HTTPS using rewrite in htaccess

I have tried url redirection of HTTP to HTTPS on my website by using several different solutions in .htaccess and none have worked.
Well, actually it does work if I type in directly:
(only for example) http://mywebsite.com
into the browser address bar....but if I try to redirect the url any other way, it won't work. If I try the link to my page listed on Google http://mywebsite.com/ it will not redirect to https://www.mywebsite.com. I need it to redirect from HTTP to HTTPS if someone clicks the link to my page from a search engine.
So far I have tried:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
then I tried...
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
and then I tried...
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R,L]
None worked. What else should I try? (this website is being hosted on a web hosting service site that has uses an apache web server). Also, I have an SSL certificate for the page, if that makes any difference.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Tip: Clear your browser cache or use a different browser to test.

Redirect all www to non-www including subdomains

I have two separate WordPress sites on the same domain, one installed in the following way:
http://subdomain1.domain.tld and http://subdomain2.domain.tld.
http://domain.tld redirects to subdomain1.
I need to remove all www. to non-www (this now works as tested on http://htaccess.madewithlove.be/), www.domain.com now returns domain.com but i am still getting a 404 page returned when i type in a subdomain prepended with www.
My current code, with the suggestions by #icabod now looks like this.
# 301 redirect www to non-www url
# including subdomains
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R]
</IfModule>
mod_rewrite is working with other redirections, I do not want to use a PHP redirection. I have no access to my registrar's CNames so I cannot redirect them through there. I have placed a .htaccess file in which I have the below code in the root folder and have tried placing it in the subdomains root folder as well.
This is driving me bananas !
Thank you.
Don't forget that you can use a part of the RewriteCond expression in the rule, just like you can match a part of the RewriteRule expression:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R]
The %1 matches the part in braces in the RewriteCond, and the $1 matches the part in the RewriteRule. This rewrite does make the assumption that you are only using http on the standard port 80, but will basically remove any www. from the beginning of the requested URI. From that point, redirecting domain.tld to subdomain1.domain.tld should be straightforward:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://subdomain1.example.com/$1 [R]
As an aside, you probably shouldn't be using [R=301] until you have the rewrite working correctly, as the 301 means it's a permanent rewrite, but during testing it is prone to change.