Forum Moderators: phranque

Message Too Old, No Replies

How to Redirect Dynamic Links to 404s?

         

HoboTraveler

6:23 pm on Dec 29, 2008 (gmt 0)

10+ Year Member



Hi All,

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

jdMorgan

6:29 pm on Dec 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The key is to detect the query string (use RewriteCond) when a .php URL-path is requested, and *internally rewrite* that to a path *that you know does not exist*. You can make one up; For example, rewrite all such requests to "/non-existent-path.html" and then make sure you never actually create a file with that name.

Jim

HoboTraveler

7:15 am on Dec 30, 2008 (gmt 0)

10+ Year Member



Hello,

I created the following rule. This did not work.

RewriteEngine On
RewriteRule ^([^/\.]+).php?page=$ /404.html [L]

Anything after page= is a number.

Any ideas on how to make this work?

Thanks

g1smd

12:42 pm on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No. Don't rewrite the request to your 404 error message page.

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.

Caterham

2:07 pm on Dec 30, 2008 (gmt 0)

10+ Year Member



Drop the substitution (via - (dash) and use the
R=404
flag to issue a 404 if you're hosted on the recommended version of apache HTTP server (which is 2.2.x).

HoboTraveler

3:57 pm on Dec 30, 2008 (gmt 0)

10+ Year Member



Hi All,

I tried the following query string and it did not work.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^page\=([^&]+)$
RewriteRule ^$ /404 [L]

Btw, the 404 file in the root does not exist.

g1smd

4:14 pm on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your rule will only rewrite for example.com/?page=<variable> with no filename present before the question mark.

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.

jdMorgan

4:44 pm on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine on
#
RewriteCond %{QUERY_STRING} &?page\=([^&]+)
RewriteRule ^[^/.]+\.php$ /no-file-exists-here.xyz [L]

This rewrites only .php URL-paths requested from your root directory which have "page=" followed by a non-blank value in the query string to a filepath which does not exist, thus forcing a 404 error response. This should work on all versions of Apache that support mod_rewrite.

Jim

HoboTraveler

4:46 pm on Dec 30, 2008 (gmt 0)

10+ Year Member



How do I add in the parameter that would add the filename?

All filenames end in a .php and they are all different from each other. All files are in the root.

g1smd

4:56 pm on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unclear question.

Explain using example.com example URLs.

Add to what?

HoboTraveler

5:17 pm on Dec 30, 2008 (gmt 0)

10+ Year Member



Thank You jdMorgan!

That works perfect!

jdMorgan

5:23 pm on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code I posted does exactly what I wrote -- all .php URLs requested from root with page=<something> in the query string will return a 404. The rule won't affect any other URLs, .php URLs without that specific query string, or with that query string, but missing a value.

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

jdMorgan

5:24 pm on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Bit of cross-posting here, apparently.

Glad it worked for you.

Jim