Question

Redirect index.php to / only in main folder

Posted March 15, 2016 3.9k views
NginxUbuntuLEMP

Hello,

I am using LEMP droplet. For SEO purposes, I made redirection from site.com/index.php to site.com 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.

Thanks,
Milos

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

I have no idea what you seek to achieve with this, but use try_files. That way, unless you are actively using http://foo.bar/index.php 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.

    Thanks

Submit an Answer