Forum Moderators: phranque
it must be re-written to:
http://example.com/dir/file.php?query_string
But, if a client request a .php file, the server must responce with a 404 error.
So I did this on the .htaccess file:
RewriteEngine On
RewriteRule ^([^\.\?]+)(\?.*)?$ $1.php$2
which add the php extension to URLs without file extension.
And to retrieve a 404 error for php request I tried this:
RedirectMatch 404 \.php$
Both directives works fine in the absence of each other. But if I put both of them in the .htaccess file it doesn't work.
How could I get the desired effect?
Thanks in advance.
RewriteCond %{THE_REQUEST} !test-dirŠtest-dir2
RewriteCond %{THE_REQUEST} \.php
RewriteRule \.php - [F]
The issue you are running into is you need to only compare original requests, not rewritten requests. THE_REQUEST is the only way to do this.
Justin
To achieve a 404 error, change the - [F] to a [R=301] redirect to a non-existant page on your site.
This is for the .php block only.
It says that REQUEST_URI is only the resource part of the whole request line THE_REQUEST.
So I did this:
RewriteRule ^([^\.\?]+)(\?.*)?$ $1.php [L]
RewriteCond %{REQUEST_URI} \.php(\?.*)?$
RewriteRule .* /error [R]
But this doesn't work. It enters in an infinite loop.
whereas this works fine:
RewriteRule ^([^\.\?]+)(\?.*)?$ $1.php [L]
RewriteCond %{THE_REQUEST} \.php(\?.*)?
RewriteRule .* /error [R]
Could the documentation be wrong?
%THE_REQUEST keep the original request all the time, but %REQUEST_URI does not. Although the documentation says that REQUEST_URI is just a sub-string of THE_REQUEST.
THE_REQUEST is always the literal request header from the client, and won't change until it is discarded when this current HTTP is completed. But REQUEST_URI is updated after any rewrite *within* the context of the current HTTP request.
Also, REQUEST_URI as defined by mod_rewrite differs in scope from REQUEST_URI in a script environment; In mod_rewrite, REQUEST_URI does not contain any query string data appended to the URL, whereas in CGI scripts, it does.
The documentation is right far more than it is wrong. But it is a necessarily brief description of a complex subject, just as the manual that comes with a new automobile does not contain full driving lessons... :)
Jim
I noticed that when a RewriteRule is used, then all the RewriteRule directives are matched again for the new rewritten URL.
It's like the iteration through the rules is repeated if there is matching rules.
I haven't tested it much, but I conclude that from a test that entered in an infite loop (not the one above).
Anyway, would there be somewhere a more detailed documentation about mod_rewrite?