Nginx: How To Redirect /index.php To / To Avoid Content Duplication

By Angsuman Chakraborty, Gaea News Network
Friday, August 8, 2008

Normally any website that responds to /index.php also responds to /. This has the potential to duplicate content. So how can you re-direct /index.php to / without causing infinite loop in nginx?

Here is a simple solution:

if ($request_uri ~* "^/index.php\??$") {
    rewrite ^.*$ https://$host? permanent;
}

It redirects any url with /index.php or /index.php? at the end. However it doesn’t prevent internal re-direction to index.php as is normal with most PHP based web software.

Discussion
January 20, 2010: 10:46 am

güzel bilgiler için teşekkür thank you werh much.


Louis Vuitton handbags
September 29, 2009: 8:31 pm

that is very good!


purab
August 13, 2009: 12:15 pm

if ($request_uri ~* “^/index.php\??$”) {
rewrite ^.*$ https://$host? permanent;
}

This code is useful when all requests are going to index.php.
What will happen when request are going to multi pal pages and if folders are different then what we need to do with ending slash.

I am looking for removing ending slash from subfolders of website.

June 9, 2009: 5:17 pm

Thanks a lot! I was fighting with this for a while…

May 2, 2009: 7:58 pm

Um, yeah… please ignore my last comment. nginx, not apache… got it. :)

May 2, 2009: 7:56 pm

With mod_rewrite you can do this:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ (.*)/index.php\ HTTP/
RewriteRule .*index.php$ https://%{HTTP_HOST}%1/ [L]

see https://blog.blacknode.net/2009/05/redirect-indexphp-to-root-of-current-directory/

April 29, 2009: 4:04 am

Thank you for the tip :) You’ve inspired me to bring my .htaccess file up to speed.

August 23, 2008: 6:45 am

Thanks for this tutorial. I had been wondering how to do that until i read this.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :