Pardoner
Pardoner

Reputation: 1111

If rewrite map doesn't exist got to 404

I've created a rewrite map that's working well:

.htaccess

RewriteRule ^([^/\.]+)/?$ index.php?page=${mymap:$1} [L]

mymap.txt

home 1
about 2
contact 3

So www.myurl/home works fine, but www.myurl/nonexistant_page doesn't exist in mymap.txt so I get an SQL error. Is there a way to check if the map exist and if not got to 404 page?

Upvotes: 1

Views: 681

Answers (2)

M'vy
M'vy

Reputation: 5774

You can specify a default value when there is no correspondence between key and values in your map.

${mymap:$1|default_value}

Then you can use this default value to trigger the 404 call in your index.php page by testing the query_string key called page.

By default, the default_value is the empty string. That may explain why you get an SQL error if your index.php page makes an SQL query with empty argument.

Upvotes: 3

noevidenz
noevidenz

Reputation: 160

I can't stress how important it is that you don't just slap the page ID into the end of your SQL query without sanitizing it first.

Anyway, perhaps surround your SQL query with a try/catch and send a 404 header in the event that an SQL Exception is caught.

Upvotes: 1

Related Questions