阿帕奇/ Linux的 - 的.htaccess如何从一个查询/ URL获得特定的变量,并适用于重写 [英] Apache / Linux - .htaccess how to get specific variable from a query / url and apply to rewrite

查看:101
本文介绍了阿帕奇/ Linux的 - 的.htaccess如何从一个查询/ URL获得特定的变量,并适用于重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有工作的一个方向的规则,但不是其他。

I have a rule that works for one "direction" but, not the other.

一个典型的外来URL /查询应为:(长URL)

A typical incoming url / query would be: (long url)

http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (可能高达9位数字)

http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (could be up to 9 digits)

我在我的htaccess的文件在地方的规则:

I have this rule in place in my htaccess file:

RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]

这对于一个有点容易获得唯一的零件号码之一的伟大工程:

Which works great for a bit of ease getting one of the unique part numbers:

http://somedomain.com/1234567.

不过,我想提出的长URLpretty的,因此,我认为我可以扭转(ISH)吧。

However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.

所以,当一个网站上的链接被点击(长网址)htaccess的文件将处理长URL的美化版本。

So, when a link on the site is clicked on (the long url) the htaccess file would process the long url to the beautified version.

我尝试了许多尝试。 这是我最新的失败。

I tried MANY attempts. Here was my latest failure.

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)

我已经尝试了大量的规则,并参观了许多网站的意见。 我想只有规则,公正的条件和QUERY_STRING的变化。

I have tried a plethora of rules and visited many sites for advice. I tried with just rules, just conditions and variations of query_string.

所以,我相信我一定只是抓住从查询partnum和重写/ 1234567或HTTP_HOST / 1234567

So, I believe I must just grab the "partnum" from the query and rewrite to /1234567 or http_host/1234567

然后,让其他的规则(作品)来处理。

Then, allow the other rule (works) to process.

所以两者:

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

http://somedomain.com/1234567

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

和双方通过整个查询到 getme.pl 脚本正常。

and both passed the whole query to the getme.pl script properly.

我在这里找到了一些密切的答案,但是,没有真正解释了什么是我需要的。

I found some close answers here but, none that really explained what I needed.

有人可以帮忙吗?

推荐答案

从它的声音,这应该让你感动沿着正确的路径:

From the sounds of it, this should get you moving down the right path:

# 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]

希望有所帮助。

Hope that helps.

这篇关于阿帕奇/ Linux的 - 的.htaccess如何从一个查询/ URL获得特定的变量,并适用于重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