Mike
Mike

Reputation: 183

How to redirect index.php/controller/action to controller/action without redirect loop?

Solved :)

RewriteCond %{THE_REQUEST} index.php
RewriteRule index.php(.*)$ http://%{HTTP_HOST}$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]

I configured a redirect so that I can open http://example.com/controller/action and ModRewrite route the request internal to ...index.php/controller/action.

Now I want to redirect a user if he opened http://example.com/index.php/controller/action automaticly to index.php/controller/action (301 redirect). But now I get an endless loop of redirects.

Is there any chance to seperate the internal redirect to the bootstrap index.php and prevent, that one user can open the url with the index.php subpath?

I tried this, but that doesnt work (enless loop):

 RewriteCond %{REQUEST_URI} index.php/(.*)
 RewriteRule ^index.php/(.*)/$ http://%{HTTP_HOST}/$1/ [L,R=301]

 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ /index.php/$1 [L]

Upvotes: 1

Views: 1417

Answers (1)

Sidora Gleb
Sidora Gleb

Reputation: 11

Common way to solve this on PHP side is to have counter which increments with every redirect. E.g.: https://github.com/vjousse/symfony-1.4/blob/master/lib/controller/sfController.class.php

if ($this->getActionStack()->getSize() >= $this->maxForwards)
{
  // let's kill this party before it turns into cpu cycle hell
  throw new sfForwardException('Too many forwards have been detected for this request.');
}

Upvotes: 0

Related Questions