sadman
By:
sadman

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

December 26, 2014 31.1k 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.

  • can you post your conf file?

  • Your missing a server-name (commented out). Uncomment and replace that with whatever your domain is. You can also add server aliases, for things like www.

    ServerName  example.com
    ServerAlias www.example.com
    
    
  • @sierracircle nope still not working. Uncommented servername, replaced example.com w/ my domain also added serveralias, then restarted apache2 but still doesn't work.

  • hmm. that should do it. Some other things I can think of:

    are you using an SSL cert? or just plain old http: ?

    have you updated permissions on your files:

    sudo chown -R www-data:www-data /var/www/html
    

    one other thing to try:

    in /etc/apache2/sites-available
    copy the 000-default.conf you are using and name the copied file after your domain like:

    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/yourdomain.conf
    

    disable the default site:

    sudo a2dissite 000-default.conf
    

    then enable that copy you made and restart apache.

    sudo a2ensite yourdomain.conf
    sudo service apache2 restart
    
  • I'm using http, and just updated the permission but still doesn't work. I'm pretty sure my htaccess file is working because when I put some random text right after RewriteEngine on my site shows a 500 internal server error, but my main target is to remove the .php extensions from mysite.com/contact.php, /about.php, etc. @sierracircle

  • sorry, it sounds like you are saying your .htaccess is working, but some of the rules you have are not..

    For some reason I thought it was not working at all.

    this rule works for me:

    # 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>
    

    if I have that, and visit http://mydomain.com/about.php, it resolves to http://mydomain.com/about/

  • @sierracircle just copied the conf file and named it as my domain but nope not working. Anything else you think I should try? Really appreciate the help so far mate.

  • I admit to being stumped.
    you say that some of your .htaccess rules are working because you can get errors when you start editing.

    what happens if you clear everything out of your htaccess except for maybe this rule:

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

    and then visit http://yourdomain.com/about

    does it resolve correctly?

  • @sierracircle removed everything from my htaccess file and pasted that code, still getting this error when I visit mydomain.com/about

    The requested URL /about was not found on this server.
    
  • @sierracircle when I go to http://mydomain.com/about.php it works fine, but it doesn't when I remove the .php extension.

  • hmm..sounds like mod rewrite is not working...that rule works fine for me:

    https://seaotter.de/about/

  • @sierracircle but if I put a rule to block a specific ip address (eg. my own ip) within <IfModule mod_rewrite.c> it works fine, I mean, it blocks my ip until I remove that line. So does it mean the rewrite module is actually working? This is what I currently have in the htaccess file and without it I cannot see my blog posts (anchor cms):

    <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>
    
  • I have to agree with you that mod rewrite seems to be working.

    Anyone else want to chime in on why some of these rules seem to not be working for him?

    How about this rule, can you tell me if this one works for you. Simple rule to re-direct www to website root.

    #redirect www to website root
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    
  • @sierracircle your rule to redirect www to root works fine, but now when I access my site without www before the url with or without this rule in the htaccess file, my whole site looks messed up - the css file doesn't load. But when I go back to the www site it works fine. :l

    Edit: I just ctrl+ refreshed and now the css doesn't load with www either.

  • okay, I just wanted to confirm that mod-rewrite was working...no need to keep that rule there.

    mod-rewrite is certainly working for your site. so now the trick is to trouble-shoot your different rules to get the ones working.

    What platform are you using?

  • @sierracircle got it working by manually adding the following codes:

    RewriteRule about about.php
    RewriteRule contact contact.php
    

    Is there a way to make it simpler or automatic? I am using a LAMP stack on Ubuntu 14.04.

  • This bit works for me:

    
    # 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>
    
  • @sierracircle that does remove the file extensions but my css file doesn't load.

  • can you give me a link to your site you are working on?

    Is your css in its own folder?

  • @sierracircle it's http://sadmansh.com. The css file is in assets/css/style.css.

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.