apachemod-rewriterewritemap

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?


Solution

  • 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.