user1527484
user1527484

Reputation: 67

Rewrite URL: QUERY_STRING and redirect


I'm trying to rewrite url from

example.com/test/?var=somevalue or example.com/test/page1.html?var=somevalue

to

example.com/test/somevalue/

Here is the code

RewriteCond %{REQUEST_URI} test/$ 
RewriteCond %{QUERY_STRING} var=([a-z]*|[a-z]*\-[a-z]*)$
RewriteRule ^test/(.*)$ /test/%1/page1.html? [R=301,L]

This for now only works with url example.com/test/?var=somevalue and what I can't get to work is url with page[1-9][0-9]*\.html before query_string

Upvotes: 0

Views: 75

Answers (2)

Jon Lin
Jon Lin

Reputation: 143866

So something like this?

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /test/([^\?]*)\?var=([^&\ ]+)([^\ ]*)
RewriteRule ^ /test/%3/%2?%4 [L,R=301]

Upvotes: 0

FrankieTheKneeMan
FrankieTheKneeMan

Reputation: 6800

RewriteCond %{REQUEST_URI} test/(page[1-9][0-9]*\.html)?$

I think that's what you're looking for, piecing together the regex you already used with the one you described at the bottom of your query. But I'm a little shocked your rewrite rule worked at all. I would expect something more along the lines of:

RewriteRule ^test/([^/?])*\?([^&]+&)*var=([^&]+) /test/%3/page1.html

But I don't write that many RewriteRules, so I could be wrong.

Upvotes: 1

Related Questions