Question
Can't get mod_rewrite to work on my Ubuntu 14.04 server
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.
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.
×
Hi! Is mod_rewrite enabled? Try running the following commands:
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:
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?
Yeah @sierracircle here is the apache2.conf: http://www.codeshare.io/zJIO3
And 000-default.conf: http://www.codeshare.io/pQasV
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.
@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:
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:
disable the default site:
then enable that copy you made and restart apache.
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:
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:
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
what happens if you go to:
http://yourdomain.com/about.php
@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):
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.
@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:
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:
@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.