Mobile app version of vmapp.org
Login or Join
Moriarity557

: How can I redirect example.com/username to Instagram app directly open from deep link? I'm trying to redirect from example.com/username​ to Instagram deep link instagram://user?username=anyusername

@Moriarity557

Posted in: #Apache #Apache2 #Deeplinks #Htaccess #Redirects

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 example.com/instagram://user?username=/instagram://user

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Moriarity557

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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=


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:

stackoverflow.com/questions/24136175/how-to-handle-mod-rewrite-with-a-custom-url-scheme



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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme