.htaccess 重定向而不更改地址栏

编程入门 行业动态 更新时间:2024-10-23 15:33:12
本文介绍了.htaccess 重定向而不更改地址栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试编写一个 .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 重定向而不更改地址栏

本文发布于:2023-10-31 12:56:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   地址栏   重定向   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!