Jump to content
mikejv

RewriteRule WITH query string

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.