Westley Knight
Westley Knight

Reputation: 25

Replacing a specific string in htaccess using mod_rewrite

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

Upvotes: 1

Views: 1232

Answers (2)

benlumley
benlumley

Reputation: 11382

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.

Upvotes: 1

Fabian Schmengler
Fabian Schmengler

Reputation: 24576

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]

Upvotes: 2

Related Questions