1

I am trying to use Apache mod_rewrite to achieve the following:

In the document root, I have these files (for example):

.htaccess
index.php
dlscript.php

I want /downloads/this_is_path_info to be rewritten to /dlscript.php/this_is_path_info, so:

RewriteRule ^downloads$ downloads/ [R=301,L]
RewriteRule ^downloads/(.*) dlscript.php/$1 [NE,L]

The above does that.

Yet, I want the request to file /dlscript.php returns a HTTP 404 code, so I try:

RewriteRule ^dlscript.php - [R=404,L]

But this also 404 /downloads/. I've tried adding RewriteCond %{REQUEST_URI} /dlscript.php, but it seems mod_rewrite replaces %{REQUEST_URI} with the rewritten one.

How could I block direct access to dlscript.php yet enabling the other rewrites?

EDIT:

Thanks to Jon Lin I've come up with this idea:

RewriteCond %{REQUEST_URI} ^/dlscript.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /dlscript.php
RewriteRule .* - [R=404,L]

And this worked. The first rule is to prevent problem in ErrorDocument.

But I wonder if this should be considered as a "hack"? Any other appropriate method? Let's say I also want to ErrorDocument /error.php and makes /error.php returns 404, other methods instead of handling it inside the php script itself?

Alvin Wong
  • 12,210
  • 5
  • 51
  • 77

1 Answers1

0

Try this:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /dlscript.php
RewriteRule . - [R=404,L]

Matching against %{THE_REQUEST} ensures that you aren't matching against an internally rewritten URI.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • This inspire me to this: `RewriteCond %{REQUEST_URI} ^/dlscript.php` `RewriteCond %{THE_REQUEST} ^(GET|POST|TRACE)\ /dlscript.php` `RewriteRule .* - [R=404,L]` And this worked. But I wonder if this should be considered as a "hack"? (P.S. the extra rule is to prevent problem in ErrorDocument) – Alvin Wong Jun 19 '12 at 07:08