.htaccess 重定向而不更改地址栏 [英] .htaccess redirect without changing address bar
问题描述
我正在尝试编写一个 .htaccess
规则来重定向到一个脚本,该脚本进一步重定向到其他地方.有点像 URL 缩短器的工作原理.但是,我不希望地址栏在重定向的 .htaccess
部分发生变化.(脚本重定向改变位置是可以的.)
I'm trying to write an .htaccess
rule to redirect to a script, which further redirects somewhere else. Kind of like how URL shorteners work. However, I don't want the address bar to change during the .htaccess
part of the redirect. (It's okay for the script redirect to change the location.)
我正在使用 mod_rewrite
,目前正在这样做:
I'm using mod_rewrite
, currently doing this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteBase /
RewriteRule (.+)$ "?url=$1" [L,R=301]
我可以使用标志或其他方法来实现我想要做的事情吗?
Is there a flag or another method I can use to achieve what I'm trying to do?
附录:位置栏在 Firefox 中不会随着重定向的发生而改变,这正是我想要做的.它只是改变一次以反映终点.Safari 在每一步都会改变它.有什么办法可以避免吗?
Addenda: The location bar doesn't change in Firefox as the redirects are happening, which is what I want to do. It just changes once to reflect the end point. Safari changes it at every step. Any way to avoid that?
推荐答案
如果您使用 R
标志,您是在告诉 mod_rewrite
您想要的是外部重定向,因此要求浏览器发出新请求,地址栏应相应更改.
If you are using the R
flag you are telling mod_rewrite
that an external redirect is what you want, therefore the browser is asked to make a new request and the address bar should change accordingly.
如果没有 R
标志,就没有重定向,而是对浏览器隐藏的 Apache 内部请求重写.因此,地址栏不会改变.但是,出于显而易见的原因,您不能使用内部重定向到外部 URI.
Without the R
flag, there is no redirect, but an Apache-internal request rewrite which is hidden from the browser. Thus, the address bar won't change. However, you cannot use internal redirects to external URIs for obvious reasons.
由于您似乎无论如何都使用内部重定向,只需删除 R
标志,它应该可以工作:
Since you seem to use an internal redirect anyway, just remove the R
flag and it should work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)$ ?url=$1 [L]
这篇关于.htaccess 重定向而不更改地址栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!