HTTP/1.1 302 Found redirect loop - php

I'm having a problem with our site, in homepage it is working appropriately but when I visit the inside pages it returns 302 Found and keeps on redirecting, I used the logs in inspector element. In chrome it is working, but in ie11 and firefox it keeps on redirecting.
Here is my htaccess:
RewriteEngine On
#
# Full path to your site
#
# RewriteBase /
#
# Rules
#
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#RewriteCond %{HTTPS} on
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?do=$1
ErrorDocument 404 /error
you can check this link to see what im talking about. http://mst.com.ph and then try: http://mst.com.ph/category/motoring/

After inspecting your page, /motoring/category is send with a 302 Moved Temporarily header. The location is however empty. I am assuming that the php page you are trying to load has a header( ... ) in it, and that the variable that contains the new location is empty or NULL. Besides that, the request is not terminated with an exit(), so the content is shown anyway. Chrome decides to ignore the status code and display whatever you sent as content after it, and other browsers apparently decide to redirect to the same page. In any case, it doesn't make sense to serve the page with a 302 status code.
This is not a problem with your .htaccess. In your script, make sure that variables contain what you believe they should contain. (var_dump(..) them for example). Also add an exit() behind headers that define a redirect.

Related

How to redirect any input to both https and www?

I need example.com be redirected to https://www.example.com.
My .htaccess file is in the root directory and configured like this:
RewriteEngine On
DirectoryIndex html/home5.html
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
With this configuration, www.example.com, https://example.com and http://example.com are all redirected to https://www.example.com (www.example.com/other.html and so on work respectively) correctly.
But example.com gets redirected to https://www.example.com/http://www.example.com/ and obviously produces "The requested URL was not found on this server".
Why does this happen? What do I need to change to make it work?
I also donĀ“t understand why with my current configuration https://example.com the www is added correctly.
Got a well working .htacces i'm using for the same problem, with 2 steps :
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
First, if the requested url got HTTPS "off", redirect to HTTPS and add the url request query after (saved in $1).
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%1/$1 [L]
Then, special case for www. subdomain and https on, if matching "www" in the request, redirect to the same page with https on !
EDIT : you don't even need the second condition in fact
Redirect to https and non-www
To instead redirect all requests to https and non-www, use the following code instead of the previous:
Canonical HTTPS/non-WWW
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/$1 [L,R=301]
</IfModule>
As before, place this code in the root .htaccess of your site. Here is what it's doing:
Checks if mod_rewrite is available
Checks if HTTPS is off, or if the request includes www
If either condition matches, the request qualifies and is redirected to the https/non-www address
When placed in the root .htaccess, this technique covers all requests, providing complete https/non-www canonicalization for your site. Remember to replace the two instances of example.com with your own domain name.
Note: if your site is suffering from duplicate pages because of index.php appended to requested URLs, check out this post at WP-Mix.com that explains how to remove www and index.php from the URL.
Redirect to https and www
The following .htaccess technique redirects qualified requests to the https and www versions of your web pages. Add to your site's root .htaccess file:
Canonical https/www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
This code does the following:
The first two lines conditionally redirect to https. If the HTTPS variable is set to off, then the request is redirected to https (see notes below if using a proxy).
The second two lines redirect to www. If the request/host does not begin with www., the request is redirected to www.
When placed in the root .htaccess, this technique covers all requests, providing complete https/www canonicalization for your site. No editing is required with this code; it's entirely plug-n-play.
Notes if using a proxy
As explained here:
"When behind some forms of proxying, whereby the client is connecting via HTTPS to a proxy, load balancer, Passenger application, etc., the %{HTTPS} variable may never be on and cause a rewrite loop. This is because your application is actually receiving plain HTTP traffic even though the client and the proxy/load balancer are using HTTPS. In these cases, check the X-Forwarded-Proto header instead of the %{HTTPS} variable."
So if you are using some sort of proxy service or similar, add the following line to the above code:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
So the final result looks like this if using a proxy server:
Canonical https/www (when using proxy)
<IfModule mod_rewrite.c>
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
reference

How to avoid the redirection to index.php when trying to access non www urls

