Remove index.php from permalink in WordPress

WordPress Permalink Structure

After WordPress migration to another server, which don’t necessarily have to have the same settings as the one you are migrating from, there is a chance of having “index.php” added to your URL. This can often lead to 404 missing page error and it disrupts friendly URL’s. Learn how to remove index.php from permalink in WordPress.

After successful migration of your WordPress site to another server the first thing you probably would wanna to do is to enter the Permalinks settings and check your desired option for permalinks, which in most cases is “Post name” and to save that setting into the database. But since you probably imported database chances are that you are going to see the “index.php” already created itself into the URL structure. If you want to remove index.php from permalink in WordPress here is what you might try to do:

1. Find out if “mod_rewrite” is enabled on your server

If you don’t have direct info from your hosting provider if this module is active on your server you can create info file and check for yourself. Learn how to create PHP info file in this post.

2. Set the permalink structure

Go to Dashboard > Settings > Permalinks and choose “Custom Structure” option and enter in the field: /%postname%/ and click on Save Changes button.

3. Edit your .htaccess file

Copy the following directive into your .htaccess file which is located in the root of your website folder:


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This should be enough to remove the index.php from the URL structure.

Learn more about Permalinks in WordPress on the official WordPress Codex page.

Additional known issues

Sometimes the steps described above don’t give any results in removing the index.php from your permalink structure, and here is what may cause the trouble. On some servers with tight security the mod_security is blocking your settings thus index.php still remains in the URL structure. Try adding this piece of code in your .htaccess file above the rules described in step 3 of this post:


<IfModule mod_security.c>
SecFilterEngine Off
</IfModule>

One more thing to check is config file for Apache which can also override your .htaccess directives. Look for Ubuntu default /etc/apache2/apache2.conf file and change the entry for / and /var/www from AllowOverride None to AllowOverride All. Then restart Apache server and hopefully it can solve the issue as well.

By janoshke

Web developer and IT consultant. Freelancer with full respect for OpenCart and WordPress. Gamer, (ex)drummer and parent.

46 comments

  1. What happens, if that doesn’t work? I’ve followed the steps that you have mentioned here and I still am stuck with /index.php/ being required in my urls.

    1. Hi there,

      Thank you for your comment and you asked a good question as well. I recently had this case and here is what came to my knowlege. There are two things you might want to check server side:
      1. Is mod_rewrite installed and active on your hosting server? (Check with your host company or server admin)
      2. Is mod_security blocking your settings? (Also check with server admin)

      In my case the 2. option apply and here is a little spice that my server admin added to .htaccess file which solved the issue. I will edit the post with this piece of code.

      I hope this helps. Cheers πŸ™‚

  2. I needed to update apache2.conf – Thank you for including this in your article. Works good.Lasts long time.

    1. Hi,
      Thanks for the comment. I tested it with Apache so far, but if you find out it works on nginx please do share in the comments.

      Cheers πŸ™‚

  3. Thank you so much. I don’t have much web experience and your article saved me so much time fixing the permalinks on my site.

    1. There is no code changing involved. You just give the command via htaccess file to overwrite the piece of the URL, and that’s it πŸ™‚

  4. Worked like a charm!
    There was a file called htaccess in the web root. After several failure attempts, I saw that file name, renamed it to .htaccess and it all worked – WordPress site was hosted on Fedora Linux

  5. That is so helpful article. The most important is the Server side, where the mod_rewirte has to be enabled. The rest is just account settings.

    Thanks.

  6. Thank you very much for the information, the last steps solved my problem.

      1. Hi!!! the last step it works for me too, but, exist a form to do that with code? is that results from a bad installation or configuration?

  7. Thank you so much! I had to pull up an additional video to assist with my C:panel and the Mod_rewrite as I am a total newbie when it comes to this, but this helped me so much, and fixed my website. Truly appreciate it!

  8. Hi janoshke,
    Thanks for the excellent tips! I have been tolerating the ‘index.php’ in my URLs for a while now then decided to Google a way to remove them. I didn’t think it could be done as the ‘index.php’ was probably serving the ‘.html’ page. It’s not so! The whole thing was unnecessary.
    I don’t know if it makes a difference but here is my setup:
    – I’m hosted at Hostinger,
    – web server is listed some places as ‘LiteSpeed’ (for caching, I believe), other places as Cloudflare, but I think it’s actually apache
    – WordPress version is 5.4.2

    I only had to use the simplest method you described to be rid of the ‘index.php’ from my URLs, namely, go to WordPress Dashboard > Settings > Permalinks > Set to ‘Custom’ and enter /%postname%/
    Save Changes and works like a charm! URLs are now like http://www.example.com/privacy-policy
    Thanks again!

      1. I tried many websites, but no one was working. but the last solution of changing `/etc/apache2/apache2.conf` worked like a charm for me.
        Second there is no option for me to add new comment, I can only reply to comment.

  9. Hi, I have tried all the options stated above but unfortunately i am still getting 404 without index.php

Leave a Reply