问题原文
原帖链接 · Bangladesh 71 · CC BY-SA 3.0
I'm trying to redirect from example.com/username to Instagram deep link instagram://user?username=anyusername
Here is my htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)? example\.com$ [NC]
RewriteRule ^ instagram://user?username=%{REQUEST_URI} [R=301,L,NE]
But it redirects to http://example.com/instagram://user?username=/instagram://user
已采纳解答
原帖链接 · MrWhite · CC BY-SA 3.0
RewriteCond %{HTTP_HOST} ^(www\.)? example\.com$ [NC] RewriteRule ^ instagram://user?username=%{REQUEST_URI} [R=301,L,NE]
Aside: You have an erroneous space in the CondPattern in the RewriteCond directive. This will result in an immediate 500 error, so I assume this is not present in your actual code and is just (somehow) a typo in your question? It should read ^(www\.)?example\.com (removing the trailing $ allows the directive to match fully qualified host names that end in a dot).
However, as you are finding, this won't redirect as intended anyway. mod_rewrite (specifically mod_rewrite and not Apache in general) validates the scheme (if any) on the substitution. If it's not one of a predefined short list of schemes and does not start with a slash, it is seen as a relative URL. mod_rewrite then prefixes the scheme and hostname from the request, resulting in the malformed redirect you are experiencing.
(I also imagine you don't require the slash prefix on the username, which comes from the REQUEST_URI server variable?)
Instead, you could use a mod_alias RedirectMatch directive, which does not validate the scheme and sends the URL as written. For example:
RedirectMatch 302 /(.+) instagram://user?username=$1
However, this doesn't check the hostname on the request, like you were doing with mod_rewrite. Would that be a requirement? Do you have multiple domains on this one account?
Also, such URLs probably won't be recognised by desktop browsers, if that is a concern.
Reference:
See also this related question if you are having problems with the directory index:
My website keeps redirect to the "index.php" account on Instagram app
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
