Apache - remove .php and .html file extensions using mod_rewrite in httpd.conf

February 21, 2017 18k views
Apache PHP Ubuntu

Running Apache 2 on Ubuntu 14.04. rewrite_module is enabled (sudo apachectl -M).

In /etc/apache2/apache2.conf (the Ubuntu version of httpd.conf) I have the following code block:

<Directory /var/www/>
    <IfModule mod_rewrite.c>
        RewriteEngine On

        RewriteCond /%{REQUEST_FILENAME}.php -f
        RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

        RewriteCond /%{REQUEST_FILENAME}.html -f
        RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.html
    </IfModule>

    <IfModule mod_expires.c>
        ...

        <IfModule mod_headers.c>
            ...
        </IfModule>
    </IfModule>
</Directory>

Ran sudo service apache2 restart.

When I visit a url on my server without the .php file extension, I get a 404! Why isn't this working?

4 Answers

Try this instead:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
  • That worked, thank you! Is there a way I could force URLs with a slash at the end (ie. http://example.com/page/) to also forward to the correct php/html file?

    • I would probably strip the slash with a redirect, but I haven't messed around with Apache for a long time - I'm using Nginx.

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)/$ $1 [R,L]
      RewriteCond %{REQUEST_FILENAME}.php -f
      RewriteRule (.*) $1.php [L]
      RewriteCond %{REQUEST_FILENAME}.html -f
      RewriteRule (.*) $1.html [L]
      
      • Thanks for this!!
        This is working great, however now my redirects are not working.
        For example I have my main website: brandileath.com
        and I have a few domains redirecting to this domain, but after inserting this code they are returning 404 errors.
        Please help! Thank you!!!!

  • thanks for the question and answer. I'm dumb i was working in the wrong conf file and couldn't figure out why it didn't work

Hi there,

Can anyone help that how to redirect example.com/abc instead of example.com/abc.php?
Please let me know as soon as possible.

Thanks

PLEASE HELP WITH THIS GUYS

<VirtualHost *:80>
        ServerAdmin www.calvano.com
        ServerName calvano.com
        ServerAlias www.calvano.com
        DocumentRoot "C:/project/htdocs/amado"
        <Directory />
                Options FollowSymLinks
                AllowOverride All
                <IfModule mod_rewrite.c>
                    RewriteEngine On

                    RewriteCond %(REQUEST_FILENAME) -d
                    RewriteCond %{REQUEST_FILENAME}.php -f
                    RewriteRule (.*) $1.php [L]
                    RewriteCond %{REQUEST_FILENAME}.html -f
                    RewriteRule (.*) $1.html [L]
                                        RewriteCond %(REQUEST_FILENAME) !-f

                                        RewriteRule ^furniture/([a-z0-9]+)$ furniture/item-detail.php?varton=$1 [L]
                </IfModule>
        </Directory>

</VirtualHost>
Have another answer? Share your knowledge.