How to prevent homepage 301 chains? - apache

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.

Related

Multiple URL redirections happening from non https www to https www

Have a small wordpress blog and was following this guide. One of the step was to change htaccess file for redirects for non-www and www to https www. According to https://varvy.com/tools/redirects/ there should only be one redirect on each type.
But for my no www no http to https www there is 2 redirect happening.
http://myportal.com
301 redirect
https://myportal.com/
https://myportal.com/
301 redirect
https://www.myportal.com/
Many of the tools are taking this as a negative and saying too many redirects. How can I make the 2 step into 1 step? So the result comes to
http://myportal.com
301 redirect
https://www.myportal.com/
Currently have the following code.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can use the following rule to force https and www in a single URL redirection.
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [L,R=301]
Note : Remove the non-www to to www redirect rule If you already have that rule in your htaccess otherwise that might conflict with this one.
Make sure to clear your browser cache before testing this new rule.

how to configure apache mod_rewrite to redirect to non-www version with two exceptions

I'd like to redirect incoming requests to our https:// non-www version of the site using a 301 redirect. It is required to exclude two subdomains specifically (webmin.domain.com and newsletter.domain.com)
This is what I have so far
RewriteEngine on
RewriteCond %{HTTPS_HOST} !^(newsletter|webmin)\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com$1 [L,R=301]
The problem with the above is, that it creates an infinite loop because it seems that the RewriteCond matches the non-www version of the URL.
How can I extend this rewrite rule properly?
Try the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!newsletter|webmin)[^.]+\.domain\.com$ [NC]
RewriteRule ^.*$ https://domain.com/$0 [R=301,L]

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]

Apache .htaccess redirect from https to non https for single URL

Ok, researched this one to death here and on Google. Trying to get an https URL to redirect to a non HTTPS URL.
I have tried all kinds of combos but, nothing is working for this URL:
https://www.urotoday.com/403-perspectives-from-the-editor-in-chief-june-2014
All I want to do is redirect to http://www.urotoday.com/403-perspectives-from-the-editor-in-chief-june-2014
This didn't; work for me:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/403\-perspectives\-from\-the\-editor\-in\-chief\-june\-2014$
RewriteRule .* "http\:\/\/www\.urotoday\.com\/403\-perspectives\-from\-the\-editor\-in\-chief\-june\-2014" [R=301,L]
Place this rule in your DOCUMENT_ROOT htaccess:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^(403-perspectives-from-the-editor-in-chief-june-2014)/?$ http://%{HTTP_HOST}/$1 [R=301,L,NC]
Let me know if this works. :)

.htaccess redirect https://domain1.com to https://domain2.com

We have domain.com and domain.com.au pointing to the same website and currently have the following in our .htaccess
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com.au/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.domain\.com\.au$ [NC]
RewriteRule ^(.*)$ https://www.domain.com.au/$1 [R=301,L]
This is semi working to redirect all requests for the site to the correct secured url.
The only issue is when we go to https://domain.com we are always warned by the browser first that the page may be insecure - if we accept the warning then the page correctly redirects to https://www.domain.com.au.
Is there some way to prevent this warning prior to the redirection of the correct secured domain or should we have the .htaccess redirection written differently?