0

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.

ahmedg
  • 309
  • 1
  • 2
  • 12
Jon Cinnamon
  • 129
  • 1
  • 7
  • For starters is `RewriteEngine On`? This looks like a nice write up on the topic: http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ – ficuscr Apr 28 '15 at 21:08
  • I added that above the RewriteRule, and still the same thing. I'll check out that link :) – Jon Cinnamon Apr 28 '15 at 21:10
  • Hmm, okay. So I just tried what the article suggested and it didn't work, but... I have other projects in my server folder too, and I tried those and they worked. I think it might be because the project has a dot in the folder name? – Jon Cinnamon Apr 28 '15 at 21:12
  • Yup! Perhaps an important detail I overlooked, because the projects folder had a dot in the name, it must've gotten confused. – Jon Cinnamon Apr 28 '15 at 21:13
  • also see http://stackoverflow.com/questions/4908122/removing-the-php-extension-with-mod-rewrite – Beau Bouchard Apr 28 '15 at 21:44

1 Answers1

1

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.

ficuscr
  • 6,975
  • 2
  • 32
  • 52