why does the regex in my rewrite condition seem to not catch?

余生长醉 提交于 2020-01-07 08:07:49

问题


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/



来源:https://stackoverflow.com/questions/19318162/why-does-the-regex-in-my-rewrite-condition-seem-to-not-catch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!