Question

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

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?


Submit an answer

This textbox defaults to using Markdown to format your answer.

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

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Try this instead:

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

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>