.htaccess and 302 vs 301 redirects - .htaccess

I have a site on a managed server which for historical reasons carries versions in subfolders which are used for development, testing and production respectively. The different versions are managed through forks of a git repo.
I need to:
ensure that all requests are https
all requests to the main URL are referred to the production site
all requests to pages which exist are honoured
I have an .htaccess file in the root which works perfectly but for one problem. If someone visits the site requesting
domain.co.uk www.domain.co.uk or http://www.domain.co.uk
They are redirected correctly to
https://www.domain.com/production/index.php
but with a 302 header
If someone visits the site requesting
https://www.domain.com/
They are again redirected correctly, but this time with a 301 permanent redirect header.
For SEO purposes I want to be able to get a 301 header for every redirect.
Here is my .htaccess content:
RewriteEngine on
Options +FollowSymLinks
# check for no www on url
RewriteCond %{HTTP_HOST} ^domain.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.domain.co.uk/$1 [L,R=301]
# check protocol
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# send everything to production
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk$ [NC]
RewriteRule ^/?$ /production/ [R=301,L,NE]
If I had access to the server, I suspect I might find this easier, but this site is hosted on a shared server running lightspeed.
At a real loss on this one - I'm a pretty good programmer, but .htaccess is not my forte!
Thanks in advance!

Related

.htaccess specific code to redirect http traffic to https and redirect subdomain traffic to offsite domain

I have recently set up a site "saskatoonwashworld.com" with SSL. I have two issues that I need to resolve...
1) Route all http requests to https (I was able to do this easy enough with code found online)
2) I also have a subdomain "portal.saskatoonwashworld.com" which I want redirected to "https://secure3.washcard.com/AP?CID=65e53149-59e9-4d67-a746-e475aa4bc7be" which is hosted by another site. I want the requests to go here whether or not the user types in http://portal.###, https://portal.###, or just the url without the http(s).
I cannot figure out how to do it as I don't know how to properly code the conditions and rewrites.
I was originally using this code found online for the http to https redirect...but if I'm understanding it correctly, it is using a wildcard to catch "www." and so would ignore/override my "portal." subdomain anyways. But I could be wrong.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://saskatoonwashworld.com/$1 [R=301,L]
Try this rewrites in .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?portal\.saskatoonwashworld\.com [NC]
RewriteRule (.*) https://secure3.washcard.com/AP?CID=65e53149-59e9-4d67-a746-e475aa4bc7be [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://saskatoonwashworld.com/$1 [L,R=301]

Redirect every page on example.com to example.org

I'm wanting every page of www.example.com to redirect to www.example.org
I'm using the following .htaccess in the root of example.com
RewriteEngine on
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
if I navigate to www.example.com, I'm redirected to www.example.org - that's great
If I navigate to www.example.com/example.php I get a 404 from example.com
Edit: Cloudflare was caching files, the accepted answer as well as the method in the question work to redirect files to the appropriate page. If using Cloudflare, turn OFF caching by turning on development mode when in development
Check this 1:1 rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule (.*) http://www.example.org/$1 [R=301,L]

HTTP to HTTPS 301 Redirection Code is Not Working, It says Too many redirects

I am using Bluehost for one of my websites. Recently, Recently, i have moved my site from HTTP to HTTPS .
After that, I have used So may different code including the below code to force HTTPS all over my website.
# SSL Rewrite
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
After using this post, when I check redirection on redirect-checker available on online, I get following messages.
But it is not working. Too many redirects. Please try to reduce your
number of redirects for http://www.example.com. You use 19 Redirects.
Ideally, you should not use more than 3 Redirects in a redirect chain.
More than three redirections will produce unnecessary load on your
server and reduce speed, which ends up in bad user experience.
And If you want to access my website from a browser, I get warning as such:
The www.example.com page isn’t working. www.example.com redirected you
too many times.
I think some redirection loop was creating such problem.
However,
My web site Status Right Now: Without using any code on htaccess
https//www.example.com is working just fine. returning 200 OK. 200 OK2
https//example.com is now 301 redirected to https//www.example.com (So, i think it is also just fine)
The problem is:
http//example.com is now 301 redirected to http//www.example.com
http//www. example.com is returning 200 OK.
To solve problem 3, I included Below code on my htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
After that I found that:
http//example.com is now 301 redirected to https//www.example.com
So the problem is solved for:
http//example.com
https//example.com
https//www.example.com
Now I just need some code to redirect 301 only:
http//www.example.com TO https//www.example.com`
Can anybody help me here?
And I am sorry if I explain my problem in a wired way... :D. The matter is I am not an expert guy on this. Hope you all will understand.
Try this:
RewriteEngine On
# If not www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# rewrite to https and www
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
# If not HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

.htacces https:// to http:// redirect not working

I have a problem. I need temperarly to redirect my stores to http again.
the non ssl and non ww url are working fine but if a https:// is in front of the url it is not redirecting to http://
This is my .htaccess redirect
RewriteCond %{HTTPS} on
# RewriteRule ^(.*) http://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [R=302]
#RewriteRule ^(.*) http://%{HTTP_HOST}%{REQUEST_URI}[R=302,L]
I tried 3 different rules but can't seem to get it redirected. Thus my google serp is hetting the warning page. Bad for business.
Non ssl http://tinyurl.com/p4zmmtx
SSL http://tinyurl.com/pteglog
I use opencart 154. All settings are ok. All static urls are ok in the script. I really don't know how to solve or what i'm doing wrong. Hope someone has a hiont or solution..

How can I redirect HTTPS to HTTP in .htaccess for specific domains?

We host a number of websites on our server. Our main site, let's call www.domain.com.
Anyway, we noticed that the other sites we host, etc, all point back to our main site (domain.com) when https:// is put in front of their domain. Part of the problem is that these are indexing in Google as well.
I've wondering how I can redirect all these in the htaccess - please help!
Add these rules to the htaccess file in your www.domain.com document root, preferably above any other routing rules that may be there:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R]

Resources