大网数据广告活动
网站运营与故障问答

How can I redirect example.com/username to Instagram app directly open from deep link?

· 52CE 我爱测

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] RewriteR…

问题原文

原帖链接 · 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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。