1

I'm using the following rewrite rule in my .htaccess file, but whenever i add parameters to the string it makes the rewrite fail

RewriteRule ^c([0-9]+)/[a-zA-z0-9\-]+$ category.php?id=$1 [L]

For example:

If i go to the following, all works fine:

c87/newest-post

But if i go to:

c87/newest-post?param1=this&param2=that
c87/newest-post/?param1=this&param2=that

it redirects incorrectly and fails. I think it has something to do with the QSA tag, but with my poor knowledge of redirects i cant see what I'm doing wrong.

1 Answer 1

1

The QSA flag is required since you have specified a query string in the replacement URL:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

For the trailing slash you need an extra tweak. The suggested RewriteRule is:

RewriteRule ^c([0-9]+)/[a-zA-z0-9\-]+/?$ category.php?id=$1 [QSA,L]
# c87/newest-post?param1=this&param2=that  -> category.php?id=87&param1=this&param2=that
# c87/newest-post/?param1=this&param2=that -> category.php?id=87&param1=this&param2=that
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.