0

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?

SatheeshCK17
  • 523
  • 6
  • 17
  • 1
    RewriteConds always affect the directly following rule. You do not want to check whether a file or folder actually exists, before you make your domain redirect. And the rule that _actually_ needs those conditions, the one rewriting to the index.php, now is completely independent from them. Put the condition that checks the host name and the domain redirect rule first (after RewriteEngine On), and the existing file/folder checks + the rule that rewrites to the index.php, after those. – CBroe Mar 15 '22 at 07:50
  • 1
    _"Actually, I want to redirect all requests (`www.domain.co.in`, `domain.co.in`, `www.domain.info.in`) to `domain.info.in`"_ - then it would probably be easier, if you just used a negated condition, that checks if the host name was _not_ exactly `domain.info.in`. (That is assuming that you do not have any other domains routed into the same folder as well, that you would _not_ want to redirect.) Because your current condition, `^(www\.)?domain\.co\.in`, matches on `domain.co.in` as well, and that would probably explain the endless redirect. – CBroe Mar 15 '22 at 07:52

1 Answers1

1

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]
SatheeshCK17
  • 523
  • 6
  • 17