mikejv Posted July 15, 2019 Share Posted July 15, 2019 Hi all, I hope I explain this well enough - my apologies for any confusion. My website uses a pretty basic "page and ID" system, and uses the following RewriteRule RewriteRule ^(.*?)/(.*?)/?$ view.php?page=$1&id=$2 So mysite.com/story/33 is actually pointing at mysite.com/view.php?page=story&id=33 The issue is, sometimes a page will have an extra query string; example mysite.com/story/33?code=123456 I can't seem to access "code" in view.php (print_r( $_GET ) only gives me "page" and "id"), and I don't know how to write a RewriteRule to put "code" in view.php?page=$1&id=$2&code=$3? Any help would be appreciated - thanks! Quote Link to comment https://forums.phpfreaks.com/topic/308969-rewriterule-with-query-string/ Share on other sites More sharing options...
requinix Posted July 15, 2019 Share Posted July 15, 2019 The mod_rewrite docs list a number of flags that can be passed to RewriteRule. Like typically you should be using [L]ast to stop further rule processing, but it's not always required. In your case you would want [QSA] - query string append. Because normally if you set your own query string in the replacement URL then mod_rewrite won't carry over any that were in the original source. But if someone puts a ?page= or ?id= in the original URL, as in /story/33?page=1, then that will overwrite the one you're setting through the replacement. So I would avoid [QSA] in general and instead make use of the fact that PHP only cares about the last value: RewriteRule ^(.*?)/(.*?)/?$ view.php?%{QUERY_STRING}&page=$1&id=$2 Quote Link to comment https://forums.phpfreaks.com/topic/308969-rewriterule-with-query-string/#findComment-1568401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.