How can I redirect example.com/username to Instagram app directly open from deep link?
.htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, apache, apache2, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :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
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
Comments
Post a Comment