How to remove index files from URL on Nginx

So yesterday while i was idling on Freenode i saw someone asking in Nginx channel that he basically wants to remove the index.php file from the url or in other words he wants to redirect into … probably for canonicalization purpose (canonical url) and/or perhaps SEO purpose. He had tried this:

# And it causes redirect loop (also tested on my local box)
# Atlhough it works fine for simple php files but not on his vBulletin setup
# or other non simple php file (because i don't have access to vBulletin i tested it on other script)
# Note the | is for illustration
location =|~ /index.php {
	rewrite ^|/ permanent;

And being curious, i tried some testing to remove that index.php thing from the URL and found a way to do it and it was simple. So if you’re currently looking for a way to remove or redirect your index file from the URL and you’re using Nginx perhaps this simple tips can help you with that

First thing first is open your nginx configuration files that hold your virtual hosts configuration and then add this line inside the server directive:

# This will redirect index.php index.htm to /
# Feel free to add other extension you need
if ( $request_uri ~ "/index.(php|html?)" ) {
	rewrite ^ /$1 permanent;

And an example just in case you want to see where it should be placed

server {

	[...] # other stuff such as server_name, access_log, etc

	# Here goes the code
	if ( $request_uri ~ "/index.(php|html?)" ) {
		rewrite ^ /$1 permanent;

	location / {
		[...] # your root configuration

	location ~ "\.php$" {
		[...] # your fastcgi configuration


Then reload your nginx configuration. Now every request for index.php or index.html or index.htm file will be displayed as And if there’s a query string in your URL it’ll be displayed as instead of


Note: Comment may not appear right away.

7 comments on “How to remove index files from URL on Nginx

  1. worked very but the problem is when trying to open a sub-directory like
    /other/index.php showing / instead of redirecting it to /other/
    i did resolve this problem by using the same code with a small change :
    if ( $request_uri ~ “^/index.(php|html?)” ) {
    rewrite ^ /$1 permanent;

  2. If{} blocks in nginx are bad. For this example, you should put the rewrite in a location block. Much better that way.

    1. Hello Dan,

      According to , using IF statement inside location is bad also the nginx wiki says that the 100% safe thing is if you’re returning value or rewrite but in this case it is used outside location context and it is a rewrite so i think this is the correct way to do it

      Other than that, i’ve personally tried using location block (because he said that he has tested it, but i just want to make sure) and it causes redirect loop (see the first code block) when tested against non simple php file and in this case he (the asker) is using vBulletin, but because i have no access to vBulletin i’m using Piwik for testing instead

      I’d be grateful if you could elaborate/explain more on the location block you’re saying and why the IF statement is bad in this case, so i can make corrections :)

  3. thanks a ton! for some reason SMF wouldnt load on my server without having something like ‘/index.php?board=1.0’ even though i have an seo url modifier. this did the trick though thanks dude!