Question

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

  • Posted December 26, 2014

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.

Subscribe
Share

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

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

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

Is your css in its own folder?

@sierracircle that does remove the file extensions but my css file doesn’t load.

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

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

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

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

For anyone else looking for an answer here because their rules for permalinks don’t work, remember to disable Multiviews on top of your htaccess file:

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

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

This comment has been deleted