1

The issue I have is that I need to match and replace a specific string in the URL, not splitting at the standard point of the querystring variables, and using only the second half of the the pagevars variable to be used in the intended URL.

In this particular case I need to use .htaccess to redirect this URL:

http://example.com/details.php?pagevars=this-variable-id-WGZ8765

to this:

http://example.com/item/id-WGZ8765

2
  • Is there a pattern or do you only want to rewrite this single URL? Commented Mar 1, 2013 at 11:09
  • The only part of the URL that is subject to change is the last part after the hyphen, in this case 'WGZ8765'. Everything else in the URL will always be the same Commented Mar 1, 2013 at 11:27

2 Answers 2

2

You have to use RewriteCond to analyze the query string, any captured patterns will be available as %N in RewriteRule (just as captured patterns in the rule itself are available as $N:

RewriteCond %{QUERY_STRING} this\-variable\-id\-(.*)$
RewriteRule ^details\.php$ /item/id-%1 [L]
Sign up to request clarification or add additional context in comments.

Comments

1

Something along the lines of this should work...

RewriteCond %{QUERY_STRING} \-id\-([A-Za-z0-9]+)$
RewriteRule ^details\.php$ /item/id-%1 [L]

Hard to be certain without a bit more knowledge about what may/may not change in the original URL - it may be that you need to alter the way the regex is anchored.

1 Comment

The only part of the URL that is subject to change is the last part after the hyphen, in this case 'WGZ8765'

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.