Multiple Domain Variations Requiring Apache Site-wide 301 Redirects - apache

So I have a website which is currently running under 4 variations:
http://www.example.com / https://www.example.com
&
http://example.com / https://example.com
I am working on a solution to have all variations redirecting to:
https://www.example.com
I have read lots about how to use an Apache domain 301 redirect to solve the http to https (there seems to be lots of formulas), however I am wondering if there is a more efficient code to have all these variations 301 redirecting to the https://www. version?
Any help would be greatly appreciated as I am a complete noob when it comes to server-side htaccess language etc.
Thanks!

You can use a single 301 redirect rule to get both tasks done:
RewriteEngine On
# add www and http -> https
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

To redirect to https://www , you can use :
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]

Related

force redirection from https://example.com to https://www.example.com

I know this is a question that is asked several time. I tried several rules but redirection of https://example.com to https://www.example.com is not working.
My current redirection rule in the Apache vHost of non SSL is pasted below
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE
The above rule works fine for http://example.com and http://www.example.com
I find it. This needs to be added in the ssl vhost file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
My current redirection rule in the apache vHost of non SSL is pasted below
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE
You already have a solution, but as you have found, the above redirect will obviously only apply to HTTP requests when in the "vhost of non ssl". In this case, the HTTPS server variable is always "off" - so the first RewriteCond directive is entirely redundant.
However, you don't need mod_rewrite at all when redirecting from HTTP to HTTPS in the HTTP-virtualhost. A simple mod_alias Redirect will do the job much "better":
Redirect 301 / https://www.example.com/

How to prevent homepage 301 chains?

I'm trying to make all versions of homepage URLs 301 to same place, without a 301.
It's difficult to show the problem because I don't have enough rep points to post image or show the http response codes in a chart (too many links!)
But, using the code below https://www.example.com/ goes to http://www.example.com/ first, before it 301s to the homepage URL http://example.com
I'm on apache, and I've been using .htaccess to try to resolve this.
I've tried also tried the following, but it only works for file paths.
Redirect 301 /oldfile.htm /newfile.htm
# Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example/$1 [L,R=301]
I've tried also tried the following, but it only works for file paths.
Redirect 301 /oldfile.htm /newfile.htm
Is there a way I can make https://www.example.com/ go to http://example.com without the 301 chain?
Thanks,
Mike.
You can use a single rule to redirect your https URLs to http and non-www version. This will redirect all of your https urls to http without creating multiple redirect chain.
Replace your htaccess rules with this :
# Redirect HTTPS to HTTP and non-www
RewriteCond %{HTTP:X-Forwarded-Proto} =https [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [L,R=301]
Make sure to clear your browser cache or use a different web browser to test this rule.

Redirect permanently all possible domains and subdomains to https without www (https://example.com)

I have a site that is getting a lot of traffic so performance is critical. I'm using apache and ubuntu.
I'm trying to redirect all possible domain possibilities to https://example.com including all subdomains (https://test.example.com).
Here is what I have currently:
RewriteEngine On
# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
# match urls that are non https (without the www)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I feel like the above code is doing extra rerouting/work. Is there a more straight forward solution (for a faster performance) either in the virtual host file or .htaccess?
Thanks!
You can combine all three conditions
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
Explanation: if not https OR not example.com then redirect to https://example.com/xxx
To answer your question about performance: rewriting in .htaccess may be slower than rewriting in Apache config file.

Can't Force HTTPS and Non-WWW via htaccess

I am trying to force any regular http request to redirect to https, and also force non www in the URLs, so that every request in the end will be https://example.com
http://example.com becomes https://example.com
http://www.example.com becomes https://example.com
https://www.example.com becomes https://example.com
http://example.com becomes https://example.com
I have the following code in my htaccess file.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
But I am getting an error that states it has too many redirects. It seems to work fine on this test site with the correct output:
htaaccess tester preview
Any ideas or a better approach to this?
The following code should help you:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
It will redirect everything to https://example.com

Apache redirect all to www https

I am currently using the following in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
..however I also want all requests to be redirected to https://www.example.com. As it stands any requests for http://example.com or https://example.com go to https://example.com. I have tried various methods in redirecting however seem to be only able to get all requests to http www or all requests to https, I can't figure it to get both.
Also any rule needs to include the original filename requested e.g a request for http://example.com/contact.php should be redirected to https://www.example.com/contact.php.
Any help or pointers would be much appreciated. Thanks!
You can use that:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.exemple.com%{REQUEST_URI} [NE,R=301,L]