My name's Jay and I'm a web designer & developer from High Wycombe in the UK.
(I'm also a keen skateboarder and golf enthusiast)
Categories
Blogroll & Links
Viewing | all articles in Server Config
Published 10 months ago under Server Config, Regular Expressions
Mod_rewrite is a very handy tool for the SEO and UX conscious web developer. It allows us to map pretty and legible web addresses onto not-so-pretty ones, and allows us to construct the query string using regular expressions.
For example:
http://www.example.com/films/horror/2008
can map to:
http://www.example.com/view.php?media=films&genre=horror&year=2008
with this mod_rewrite rule in the .htaccess:
1: RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ view.php?media=$1&genre=$2&year=$3
My problem has always been that once I'd used mod_rewrite on a URL, I couldn't add extra query string variables onto it, as they wouldn't be passed through the rule:
http://www.example.com/films/horror/2008?orderby=price
(the variable $_GET['orderby'] is not available on the view.php page)
My inelegant solution was to use $_SERVER['REQUEST_URI'] and split the URL with ? as the delimiter, thereby accessing whatever preceeded it.
The actual solution to the problem is extremely simple though. All that is needed is to add &%{QUERY_STRING} to the end of the mod_rewrite rule, and any extra query string variables are handled correctly:
1: RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)/?$ view.php?media=$1&genre=$2&year=$3&%{QUERY_STRING}
Post a comment