Forum Moderators: phranque
Google has indexed identical copies of a page.
www.domain.com/directory/Oceania/Australia/
and
www.domain.com/directory/Oceania/Australia/?ID=462
The page with ?ID=462 should display a 404 error.
How can I set this up in my htaccess file?
# Create 404 on all root folder requests with query strings
# appended by rewriting to a file that does not exist
RewriteCond %{QUERY_STRING} &?ID=
RewriteRule ^[^/]*$ /non-existent-file.hmtl [L]
# Return 404 response on all root folder
# requests with query strings appended
RewriteCond %{QUERY_STRING} &?ID=
RewriteRule ^[^/]*$ - [R=404,L]
You might do better by deciding exactly which directories should 404 if a query string is present, and which should not and then listing them. Then reduce these lists by removing all but the "common paths" from directories which should all be treated in common -- For example, if all subdirectories of "/directory/" should be 404'ed when a query string is present, then "directory/" is all you need to match in the pattern. Take the shorter or least-likely-to-change (should or should not 404) list and code for that.
It might be that simply excluding the specific path to your script directory, and perhaps the path to your Web-accessible "stats" directory would be an easy, compact solution. But you have to decide, as pretty much all hosts configure their servers differently.
If you read our Forum Charter [webmasterworld.com], you'll find that while we're happy to get you started with examples or to help you to fix a *difficult* bug, we cannot write your code for you. Please check out the documents cited in that charter, using them to decipher the examples above, and see if you can help us help you...
Thanks,
Jim
I have tested what I thought would work, but I am having trouble getting there:
rewriteCond %{QUERY_STRING} .
rewriteRule (.*) http://www.example.com/$1? [R=301,L]
I know I don't have it right yet, and I am probably not putting it in the right order with respect to my other redirects. Any suggestions?
Thanks. Works great!
I was going to say something ...
You should have, then I would have picked up the error rightaway. :)
I had meant to post this:
RewriteCond %{QUERY_STRING} &?ID=
RewriteRule .* /does-not-exist [L] but the conversation has moved on a bit since then.
On Apache 1.x or 2.x
# Create 404 on all root folder requests with query strings
# appended by rewriting to a file that does not exist
RewriteCond %{QUERY_STRING} &?ID=
RewriteRule ^[^/]*$ /non-existent-file.hmt[b]l?[/b] [L]
---
crobb305,
If you get 'naked question mark' requests, the code above won't work because the question mark is a delimiter between the URL-path (or optional fragment identifier/named-anchor) and the query string. Therefore it is not visible in either the URL-path examined by RewriteRule or the %{QUERY_STRING} variable.
So in order to 'see' it, we have to look at %{THE_REQUEST} :
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]*\ HTTP/
RewriteRule ^ /non-existent-file.hmtl? [L]
Jim
# If a spurious query string delimiter and/or query string is appended
# to an otherwise-valid URL, externally redirect the request to strip
# off the query string delimiter and query (else just let it go 404).
RewriteCond $1 !^(forum/index\.php¦stats/)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]*\ HTTP/
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ http://www.example.com/$1? [R=301,L]
Note that as discussed in several recent threads, file- and directory-exists checks should always be done last in order to avoid wasting a lot of server resources.
Jim