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.

5
  • For starters is RewriteEngine On? This looks like a nice write up on the topic: alexcican.com/post/… Commented Apr 28, 2015 at 21:08
  • I added that above the RewriteRule, and still the same thing. I'll check out that link :) Commented Apr 28, 2015 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? Commented Apr 28, 2015 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. Commented Apr 28, 2015 at 21:13
  • also see stackoverflow.com/questions/4908122/… Commented Apr 28, 2015 at 21:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.