Brian D.
Brian D.

Reputation: 184

htaccess issue with query strings

There was an existing site with a flat file structure with tons of urls with query strings galore. My issue is I wrote a CMS and have setup a custom error code page that uses a query string like:

ErrorDocument 404 /error.php?errorcode=404

The issue with this is if the old broken url has a query string this overwrites the query string in the errordocument redirct. For example if you do

www.example.com/missingpage.php - this will work and redirect to the 404 without issue.

But if you do

www.example.com/missingpage.php?anything=anything

or even

www.example.com/missingpage.php?

the errorcode=404 query string will not make it to the php page and it throws the generic message I have setup when the errorcode doesn't match. The problem is there are lots and lots of links with all kinds of crazy query strings so it is very hard to filter them out individually.

Has anyone ran into this issue before?

Upvotes: 3

Views: 924

Answers (3)

Brian D.
Brian D.

Reputation: 184

I found another option, even though it is not 100% ideal. If you use a full url in the error document definition it will do a redirect and ignore the original url query string. For instance if you do:

ErrorDocument /error.php?error=404

the bad url that the user used to access the page will remain in the address bar and your query string will be overwritten by whatever is in the address bar. but if you do

ErrorDocument http://www.google.com/error.php?error=404

Then a redirect will occur and the original "bad" url the user used will be replaced with the error document one.

Here is the caveat that you must know. This is a soft 404 error so search engines might hate your guts.

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143906

Something else that you can do is to use mod_rewrite and append the query string. I've seen this done on other setups before because the ErrorDocument target gets put back through the URL mapping engine (at least in apache 2.4).

For example:

ErrorDocument 404 /error-doc-404
ErrorDocument 405 /error-doc-405
ErrorDocument 500 /error-doc-500

And then you have this:

RewriteEngine On
RewriteRule ^error-doc-([0-9]{3})$ /error.php?errorcode=$1 [L]

This way, the url on the address bar does not get changed.

Upvotes: 3

Madara's Ghost
Madara's Ghost

Reputation: 175007

Simple solution, make a specific 404.PHP page. I have an entire directory in my project for all of these custom error pages.

Upvotes: 0

Related Questions