How I finally got rid of index.php from my urls.
TL:DR It was an Apache server setting
I’ve been trying to get this site listed in Google with various levels of success, some things get indexed, some things don’t. One of the things that kept coming up is apparently an extremely important file that many search engines depend on, the sitemap. A file that lists all posts, pages, categories, tags, etc. for search engines in one (or more) convenient package. Apparently I did not have one.
This is not Sitemap’s story
In my quest for the coveted sitemap, I stumbled upon another issue: index.php
. And the fact that it kept showing up in my urls. When I finally got a plugin to create my sitemap, you could only get the file if index.php was in the url, contrary to link the plugin provided. From what I’ve read, having index.php in your urls can have detrimental effect in getting your pages indexed, and makes your urls look messy. It doesn’t help if you need it to retrieve your sitemap file, and the search engines don’t know it.
This is Index.php’s story (on how I removed it)
When loading any of the posts or pages on this site, the url-bar would have something like this address:https://www.einherjar.org/index.php/the-poetic-edda/havamal/
instead of https://www.einherjar.org/the-poetic-edda/havamal/
(Both of these links work, because I fixed the problem.) Before, when you clicked on the link without index.php, you would get a 404 page.
Searching the internet for answers, the most common fix was logging into your WordPress site, navigating to Settings —> Permalinks and then choosing ‘Day and Name‘ or ‘Month and Name‘, instead of ‘Custom Structure‘ and then save changes. This seemed to help a lot of people. Unfortunately it just about broke every link on this site. 404 pages everywhere.
Again searching the internet for answers, the most common answer was issues with the .htaccess file in the root of the website. Either it wasn’t being read, or it couldn’t be written, or problems the settings inside the file. The .htaccess file tells the webserver how to perform certain functions and deal with specific situations. To test if WordPress couldn’t write to file, I renamed .htaccess to .htaccess.bak. Then I went back into WordPress, changed the Permalinks, and saved. a brand new .htaccess file was written there. To test if the .htaccess file was being read, I edited the file. I typed jibberish at the top of the file and saved it. Then I tried to navigate to one of my pages, and the whole website failed to load. So .htaccess file was being read. I deleted the new file and restored the back up. everything worked fine and dandy as long a index.php was in the url. It took a couple of days of pulling my hair out (which seems to be a theme in a lot of these posts, but I have a lot of hair) when I finally stumbled on the solution.
The Solution
The problem wasn’t with WordPress, nor was it with .htaccess being read or written to. The problem was that the .htaccess file was being read, but wasn’t being obeyed. I had to edit /etc/apache2/apache2.conf
to get eveything to work.
First I made sure the Apache’s mod_rewrite was installed
sudo a2enmod rewrite
Then I edited Apache’s configuration file
nano /etc/apache2/apache2.conf
Next I looked for this entry:
<Directory /var/www/>
# Options Indexes FollowSymLinks <Lynx Edit>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
As you can see, I’ve already edited this file in the past, I set it to stop the server from showing the content of directories. I always mark my edits, makes it easy to fix any changes I’ve done. this time I changed AllowOverride
setting
<Directory /var/www/>
# Options Indexes FollowSymLinks <Lynx Edit>
Options FollowSymLinks
# AllowOverride None <Lynx Edit>
AllowOverride All
Require all granted
</Directory>
Saved the file, and restarted the webserver
sudo systemctl restart apache2
Then I went back into WordPress, changed my Permalink to ‘Day and Name’, hit ‘Save Changes’ and viola! No more Index.php. My sitemaps were where they were supposed to be, and all my links look pretty.
Hope this helps someone else out.