Forum Moderators: phranque
I need to redirect links like these to a 404
.com/filename.php?page=2
.com/filenamefhj.php?page=5
.com/filenameasdf.php?page=6
.com/filenamedfg.php?page=8
The filename before the .php can be anything. They are all from the root.
Is it possible to create a Rewrite rule that would send them all to a 404? The page= is a constant.
TIA
Jim
Doing that means the page is "found", and every URL request will return "200 OK". Every URL that "doesn't exist" will be indexed by search engines, because the "200 OK" statis is saying that the URL *does* exist. Since every URL also returns the exact same content, you have just caused an Infinite Duplicate Content problem for your site.
Rewrite the request to an internal file that does NOT exist. I use the literal path /this-file-does-not-exist for example. As the rewrite then goes to an internal file path that does not exist, the server itself sends the proper "404 Not Found" response back to the browser... because what was asked for does not exist on the server.
The
RewriteRule also cannot see the query string. You need to test %{QUERY_STRING} with a preceding RewriteCond to do that.
That's controlled by the
^$ which matches only / in the root. If the request has more than one parameter then it will also not match, because the parameter must not contain an ampersand at all (according to your condition).
You don't need to escape the = sign.
RewriteEngine on
#
RewriteCond %{QUERY_STRING} &?page\=([^&]+)
RewriteRule ^[^/.]+\.php$ /no-file-exists-here.xyz [L]
Jim
Please read the details of what I wrote -- I try very hard to be concise but thorough. Mod_rewrite demands complete attention to detail.
If you have a "list" of specific .php URL-paths that should return a 404, or if my detailed description of the rule's operation does not meet your requirements exactly, then please say so.
Jim