Using mod_write with Lets Encrypt

October 4, 2016 135 views
Apache Ubuntu

Hi, I have enabled Let's Encrypt on Ubuntu 14.04 and used the auto option to re-direct all http requests to https. This is working fine.

However, I now want to use mod_rewrite to use cleanurls on my site - all I need to do is remove the .php extension from all filenames.

I have tried adding the following rewrite rule to my .htaccess file:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

This works fine on my (MAMP) localhost but does not work on the instance running Lets Encrypt.

I have tested that the .htaccess is working by adding this rule which works as expected (redirecting all www requests to the root domain)

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

I suspect that there may be some conflict between the Lets Encrypt auto setup option and my mod_rewrite rule but I am stuck as to how to make them both work together.

Any help much appreciated.

2 Answers

Generally I would first recommend making sure that you had done a a2enmod rewrite, restarted apache and made sure you had set AllowOverride All in your apache configuration but since you've confirmed that rewrite is working I would suggest trying this slightly different version of the rewrite rule.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

When using an .htaccess file it will take precedence over rewrites specified in your configuration when AllowOverride is set to All so it's unlikely that the letsencrypt changes are directly responsible.

Thanks ryanpq. I tried using this alternative rule, but it had no effect. In the meantime however I have discovered that the cause of the problem was a conflict with MultiViews being enabled on the server (I was not previously aware of the existence of MultiViews).

In order to fix this particular issue I added the following line to the .htaccess file

Options -MultiViews

Posting the answer here in case it helps others.

Have another answer? Share your knowledge.