Redirect index.php to / only in main folder


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.

Thanks, Milos

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.