.htaccess wrong redirection - php

I force http to https redirection in my .htaccess and it works just fine. But I added RewriteCond %{REQUEST_URI} !^/robots.txt$ rule, not to redirect http://example.com/robots.txt anywhere and just serve the robots.txt file.
The problem is, it does not work and http://example.com/robots.txt gets somehow redirected to http://example.com/index.php url.
My .htaccess is as following:
RewriteEngine On
# HTTPS redirect
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.+)$ /index.php [L,QSA]
Any suggestions? Thank you.

It redirects because last rule change REQUEST_URI to /index.php. You should be using THE_REQUEST variable in first rule as that doesn't get overwritten after application of rules.
RewriteEngine On
# HTTPS redirect
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{THE_REQUEST} !/robots\.txt [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Also make sure to test this change in a new browser or test it after completely clearing your browser cache.

Related

Remove .html at the end of URL whilst rerouteing to HTTPS

I need to redirect my website from http to https in order to use the SSL certificate, but I also want to remove .html at the end of URL. I can't seem to get it to work.
This is my code:
# Redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove .html from URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
The following code disables the SSL and doesn't reroute to HTTPS.
This should work:
# Redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove .html from URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html /$1 [L,R=301,NC]
Make sure the browser cache is cleared before each test.
The problem is with line RewriteCond %{REQUEST_FILENAME} !-f because the file does exist and the rewrite rule is not processed. So I removed it (Commented) along with next line RewriteCond %{REQUEST_FILENAME} !-d that should be left if there is no directory inside (.*) and your tests go right. I did not test it with that RewriteCond, though. But I did with this code and works fine.
#remove html file extension-e.g. https://example.com/file.html will become https://example.com/file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

Move to https://www, when it is www or http:// or without www and http://?

I am using Zend framework where i have nice looking url controllers. Following .htaccess is working but its making SEO to see us as four links for one page.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
I need to do following fix using htaccess:
www.stackoverflow.com/nice-looking-url =
https://www.stackoverflow.com/nice-looking-url
stackoverflow.com/nice-looking-url =
https://www.stackoverflow.com/nice-looking-url
http://www.stackoverflow.com/nice-looking-url =
https://www.stackoverflow.com/nice-looking-url
http://stackoverflow.com/nice-looking-url =
https://www.stackoverflow.com/nice-looking-url
How to do it correctly using htaccess? so that above input urls are safely landing with https://www. in front always?
Have your full .htaccess like this:
RewriteEngine On
## add www and turn on https in same rule - main domain
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
## turn on https in same rule - sub domain
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Make sure to use a new browser to test your changes to avoid old browser cache.

How to redirect all to ssl and to index.php but not ssl but redirect all to index.php on a specific page

I want all to force redirect to https but not on a specific page currently i have this .htaccess file:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
I want all to redirect to https but force http on a specific page:
/exchage
What code do I need to put on my .htaccess file to achieve this function? Thanks.
What about adding another condition to the HTTPS redirect to check that the path doesn't start with exchange? (I assume you misspelled exchage)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/exchange
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} ^/exchange
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
Once you have updated your .htaccess file, please restart your browser. Otherwise it might force https if it has previously seen the URL use https.
I solve the problem with this code.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/exchange
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /exchange
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
took me almost 3 hours just to come out with this code.

Redirecting codeigniter project to https with htaccess: never complete

I want to redirect a Codeigniter 3 site from http to https.
This is my .htaccess file:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(index\.php|resources|robots\.txt|static) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?$1 [L,QSA]
I get this error:
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Try these .htaccess codes
Method 01
# force SSL
RewriteCond %{HTTP_HOST} \.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Method 02
# Enforce SSL https://www.
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

htaccess RewriteRule cause loop

please, could you give me an advice, why this .htaccess file cause redirect loop?
I have this url:
www.akomin.cz/ or www.akomin.cz/smth/
and I want to redirect it there:
www.akomin.cz/nedostupny/
But Safari throws me an error, that while trying to open www.akomin.cz/nedostupny/ there were many redirects.
RewriteEngine on
RewriteRule !^/nedostupny/ /nedostupny/ [L,R=302]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Change your first rule to this:
RewriteCond %{THE_REQUEST} !/nedostupny/ [NC]
RewriteRule ^ /nedostupny/ [L,R=302]
You need to use %{THE_REQUEST} because your last rule is changing REQUEST_URI to /index.php hence causing first rule to fire again. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
Make sure to test it after clearing your browser cache.

Categories