why does the regex in my rewrite condition seem to

2019-09-05 17:43发布

问题:

I have URLs like the following:

mysite.com/eng?sentence=dog

and I want to rewrite them as

mysite.com/eng/dog

so I want to replace ?sentence= with a "/"

I have tried all of the following:

RewriteCond %{THE_REQUEST} ^GET\ sentence= [NC]
RewriteRule ^ /%1 [NC,L,NE]

RewriteCond %{THE_REQUEST} ^GET\ \?sentence= [NC]
RewriteRule ^ /%1 [NC,L,NE]

RewriteCond %{THE_REQUEST} ^GET\ \?sentence=([^/?&\s]+) [NC]
RewriteRule ^ /%1 [NC,L,NE]

RewriteCond %{THE_REQUEST} ^GET\s/+\?sentence=([^/?&\s]+) [NC]
RewriteRule ^ /%1 [NC,L,NE]

then I tried accessing the URL:

mysite.com/eng?sentence=cat

but it stays as it is. I assume my regex logic is not correct and the rewrite cond is never satisfied. I always have trouble with regex.

回答1:

You can use this rule:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(eng)\?sentence=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/? [L,R]

This will redirect http://mysite.com/eng?sentence=dog into http://mysite.com/eng/dog/