Can't get mod_rewrite to work on my Ubuntu 14.04 server

December 26, 2014 41k views

Hi. Can anyone please tell me why my htaccess rewrite is not working? I want to remove the .php extensions and I have the following in my .htaccess file, but to no avail. I am running a LAMP stack on Ubuntu 14.04 x64.

Options -indexes

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase /blog

# Allow any files or directories that exist to be displayed directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL 
RewriteRule ^(.*)$ index.php/$1 [L] 
</IfModule>

<IfModule !mod_rewrite.c> 
ErrorDocument 404 index.php 
</IfModule>

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteRule about about.php 
</IfModule>

# Apache Rewrite Rules 
<IfModule mod_rewrite.c> 
Options +FollowSymLinks 
RewriteEngine On 
RewriteBase /

# Add trailing slash to url 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$ 
RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([^\.]+)/$ $1.php

# End of Apache Rewrite Rules 
</IfModule>

Can anyone please take a look and tell me what's wrong with it? Will be much appreciated.

-Sadman.

26 comments
  • Hi! Is mod_rewrite enabled? Try running the following commands:

    sudo a2enmod rewrite
    sudo service apache2 restart
    
  • Hi @kamaln7 , yes mod_rewrite is enabled. I also tried this guide but it didn't work.

  • your conf file (located in /etc/apache2/sites-enabled) needs to have this:

        <Directory /var/www/yourwebdirectory>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride all
            Order allow,deny
            allow from all
        </Directory>
    

    then restart apache and see if it is working

  • Hi @sierracircle I already have that included in my 000-default.conf file. I've also uncommented a line in apache.conf file as mentioned in a guide to make htaccess work but it's still not working.

  • Show 22 more comments
2 Answers

it looks like your link to your stylesheet is the issue. Try changing it to:

/assets/css/style.css

instead of

assets/css/style.css

Have another answer? Share your knowledge.