I have a Laravel website in production and here is my problem explained with 4 simple url tests:
Access: https://www.my-website.kr/
Result: https://www.my-website.kr/ (all good)
Access: https://my-website.kr/ (home page again but without www
Result: https://www.my-website.kr/ (all good, redirects my non www
urls to www)
Access: https://www.my-website.kr/subpage
Result: https://www.my-website.kr/subpage (all good)
Access: https://my-website.kr/subpage
Result: https://www.my-website.kr/index.php (not good, I don't want this index.php)
This last test it the one I cannot fix and it is quite annoying. When I access a subpage without www; it's okay if the only solution is to get a redirection to the home page again but at least without the index.php this is terrible for the SEO.
I know these questions about htaccess have been answered many times but I am loosing hope... Even the technical support of my dedicated server couldn't answer me properly.
I have two htaccess files at the moment; one located directly at the root of my public_html/ with the following content:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.kr [NC]
RewriteRule ^(.*)$ https://www.domain.co.kr/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^domain\.co\.kr [NC]
RewriteRule ^(.*)$ https://www.domain.co.kr/$1 [L,R=301,NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
And another one under public_html/public with the default Laravel's htaccess content:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
You might ask me why have I two htaccess at two different location. And I wonder the same. The thing is I have done many test and I discovered that putting the www / https rules directly at the root folder kind of worked better. But I might be wrong.
Again sorry if this question has been asked a million time but I couldn't find the answer that would work for me.
Thanks in advance for any bit of help.
The .htaccess file taking care of all the redirects (www or not, https or not) is over-engineered.
It seems like the final domain should always be www.domain.co.kr + SSL so there's no need to use %{HTTP_HOST} as the final domain is not dynamic, or that %{HTTPS}s ^on(s) match to extract s or not.
The order is correct tho: always do all the nitty gritty redirects/http(s) before the framework rewrites.
Because you do that in the parent folder, it's OK. You could put those rules in the same .htaccess file too, but you'd have to put them before the Laravel ones.
I would start with simplifying it:
RewriteEngine On
# Redirect http to https
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,QSA]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.domain.co.kr/$1 [R=301,L,QSA]
You'll notice I also added the QSA flag to pass any query string you might have in the original request.
See how you go from there.
Also make sure you always empty your browser cache every time you try something new as redirects are cached.
If it's still not working and you can change the log level on your apache config, check this: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging
You'll be able to debug what happens during redirects/rewrites, step by step, to pin point where the actual issue is.

Htaccess conditional https for static pages

My site is HTTPS enabled and all the pages are served using HTTPS only. Client now has the requirement where he wants to show static pages like about-us, termsofus as HTTP pages and not as HTTPS. This means that even if the user tries to open about-us page as HTTPS it should redirect to HTTP version of about-us.
My .htaccess code is as follows:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^\/about-us
RewriteCond %{REQUEST_URI} !^\/termsofus
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} \/about-us [OR]
RewriteCond %{REQUEST_URI} \/termsofus
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
Problem: Whenever I open HTTP/HTTPS version of about-us page it keeps on redirecting me to index.php.
For example: https://example.com/about-us to https://example.com/index.php
The site uses PHP YII framework.
Use THE_REQUEST variable instead of REQUEST_URI. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
Options -Indexes
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+(about-us|termsofus) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+(about-us|termsofus) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Make sure to clear your browser cache before testing this change.
anubhava's answer already addresses the problem and provides a good solution. I thought I'd just provide some additional explanation as to what was happening with your stated example with reference to your original code:
For example: https://example.com/about-us to https://example.com/index.php
Given a request for https://example.com/about-us:
This matches your second rule (HTTPS is "on" and about-us is requested) and redirects to http://example.com/about-us (ie. back to HTTP).
The redirected request (ie. http://example.com/about-us) now matches the last rule and gets internally rewritten to index.php (the front-controller).
However, in per-directory .htaccess files (directory context) "the rewritten request is handed back to the URL parsing engine" and the process effectively starts over. The REQUEST_URI server variable is also updated to hold the rewritten URL ie. /index.php.
On the second pass through the .htaccess file the request (now rewritten to http://example.com/index.php) matches your first rule (HTTPS is "off" and the request is not /about-us or /termsofus) so the request is redirected (a second time) to https://example.com/index.php. (The internal rewrite is effectively changed into an external redirect.)
The redirected request (now https://example.com/index.php) does not match any rules in your .htaccess file, so passes through unchanged. Page is served.
If you check the network traffic, you should see the two external redirects mentioned above.
Another possible solution is to use the END flag (Apache 2.4+ only) on the last RewriteRule. This effectively ends the URL rewriting process, so the process stops at step #2. Although I would still favour anubhava's solution and check against THE_REQUEST instead, which works on Apache 2.2 and will still work should you introduce additional rewrites.

www to non www redirecting issue

I am using .htaccess to redirect from www.test123.com to non http://test123.com site.
Because it cause the cross domain issue in Ajax.
I am not able to use www.test123.com in Ajax(returns 500 error).
So, i used this code in .htaccess file
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
And all thing is working great.
Now issue is, i have unity app which calling php file like this
http://www.test123.com/save.php
And posting some variables with this URL also.
so i am not able to get that post data because this will redirect from www.test123.com to http://test123.com. So i lost post data.
What should i do to get my post data?
You should ignore POST method from your redirect rule as POST data does get lost while doing a full redirect.
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]

All my .htaccess redirects are going to index.php

I am trying to redirect all my website traffic from any url to https://, secure ssl using a .htaccess file. This has to match the current domain and redirect including any sub url's.
My code:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^shop.test.com$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The issue i am having is that all traffic is being sent to index.php
shop.test.com/testurl
goes to - https://shop.test.com/index.php
expected - https://shop.test.com/testurl
This is most likely due to another rule that writes everything to index.php and uses that as front controller.
Make sure you place above rule as your very first rule just below RewriteEngine On line.

Categories