Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond and RewriteRule question

mod_rewrite

         

josefinita

6:51 pm on Apr 20, 2010 (gmt 0)

10+ Year Member



Hi,
I would like to do this:

- go to http://www.example.com/?content=test
- check if http://www.example.com/test is a valid directory
- if not, then redirect to another page

This is my attempt, but doesn't work:

RewriteCond %{QUERY_STRING} ^content=(.+)$
RewriteCond ^/example/${1}/ !-d
RewriteRule ^(.)+$ http://www.example.com/error.php [R,L]

How can I resolve this?
Thanks

jdMorgan

7:34 pm on Apr 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "-d" and "-f" functions test a filepath, so you must provide a valid one.

If you wished to match the URL-path consisting only of "/" then the pattern in your rule won't ever match.

You can invoke the standard 404 handler and return a proper 404-Not Found response by internally rewriting to a filepath that is known not to exist, and will never exist. This is recommended if search rankings on this site are important to you.

RewriteCond %{QUERY_STRING} ^content=([^&]+)
RewriteCond %{DOCUMENT_ROOT}/%1 !-d
RewriteRule ^$ /path-to-file-that-will-not-ever-exist.xyz [L]

Jim

josefinita

8:55 am on Apr 21, 2010 (gmt 0)

10+ Year Member



Thanks Jim, the problem I still have is I go to http://www.example.com/error.php?content=test but I would like to remove the query string from the end as well, is this possible?

josefinita

12:52 pm on Apr 21, 2010 (gmt 0)

10+ Year Member



Oh, got it!, just adding to the /path-to-file-that-will-not-ever-exist.xyz a question mark at the end like this:

/path-to-file-that-will-not-ever-exist.xyz?

Thanks!