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

发布于 2024-11-02 15:26:21 字数 1324 浏览 1 评论 0原文

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

典型的传入网址/查询为:(长网址)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2024-11-09 15:26:21

从听起来来看,这应该会让您走上正确的道路:

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

希望有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文