I am updating a php app, which currently does not use url rewriting. The aim is to hide file extensions. The general structure of the website is as follows
root/
index.php
login.php
page1.php
page2.php
page3.php
page4.php
page5.php
page6.php
page7.php
page8.php
subfolder/
index.php
validpage.php
images/
css/
The subfolder folder in the above structure is unique in that it is the only subfolder containing php files.All of the other files are in the root folder.
I have been through these questions Mod Rewrite and PHP and Mod-Rewrite or PHP router? and mod_rewrite, php and the .htaccess file
Updated htaccess. thanks to @thomasmalt
RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l
RewriteRule ^.*$ - [NC,L]
# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s
# add .php to the url
RewriteRule ^(.*)$ $1.php
Problem now is I get a 500 instead of a 404 error, when I try to access a non existent page like localhost/inex.php. The Apache error log gives me the following errors. (I accessed localhost/index which loaded the contents of index.php , followed by which I loaded localhost/inex which gave me a 500 error.)
[Sat Sep 25 14:44:26 2010] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/index
[Sat Sep 25 14:44:36 2010] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Update: found the answer to my question here. The question is resolved.
I’m not totally sure what you’re trying to achieve, and i suspect, no disrespect intended, that you need to understand both mod_rewrite, and the way the webserver and php parses and executes files. I’m not sure how to answer without shooting over or under your level of understanding, but I’ll try.
I can tell you what you’re current .htaccess is doing: (pseudocode)
Since index.php is your php script that is executed and everything put in $1 is ignored.
That’s why index.php is executed whatever you do.
What you could try to put in your .htaccess is this:
That should take the request and try to append .php to the end of it. I haven’t tested it unfortunately, but you should give it a try. The result should be something like this:
And you should read the mod_rewrite documentation twice a day for a month, and buy the O’Reilly book about Regular Expressions.
And if you have administrator access to the development server you should look into