apache - same file with .php and without .php without any mod_rewrite RewriteRule


I have installed a Apache (on ubuntu) with some vhosts. In the www-dirs I have a file (for example) called info.php. And I can open this file by /info.php AND /info (without the .php at the end).

There is no mod_rewrite rule in the Vhost or the dir.

And if I try a rewrite rule like:

RewriteRule ^info$ http://www.google.com/ [R=307,L]

and reopen /info (without the .php) it dosn't work.

When I change the role to

RewriteRule ^info.php$ http://www.google.com/ [R=307,L]

and reopen the /info it works.

And when I try this rule:

RewriteRule ^(.*)$ echo.php?value=$1 [L]

and open a site like /foo and print the $value I get "echo.php". When I try /foo/bar I get "echo.php/bar"

Does anyone have a tip for me?

Add: Here are some information from the phpinfo():

SCRIPT_FILENAME /var/www/test/echo.php 
QUERY_STRING    value=echo.php
REQUEST_URI     /foo
SCRIPT_NAME     /echo.php 



You need aRewriteCond before yourRewriteRule that matches the base case where no rewriting should occur. Otherwise your redirected request will also be rewritten.




The dollar sign marks the end of a string. You could use (untested):

RewriteRule ^info(.php)?$ http://www.google.com/ [R=307,L]



Being able to access files without needing to write extensions is caused by MultiViews options in Apache. To disable it, use:

Options -Multiviews

You can try turning it off and see if it works or not.

Alternatively, for yourecho.php, you can just use:

RewriteRule . echo.php [L]

And then haveecho.php check fromREQUEST_URI which contains the path that is originally requested.

People are also looking for solutions to the problem: php - Problem with checking GET


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.