Removing Index.php From the URL Slug in WordPress

This post assumes you’re having a hard time removing the index.php file name from the URL slug of your WordPress site.

Looks like something like this:

defragged.org/index.php/%postname%

This means one of two things:

  1. Your web server (Apache) doesn’t have the mod_rewrite module enabled
  2. Your mod_rewrite module is not recognizing your .htaccess file
  3. Or you’ve royally fucked up

The rest assumes it’s either 1 or 2. First thing you want to do is make sure that you have made the appropriate setting update in your WordPress settings page. Looks like this:

Assuming you have this set, we now have to switch our focus to your server. In this instance, I assume you’re on a linux machine (Ubuntu 18.04) and working with Apache. If you’re not, it should give you general instructions that help you with your machine.

First verify that your server allows an .htaccess to be created at the root of your site. You should have something that looks like this:

# ls -la /var/www/defragged.org/
total 204
drwxr-xr-x5 www-data www-data4096 Oct5 21:29 .
drwxr-xr-x5 root root4096 Oct5 04:43 ..
-rw-r–r–1 www-data www-data 235 Oct5 20:51 .htaccess
-rwxr-xr-x1 www-data www-data 418 Sep 252013 index.php
-rwxr-xr-x1 www-data www-data 19935 Jan62018 license.txt
-rwxr-xr-x1 www-data www-data7415 Mar 182018 readme.html

Open your .htaccess file, it should have something like the following:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

If you have this, this is all good. You’re making progress. Now, we’ll switch our focus to the web server.

Again, assuming Ubuntu 18.04, you’re going to want to enable your rewrite module. You do that like this:

# a2enmod rewrite

Go ahead and restart your web server:

# systemctl restart apache2

Now you want to insert your directives into the virtual host file for the site you’re working on. So it’ll look something like this:

# vim /etc/apache2/sites-available/defragged.org.conf

This is where I screwed up. I put the syntax inside the syntax, but it needs to be outside. It should look something like this:

<VirtualHost *:80>
ServerAdmin [your email]
ServerName defragged.org
ServerAlias www.defragged.org
DocumentRoot /var/www/defragged.org/
ErrorLog ${APACHE_LOG_DIR}/defragged.org.error.log
CustomLog ${APACHE_LOG_DIR}/defragged.org.access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =defragged.org [OR]
RewriteCond %{SERVER_NAME} =www.defragged.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<directory /var/www/defragged.org>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</directory>

Go ahead and restart Apache again for good measure:

# systemctl restart apache2

This should do the trick. Navigate to your site, select the post and it should work.

Leave a Reply

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