Redirect index.php to / only in main folder

March 15, 2016 3k views
LEMP Nginx Ubuntu


I am using LEMP droplet. For SEO purposes, I made redirection from to like this:

server {
        if ($request_uri ~* "^(.*/)index\.php$") {
                return 301 $1;

It works just fine, but I need it to work on root folder of the site only. It currently redirects all folders, in example /folder/index.php will be redirected to /folder/

This prevents some of my scripts to work normally (Joomla backend in example). How can I change the above code and make this redirection for root folder only.


1 Answer

I have no idea what you seek to achieve with this, but use try_files. That way, unless you are actively using in your code, all requests will be evaluated at /.

Something like this:

location / {
  try_files $uri $uri/ /index.php?$query_string;

If you are doing this because of duplicate content you can rest assure that Google don't care all that much. Just be consistent in the way you link to your content.

  • I already have this line in my file.

    Yes, I am doing this to avoid duplicate content. Maybe I am not aware of new Google updates, but it used to be very important in past.

    I am new in nginx. On apache servers I was achieving that easily by using .htaccess files only in folders where I need this redirection.


Have another answer? Share your knowledge.