why does the regex in my rewrite condition seem to

2019-09-05 17:38发布

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条回答
Explosion°爆炸
2楼-- · 2019-09-05 18:40

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/

查看更多
登录 后发表回答