Enabling pretty permalinks on WordPress – apache configuration does not work

Probably this error has a pretty easy solution but I’ve been looking way to long at this and still don’t get the error. I think I’ve tried whatever I could.

Problem: when I enable pretty permalinks on my wordpress installation (so, that it is using /%postname%/), it doesn’t work. I get a 404 on all pages except for the homepage.

Read More

This page http://codex.wordpress.org/Permalinks tells me the requirements for permalinks to work:

  • Apache web server with the mod_rewrite module installed
  • In WordPress’s home directory,
    • The FollowSymLinks option enabled
    • FileInfo directives allowed (e.g. AllowOverride FileInfo or AllowOverride All)
    • An .htaccess file (if this file is missing, WordPress will try to create it when you activate “pretty” permalinks)
    • If you want WordPress to update the .htaccess file automatically, WordPress will need write access to the file.

Apache web server has been installed, the mod_rewrite module has been loaded with a2enmod rewrite command (and the server has been restarted multiple times after). So, under /etc/apache2/mods-enabled, the symlink to rewrite.load is present. Also, when I run phpinfo command, I see that the mod_rewrite module has been loaded. You can check this as well here: http://namorti.com/phpinfo.php

Then, in /etc/apache2/sites-enabled, there was no “default” present. I copied 000-default.conf to default and edited default afterwards. It contains the following:
DocumentRoot /var/www

    <Directory />
            Options FollowSymLinks Indexes
            AllowOverride FileInfo
    </Directory>

So as far as I’m concerned, FollowSymLinks has been enabled and FileInfo directives are allowed.

As for the last two points, in my wordpress home directory (/var/www), .htaccess is present and writeable by WordPress (I updated the permalink structure a couple of times and it updates the .htaccess file accordingly). Right now it contains the following:

# 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

So, as far as I know, it SHOULD be working. I restarted the server (service apache2 restart) several times. I don’t see what I’m missing. Anyone has a clue here?

Thanks in advance!

* EDIT *

So, I did what calcinai told me to do… I edited my /etc/apache2/sites-enabled/default file (containing the vhost). It now looks like this:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin www.namorti.com
    DocumentRoot /var/www

    <Directory /var/www>
            Options MultiViews
            AllowOverride None
            Order allow,deny
            allow from all

            RewriteEngine On
            RewriteBase /
            RewriteRule ^index.php$ - [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]
    </Directory>
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I’ve restarted apache again, but unfortunately it still doesn’t work. Honestly it would have surprised me, because moving the directives from the .htaccess file to the vhost would work if the htaccess on itself would work, as everything else seemed correct enough to me…

Any other suggestions? Thanks for the effort!

Related posts

Leave a Reply

5 comments

  1. Make sure the AllowOverrides directive is set to all in the vhost – that’s what tells apache to look for .htaccess files in the webroot.

    A better solution is actually to put your rewrite directives in the vhost, as you clearly have write access to the file. When you have AllowOverrides on, apache will look in the directory and all parent directories for .htaccess files on every request, which can be a big performance hit.

    For WordPress, your vhost should look something like:

    <VirtualHost *:80>
        ServerName example.com
        DocumentRoot /var/www/site
    
        <Directory /var/www/site> 
            Options Indexes FollowSymLinks
            AllowOverride None 
            Order allow,deny 
            allow from all 
    
            RewriteEngine On
            RewriteBase /
            RewriteRule ^index.php$ - [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]
        </Directory> 
    </VirtualHost>
    
  2. Solved it myself.

    It had to do with the file “000-default.conf” – I copied it to “default” and edited “default”, like I already mentioned in my question.

    Apparently the service is using the file “000-default.conf” on itself. By copying the “default” file back to “000-default.conf” and restarting the apache service, everything works now.

    One caveat: calcinai’s suggestion seems like it should work, but with his suggestion in my vhost in the correct “000-default.conf” file, I got a 403 Forbidden error. When I replaced his content with my original content

    DocumentRoot /var/www
    <Directory />
            Options FollowSymLinks Indexes
            AllowOverride FileInfo
    </Directory>
    

    And then once again restarting the apache service, it all worked.

    Thanks calcinai for your effort to try to help me 🙂

  3. Permalink Problem

    Inside of this file, we want to change all things.

    sudo nano /etc/apache2/sites-available/000-default.conf
    000-default.conf 
    

    – should look something like this:

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    

    If you dont see like this then paste the above code inside virtual host

    When you are finished, save and close the file.

    Next, we need to enable the rewrite module, which allows you to modify URLs. You can do this by typing:

    sudo a2enmod rewrite
    

    After you have made these changes, restart Apache:

    sudo service apache2 restart
    
  4. After spending really lots of time, here is what worked successfully in my Linux machine’s LAMP server. Following changes are needed for me to make it work perfectly,

    1. Add this to your /etc/hosts file.

      127.0.0.1   sitename.com
      
    2. Add host entry as follows in the path /etc/apache2/sites-available/sitename.com.conf

      <VirtualHost *:80>
          ServerAdmin webmaster@localhost
          ServerName sitename.com
      
          DocumentRoot /var/www/sitename.com/public_html
          <Directory />
              Options FollowSymLinks
              AllowOverride None
          </Directory>
          <Directory /var/www/sitename.com/public_html>
              Options Indexes FollowSymLinks MultiViews
              AllowOverride All
              Order allow,deny
              allow from all
          </Directory>
      
          ErrorLog ${APACHE_LOG_DIR}/error.log
          CustomLog ${APACHE_LOG_DIR}/access.log combined
      </VirtualHost>
      
    3. Create symlink to sites-enabled.

      sudo ln -s /etc/apache2/sites-available/sitename.com.conf /etc/apache2/sites-enabled/sitename.com.conf
      
    4. Restart apache by using the command,

      sudo service apache2 restart
      

    This works perfectly for me. Instructions for adding host entry taken from, https://www.digitalocean.com/community/questions/wordpress-permalinks-not-working-on-ubuntu-14-04