Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

.htaccess redirect without changing address bar

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.)

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?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...