I recently had the frustrating experience that a new WordPress staging site that I installed on a Google Cloud Instance was all of a sudden giving 404’s on all my interior pages. I realized this was due to the site not generating “pretty permalinks”. This is the option under settings in which you can have a URL such as http://mysite.com/example rather than have to use http://mysite.com/?p=123

Before one can figure out how to to fix a problem, you first have to figure out what is causing the problem.

For most issues in WordPress, my first point in the debugging process was to deactivate all my plugins, revert to the default theme, and try the process again. When this didn’t work, I knew it was an issue with the .htaccess file.

Is Your .htaccess file working?

The .htaccess acts as a gatekeeper on Apache servers, doing all manner of things including redirects, setting up permalinks and so on. While I’m unlikely to touch the file on a day to day basis, a plugin might corrupt it without my knowing.

Other times it’s possible that one might delete the file completely by accident or make changes to it that break your permalinks.

Reset Your Permalinks

Normally this means that somehow your .htaccess file has gotten corrupted somehow, and just needs to be reset. To do that one would normally just  use the usual technique of turning the permalinks off and back on in the wp-admin>settings>permalink page. Next, if your standard permalink structure is set to Day and name for instance, change it to Numeric and click the Save Changes button at the bottom of the screen. Don’t rush to refresh your site yet, there is one more step. Next, select your original permalink structure (i.e. Day and name in our case) and hit the Save Changes button once more. Now you can reload your site and check your posts/pages.

Is the .htaccess file writable?

When this still isn’t working, I need to confirm that the .htaccess file is writable by the server. Bring up the .htaccess file via ftp or ssh. Change the permalinks settings and see if anything in the file has changed. If it hasn’t, check to see if the file permissions are good. According to the codex, 644 is normally required and recommended for .htaccess files.

If you’ve modified the permissions, and running the settings reset hasn’t resulted in any changes to the file, go ahead and TEMPORARILY set the permissions to 777.

If you are now able to write to the file, then you know there’s a permissions issue between the user that controls your WP install, and the files it’s supposed to be able to touch. 

In my case it was fine, I saw changes occuring to the file, but there wasn’t a change in the front end permalinks working. This indicated to me that the .htaccess file just wasn’t being used by the server. A real rudimentary way to test this is to put some bad code at the top of the file.

Testing whether your .htaccess file is being used

The simplest way to test if apache uses your .htaccess file, or if it otherwise ignores it, is to intentionally break it.

Edit the .htaccess file, so the first line reads ‘Test.’:

Test: I'M INSERTING PLAIN TEXT TO BRING THE SITE DOWN, WHICH WILL RESULTS IN A 500 ERROR

# Use PHP56 Single php.ini as default
AddHandler application/x-httpd-php56s .php

# BEGIN WordPress
AddHandler application/x-httpd-php70 .php
<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

Now, if you refresh the page in your browser, you should see an error page like this:

THIS IS WHAT HAPPENS IF YOU INTENTIONALLY BREAK YOUR .HTACCESS FILE

If you got this screen, that’s a good sign. That means Apache is parsing your .htaccess file correctly. So when I didn’t get a broken 500 page error, I knew that the .htaccess file was just getting ignored all together.

How to make your server respect your .htaccess file

First, let’s confirm that the apache mod_rewrite module is enabled. 

  1. Create a new php file in your root folder of your WAMP server. Enter the following phpinfo();
  2. Access this file from your browser.
  3. Ctrl F to open a search. Search for ‘mod_rewrite’. If it is enabled you see it as ‘Loaded Modules’
  4. If it’s not, then:

Enable mod_rewrite in Apache

There are a number of ways to enable mod_rewrite, in case it’s not yet enabled on your setup. See this Stack Overflow thread for various ways this may be done on different setups: How to enable mod_rewrite for Apache 2.2.

If you are still with me on this journey, the next one is going to be the least familiar and most technical of the steps. Don’t be intimidated but you’re going to need to roll up your sleeves and get into the command line via the browser based SSH built into your Google Cloud Platform Instance. 

You’ll get to the browser based ssh 

Step 1) Make sure you have enabled mod_rewrite in apache. This can be done by going through Compute Engine>VM Instances > identify the correct instance and to the far right on the line will be SSH. Click it and it will take you securely inside a command line interface. Run this command: 

$: a2enmod rewrite

Enable Apache AllowOverride

2) Check your virtualhost configuration in /etc/apache2/apache2.conf and make sure that it is configured with AllowOverride All instead of the default AllowOverride None for your web root directory. This tells apache it is ok for settings in .htaccess files to override settings. Use this command to modify the file.

sudo nano /etc/apache2/apache2.conf
Once you're in there, find the right spot and change it.

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride ALL <----(edit from none)
        Require all granted
</Directory>

After you’ve made that change, make sure to restart the server:

sudo service apache2 restart

Now, your .htaccessshould work. You can also make this change inside a virtual host, which would normally be preferable, but that depends on the way Apache is set up.

19 Comments

  1. Man you are a freakin’ life saver! I’ve been developing sites for 7 years but I am very, very new to Google Cloud…

    I spent hours digging through this until I came across your post.

    Thank you for sharing this!

  2. Many thanks for your post, it is the only one that I found about this, however, I am having problems to run the command “$: a2enmod rewrite”

    Could you help me with this?

    Initially, this is what I found:
    info@wordpress-1-vm:~$
    but when I write your command and press enter I get this:

    info@wordpress-1-vm:~$ a2enmod rewrite
    -bash: a2enmod: command not found

    Many thanks for your help!!

    • Hi Hector,
      You’re probably aware of this, but just to make sure, you’re not actually typing the “$: ” in your commands, right? That’s just the stand in representing that you should run the command (without the $) as a regular user, as opposed to running it as root.

  3. I was so happy that I could have fixed it with this article, but in my case – it didn’t.

    Thanks to the article, I could check that my re-write module is enabled, and I could have confirmed that my .htaccess file is ignored. I have no idea what’s next :(

    I am running a clean wordpress instance on google cloud (instance is a google cloud marketplace instance wordpress already installed) ; totally fresh vm (at least 5 times).

    Any idea how I can fix it? Many thanks in advance

    Should I keep digging or just to

    • Hi Ark,

      Did you follow through with enabling Apache AllowOverride? The last (and only time) that I installed a fresh WP install on Google Cloud this was the final step I had to go through.

  4. Thank you! Crikey, I had tried everything that I could think of doing to get my desired permalink structure to work, and got absolutely no where except the awful 404 page. Your solution worked!

    • You’re so welcome Liam. Knowing that my own hard learned lessons can be helpful in getting my fellow developers to a place of peace makes writing these tutorials worthwhile. Lord knows, it’s certainly not to impress my potential clients with all the jargon and geek speak :D

  5. Attempting in ssh:
    a2enmod rewrite
    returned..
    -bash: a2enmod: command not found
    .. in every directory i attempted.

    Does this also apply to appengine projects? I cant seem to reroute permalinks.

  6. thanks for the great tutorial, still it did not work for me, I’ve followed every single step but .htaccess is still getting ignored.

    I’m not using WordPress, it’s a Expression Engine installation, but still the root proble is the same, google computer engine ignoring .htaccess.

    I’ve posted the question here if anyone feels like contributing! :)

Leave a Reply

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

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.