Apache/Linux-.htaccess如何从查询/url获取特定变量并应用于重写

Apache/Linux-.htaccess如何从查询/url获取特定变量并应用于重写,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我有一条规则适用于一个方向,但不适用于另一个方向 典型的传入url/查询是:长url http://somedomain.com/getme.pl?dothis=display&partnum=1234567 最多可以是9位数字 我的htaccess文件中有此规则: RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] 这非常有助于轻松获得一个独特的零件号: http://somedomain.com/123456

我有一条规则适用于一个方向,但不适用于另一个方向

典型的传入url/查询是:长url

http://somedomain.com/getme.pl?dothis=display&partnum=1234567 最多可以是9位数字

我的htaccess文件中有此规则:

RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]
这非常有助于轻松获得一个独特的零件号:

http://somedomain.com/1234567.
然而,我想让长url变得漂亮,所以我认为我可以反转它

因此,当在长url上单击站点上的链接时,htaccess文件会将长url处理为美化版本

我试了很多次。 这是我最近的一次失败

RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(works)
RewriteCond %{QUERY_STRING} ^partnum=([0-9]*) #(tried to get partnum)
RewriteRule ^.* http://%{HTTP_HOST}/%1 [R] #(make the short url)
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(the known working rule)
我尝试了很多规则,并访问了许多网站寻求建议。 我尝试使用规则、条件和查询字符串的变体

因此,我认为我必须从查询中获取partnum并重写到/1234567或http_host/1234567

然后,允许其他规则工作进行处理

因此,两者:

http://somedomain.com/getme.pl?dothis=display&partnum=1234567

显示为:http://somedomain.com/1234567 在浏览器中

并且都正确地将整个查询传递给getme.pl脚本

我在这里找到了一些很接近的答案,但没有一个能真正解释我需要什么


有人能帮忙吗?

听上去,这会让你走上正确的道路:

# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]

# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]

希望能有所帮助。

效果很好!非常感谢。我确实觉得奇怪,我真的需要愚弄重写,使其工作,但它确实!再次感谢!很高兴听到!是的,mod_rewrite使它有点混乱,但由于它对第一条规则使用了内部重定向,因此这些规则将再次应用于该重定向。根据我在这里学到的内容,我还尝试重写到just/1234567或我的版本,正如我们前面所做的那样。将/1234567置于上述规则之前。我失败了!!帮助
# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]

# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]