Apache / Linux - .htaccess 如何从查询/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.
但是,我想让长网址“漂亮”,所以我认为我可以反转(ish)它。
因此,当单击网站上的链接(长 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)
我尝试了很多规则并访问了很多网站寻求建议。 我尝试仅使用规则、条件和 query_string 的变体。
因此,我相信我必须从查询中获取“partnum”并重写为 /1234567 或 http_host/1234567
然后,允许其他规则(有效)进行处理。
所以两者:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
并
http://somedomain.com/1234567
在浏览器中显示为:http://somedomain.com/1234567
。
并且都正确地将整个查询传递给 getme.pl
脚本。
我在这里找到了一些接近的答案,但是没有一个真正解释了我需要什么。
有人可以帮忙吗?
I have a rule that works for one "direction" but, not the other.
A typical incoming url / query would be: (long url)
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
(could be up to 9 digits)
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.
However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.
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)
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.
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.
So BOTH:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and
http://somedomain.com/1234567
Display as: http://somedomain.com/1234567
in the browser.
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.
Can someone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从听起来来看,这应该会让您走上正确的道路:
希望有所帮助。
From the sounds of it, this should get you moving down the right path:
Hope that helps.