htaccess error on redirect - too many redirects - apache

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]

Related

how to change url to a clean url that works

I've been looking for an answer for my problem, however the things I tried didnt work out. What I've been trying to do is to create a beatiful url for this link:
mywebsite.com/blog_template?slug_url=blog-post-name
to
mywebsite.com/blog-post-name
To achieve this I tried the following code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog_template/([^/]+)$ /blog_template?slug_url=$1 [L]
But my code didnt work... Any advice?
Here’s the full htaccess my website:
RewriteEngine On
RewriteBase /
ErrorDocument 404 http://91.218.67.117/404/
ErrorDocument 500 http://91.218.67.117/500/
#redirect 404
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
#remove php
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
Considering you want to rewrite this request in backend to index.php(OR change it to appropriate file's name in case its some other php file). With your shown samples and attempts please try following .htaccess Rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/blog_template\?slug_url=(\S+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?slug_url=$1 [QSA,L]
ErrorDocument 500 /pages/errors/500.php
NOTE: Please keep your .htaccess file and index.php(OR any php file which is taking rewrite request in backend for that matter) in same path(your root etc).
Your attempt was close. Just a small fix:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L]
This relies on additional rewriting rules to get applied though, since /blog_template most likely is not a resource the http server can somehow "execute" immediately. So you may want to combine above rule with other rules, which you did not reveal to us, which is why I cannot say anything about that.
If you also want to redirect requests to the "old" URL, then that variant should do:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^slug_url=([^/]+)$
RewriteRule ^/?blog_template$ / [QSD,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L]
It is a good idea to start out using a R=302 _temporary_redirection and to only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues on the client side.

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 pretty url and force www

I've been using following code to achieve pretty urls:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?p=$1 [L]
And it is working fine.
However, I also need to force my domain to redirect to www while keeping pretty urls.
I've read through some threads that ask for redirection to www, but I am unable to implement it together with pretty url (domain.com -> www.domain.com, domain.com/news -> www.domain.com/news respectively).
Can anyone help me achieve this?
Have www redirect rule before rewrite rule you already have:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?p=$1 [L,QSA]

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

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]

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]