Prevent duplicate URLs

I’m sure this has been answered before but I’ve spent hours looking searching through the web without success.

I want to disable all appearances of “index.php” in my URLs. To do this I’ve added:


'showScriptName'=>false

into my configs and edited my .htaccess into:


Options +FollowSymLinks

IndexIgnore */*


RewriteEngine on


RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d


RewriteRule ^(.+)$ /web/app/index.php [L]

The above does what I want. However, the pages are still accessible through multiple URLs:

localhost/web/app/site/contact

localhost/web/app/index.php/site/contact

How would I prevent this?

Thanks,

Daniel

I had the similar problem and has been solved nicely.

Look at this thread, search the post #6:

Clean url’s - it’s possible?

Thanks!

I’m not sure that you are referring to the same problem though.

I’m able to access my test-suite using urls like:

localhost/web/app/site/contact

But i’m also able to use:

[color="#FF0000"]localhost/web/app/index.php/site/contact[/color]

I want to prevent the RED version by redirecting the user to a URL without index.php. Yii won’t create any URLs that look like this but who knows what google might find…

Google will find only url’s you provide in your html code (generated by php/yii)

If you still want to stick to your original idea, display an error when your url contains "index.php":

In Controller.php, init() function:




if (strpos(Yii::app()->request->requestUri, 'index.php') !== false)

  throw new CHttpException(404);



Thanks!

That’s good enough :wink:

Actually, this just displays an exception. After some debugging it seems Yii re-routes all exceptions to the error handler which in turns calls the controller again, causing a second exception. The above code will work fine when placed in an action, but not in the init() function of the controller since it will be run multiple times.

The only solution I can think of is adding an if statement, disabling the code to run if the controller/action actually is the error handler.

Yes, you are right…

Perhaps this may be an another solution:




if (strpos(Yii::app()->request->requestUri, 'index.php') !== false)

  Yii::app()->end();