Forum Moderators: phranque

Message Too Old, No Replies

Hiding .php extensions

         

GetFree

10:55 pm on Dec 9, 2006 (gmt 0)

10+ Year Member



I want to convert every .php URL to the same one without the php extension.
For example, if a client request:
http://example.com/dir/file?query_string

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.

jd01

11:17 pm on Dec 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this is what I use:

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.

GetFree

7:01 am on Dec 10, 2006 (gmt 0)

10+ Year Member



How weird this mod_rewrite thing.
Here in the documentation:
[httpd.apache.org...]

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.

jdMorgan

2:48 pm on Dec 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The documentation is a bit weak on the temporal connections between variables -- Which variables are updated and when, for example. Another poorly-documented facet is which variables can contain escaped characters, and which are always un-escaped.

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

GetFree

9:54 pm on Dec 10, 2006 (gmt 0)

10+ Year Member



That's what I thought.

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?