Jon Cinnamon
Jon Cinnamon

Reputation: 129

Remove php extension on localhost apache server?

I'm trying to get the .php extension to not be required when you view a php file. My current config is the following:

<VirtualHost *:80>
    DocumentRoot /var/www/server
    ServerName localhost

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    RewriteRule ^([^/\.]+)/?$ $1.php  [L,QSA]

</VirtualHost>

However, it doesn't work as I'm getting 404's when I try access a files like login.php as login, for instance. My server setup is kind of strange, in the /var/www/server is a symbolic link to another folder Dev/server/. Also, since it's localhost, I'm accessing it via localhost/project/login.php.

Upvotes: 0

Views: 4041

Answers (1)

ficuscr
ficuscr

Reputation: 7054

You are correct that regex rule looks like it would fail if the URI contained periods in the path. Which is bad since RFC does not say they are not valid.

You could try this:

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

That would result in: http://test.com/test/test.test/test.php for a request like http://test.com/test/test.test/test

I would test it more though. You might want to take a look at an .htaccess tester like this: http://htaccess.madewithlove.be/

The REQUEST_FILENAME can be swapped out for REQUEST_URI if needed in testing.

Upvotes: 1

Related Questions