2

My htaccess code allows example.com/username redirect to any username on Instagram app. But when I open example.com it redirects to an Instagram account called index.php. How can I fix it? Here is my htaccess

RedirectMatch 302 /(.+) instagram://user?username=$1
2
  • Do you have any other directives in your .htaccess file?
    – MrWhite
    Aug 9 '17 at 21:30
  • @MrWhite No. I don't have any other directives in my .htaccess Aug 9 '17 at 21:31
2

This is probably due to mod_dir issuing a subrequest for the directory index (ie. index.php).

You can try adding a negative lookahead to the regex to specifically exclude requests for index.php. For example:

RedirectMatch 302 /(?!index\.php)(.+) instagram://user?username=$1

UPDATE: If you are now having a problem with index.html (another possible directory index) then you can exclude this as well:

RedirectMatch 302 /(?!index\.php|index\.html)(.+) instagram://user?username=$1

Or disable directory indexes altogether and redirect everything (change .+ to .*). For example:

DirectoryIndex disabled
RedirectMatch 302 /(.*) instagram://user?username=$1

However, this will redirect even when no username is given.


To avoid conflicts with your existing site, it would probably be preferable to make this "instagram" URL unique, so it doesn't override your existing URLs. For example: example.com/instagram/<username>. You could then simplify the above directive to:

RedirectMatch 302 /instagram/(.+) instagram://user?username=$1

Or, you could even reduce it to something like example.com/i/<username> - if that is still unique.

RedirectMatch 302 /i/(.+) instagram://user?username=$1
9
  • It is now redirecting to "index.html" username of Instagram Aug 9 '17 at 21:51
  • Does index.php and/or index.html exist? Anyway, you can exclude both of these, or disable DirectoryIndex altogether. I've updated my answer.
    – MrWhite
    Aug 9 '17 at 22:06
  • Or... what do you want to happen when accessing the bare domain example.com/ (ie. no username)? Do you still want to redirect? Or serve a local file instead? I've updated my answer.
    – MrWhite
    Aug 9 '17 at 23:15
  • 1
    I just want to show hompage of my website. When I open example.com and your RedirectMatch 302 /(?!index\.php|index\.html)(.+) instagram://user?username=$1 working Aug 9 '17 at 23:19
  • 1
    oh sorry. Updated code is working. Thank you very much Aug 9 '17 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.