Last updated on September 13, 2023

Remove index.php from URL

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Remove index.php from the URL.

It’s great for SEO if your permalinks look pretty. Hence, you might want to remove the index.php part from your WordPress URL. If the index.php part shows up in your URL, you probably don’t have pretty permalinks enabled.

Method 1: Enable Pretty Permalinks

The most practical way to remove the index.php from your URL, is through the WordPress settings. Go to Settings → Permalinks and select one of the “Pretty” options.

A screenshot of the WP Admin area showing the Permalinks subpage.
Pretty Permalinks can be enabled from the WP Admin area under Settings → Permalinks.

If this doesn’t resolve the issue, continue reading.

Method 2: Modifying the.htaccess file

To remove index.php from WordPress URLs, you can to modify the .htaccess file in the root directory of your WordPress installation. Here’s an example code snippet that you can add to the .htaccess file:

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

In this code snippet, we use the mod_rewrite module to remove index.php from URLs. We first add the RewriteBase directive to set the base directory for the rewrite rules, and then we add a RewriteRule to match requests for index.php and stop processing any further rewrite rules.

Next, we add another RewriteRule to match URLs that end with /index.php and redirect them to the same URL without index.php, using a 301 (permanent) redirect. This ensures that any existing links to URLs with index.php are redirected to the new URL format.

Finally, we add two RewriteCond directives to exclude requests for files and directories that actually exist on the server, and a final RewriteRule to rewrite all other requests to index.php.

By adding this code snippet to your .htaccess file, you can remove index.php from WordPress URLs and create cleaner, more user-friendly URLs for your site.

Last updated on September 13, 2023. Originally posted on May 12, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *