.htaccess 301 redirect not working correctly. Need to force HTTPS - apache

We would like to force all requests to our website to use the HTTPS protocol. We just want to replace the protocol of the URL, the rest of the URI can stay the same. Everything works when we start browsing through the website from the homepage. When we open any other page that isn't the homepage first (i.e. ourdomain.com/this-is-a-page/) we don't get redirected to use HTTPS. What do I need to change on my htaccess file to accomplish this?
This works (it inserts https): ourdomain.com
This doesn't work: ourdomain.com/this-is-a-page/
Thanks!
htaccess code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

You need to put your redirect rule before your routing rule. The rewrite engine loops so the routing rule (which routes stuff to index.php) gets executed first, the rewrite engine loops, then the second redirect rule gets applied, which redirects the wrong thing. Try:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Related

htaccess error on redirect - too many redirects

I'm trying to redirect all requests from one domain (domain.co.in) to another domain (domain.info.in). I've tried Rewrite directives and Redirect directive in htaccess, but getting too many redirects error in browser. Below is the configuration I'm trying to implement.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.in [NC]
RewriteRule ^(.*)$ index.php/$1
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
My actual working htaccess configuration is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1
Using this, I'm able to redirect all requests to index.php and working fine. But when I add domain rewrite rules, I'm getting the redirect error. I've tried Redirect directive also,
Redirect 302 / http://domain.info.in/index.php/$1, but same error.
I tried the Fiddler tool mentioned in this post, Tips for debugging .htaccess rewrite rules. There it is working good.
Actually, I want to redirect all requests (www.domain.co.in, domain.co.in, www.domain.info.in) to domain.info.in
Any suggestions on this?
As per #CBroe's suggestion, I've updated the configuration and it works. As he said,
RewriteConds always affect the directly following rule.
And I've also added negation to the checking to redirect all other requests.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.co.in$ [NC]
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [L]

Redirect URL from https://www.oldweb.example/Vidz to https://www.newweb.example/Vidz

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Redirect 301 /Vidz https://www.oldweb.example/Vidz
Redirect 301 /Vidz https://www.oldweb.example/Helium
Above is my code on. htaccess, and it is not working according to what it needed to be.
Right now, my site's pages namely https://www.oldweb.example/Vidz and the other one redirects on to https://www.newweb.example. What I would like to happen is for them to redirect to their respective new links which will be mentioned below.
https://www.oldweb.example/Vidz must be redirected to https://www.newweb.example/Vidz As well as https://www.oldweb.example/Helium must be redirected to https://www.newweb.example/Helium

cpanel .htaccess is not redirecting to HTTPS

I'm missing something simple I'm sure, some insight would be very helpful. I am working on setting up web site on a vps using cpanel. I'm trying to get it to always redirect to https instead having both available, I can do this without cpanel, but seem to be stumped when cpanel gets involved. I saw this, but it was zero help
https://www.namecheap.com/support/knowledgebase/article.aspx/9770/38/how-to-force-https-using-htaccess-file-in-cpanel
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
RewriteCond %{HTTPS} on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Try writing your RewriteRule like this:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L,NE]
Make sure you clear your cache before you test this. I've set the flag to R, but if you're happy with the redirection then change it to R=301 as that will make it a permanent redirect.
You can always use a different .htaccess rule to redirect to HTTPS. I would re-write your .htaccess to the following (hardcoding your domain):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>

htaccess rewrite after redirect

I have a site which uses htaccess to rewrite all pages to the index page with a hash which is then used to serve up content. The file looks like this....
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
I am now moving some of the pages of the site, however if I add a redirect such as....
Redirect 301 /blog /new_location/blog/
I am running into problems with the resulting url looking like
https://mydomain/new_location/blog/urlpath=blog.php
Can anyone suggest a way that I get the page to redirect to mydomain/new_location/blog/ and then run the rewrite on the new url.
Many thanks
RewriteRule and Redirect are from different Apache modules, so run at different times in the processing, not in the order they appear in the configuration. You're best off sticking to one module or the other, by using the [R] flag to RewriteRule.
RewriteRule /blog(.*) /new_location/blog$1 [R=301]
OK, I managed to get this working using a combination of Redirect and Rewrite like so....
Redirect 301 /blog /new_location/blog
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
Maybe not the neatest solution, but it work!

htaccess issue causing internal server error too many redirects

so I am playing with .htaccess to have clean URLs in my codeigniter app.
in short, i am trying to:
1) remove index.php in urls (redirect permanent)
http://localhost/directory/index.php*
to
http://localhost/directory/*
http://my.domain.com/index.php*
to
http://my.domain.com/*
2) rewrite requests for certain controllers to index.php/[controller_name]
http://localhost/directory/controller1*
to
http://localhost/directory/index.php/controller1*
http://my.domain.com/controller2*
to
http://my.domain.com/index.php/controller2*
my htaccess file currently goes like this:
Options +FollowSymlinks
RewriteEngine On
#RewriteBase /
# Redirect index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^(.*)index\.php((/)(.*))?$ /$1/$4 [R=301,L]
first issue:
this does not work for http://localhost/dir/index.php/controller1.
instead redirecting to http://localhost/dir/controller1, it redirects to http://localhost//controller1 ($1 return empty string?)
# Rewrite CI certain controllers
RewriteCond %{THE_REQUEST} directory/(home|other_controller) [NC]
RewriteRule ^(.*)(home|other_controller)(.*)$ /$1index.php/$2$3 [NC,L]
second issue:
this does not work for http://localhost/dir/home gives internal server error (too many redirects).
but if I test added R=301 code, it successfully redirect to http://localhost/dir/index.php/home. but this is not my intention to redirect, I only need to rewrite it.
please advise.. :)
Try with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You can place this in the directory the bootstrap file (index.php) is in.
If you have FastCGI implementation, you need to add a question mark in the rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]