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?

Subscribe
Share

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.

Try this instead:

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

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>

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