Forum Moderators: phranque

Message Too Old, No Replies

404 for specific $ GET Variable

         

lorus

3:47 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



Hi,

I want my apache to return a 404 error, when my index.php is called with a specific $_GET Variable.

For example:
[mysite.com...] --> return 404
[mysite.com...] --> do nothing

How can I do this with .htacces ?

Thanks for your help :-)


lorus

sublime1

6:22 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



Hi Lorus --

If you want to do this using an apache .htaccess...

You'll need to create a test for the condition you want to 404, very likely using a RewriteCond directive. Check the value in %{THE_REQUEST} -- check this forum for example of the pattern to look for and an appropriate regular expression.

It it matches, create a RewriteRule that sends the user to whatever page on your site you use for 404 errors now. This will be an internal rewrite, and it should be the last RewriteRule processed -- use the [L] flag for this.

But you show a $_GET variable, one of PHP's global variables -- if you can change the PHP code you can do this simply without messing with .htaccess -- if you're not familiar with .htaccess and rewrite rules then changing the PHP is almost certainly the better option.

Both will work.

Tom

lorus

6:46 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



Hi thanks for your reply,

yeah it must be a solution with .htaccess
Of course I searched the forum and google but I don't found a snippet, that fits to my specific needs.

jdMorgan

6:48 pm on Oct 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Apache 1.x or 2.x :

# Rewrite requests for index page with unwanted query to non-existent filepath, forcing a 404 response
RewriteCond %{QUERY_STRING} ^foo=bar$
RewriteRule ^(index\.php)?$ /some-arbitrarily-named-filepath-that-you-know-does-not-exist.html [L]

Apache 2.x only :

# Force 404 response for requests for index page with unwanted query
RewriteCond %{QUERY_STRING} ^foo=bar$
RewriteRule ^(index\.php)?$ - [R=404,L]

Note that in both cases, only an exact match on the "foo=bar" query string will result in a 404. If the query string contains additional name/value pairs, then the rule will not be invoked. To handle this case as well, change the RewriteCond pattern to
 ^([^&]*&)*foo=bar(&.*)?$ 


Using that pattern, any query string that *contains* the name/value pair "foo=bar" will invoke the rule, but the pattern is still unambiguous enough to avoid invoking a 404 on a query like "afoo=bar" or "foo=barn" which would be a potentially-hard-to-find problem with a completely-un-anchored pattern like "foo=bar".

The parenthesized ^(index\.php)?$ subpattern with the match-zero-or-one-time "?" quantifier in the RewriteRule pattern allows for invoking a 404 on either
example.com/index.php?foo=bar
or
example.com/?foo=bar

Either of which would usually be possible ways of accessing /index.php if it represents your "home page" or is the script used to generate all of your pages, including your home page. (Note that although a client request for "example.com/index\.php" should be redirected to the canonical "example.com/" URL, it is generally recommend that you put access control rules like the one here above the canonicalization redirects - there is no use wasting time telling an unwelcome visitor your canonical URLs, so generally you want the access control rules first, redirects next, and internal rewrites last.

Jim

lorus

7:11 pm on Oct 25, 2010 (gmt 0)

10+ Year Member



Big thanks, this works like a charm :)