0

I have a website that uses Apple universal links for sharing app events with others. Opening an event link on a device without the app installed should display a site that lets the user download it, but it should also keep the address the same. Reason for this is that this page will also be shown when the universal link is opened inside an app such as messenger, where it will have a label that asks the user to press "open in safari". Pressing that button must the open the initial link in safari to open the app correctly.

The website looks like this:

www.website.com/event/<event_id_wildcard>

The user will then be shown the site:

www.website.com/invite

While keeping the initial link in the address bar.

I need to accomplish this using my .htaccess file, and as I do not host the server myself but rather through a webhosting service, changing any deeper settings is difficult.

I've tried to use RewriteEngine RewriteCond and RewriteRule , by looking at these questions among others without any luck:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^/event/.*$
RewriteRule ^(.*)$ http://website.no/invite [P,NC,QSA]

This is a rather pressing issue and I have not had the time to understand the various flags

Redirect wildcard subdomains to subdirectory, without changing URL in address bar

How can I use htaccess to redirect paths with a wildcard character

https://gist.github.com/ScottPhillips/1721489

** Update **

Full .htaccess:

# Begin

RewriteEngine on

<Files "apple-app-site-association">
ForceType 'application/json'
</Files>

# Handle wildcard links
#RedirectMatch 301 ^/event/.*$ http://www.website.no/invite
RewriteRule ^event/[\w-]+/?$ invite [L,NC]

# Enforce SSL
# Handle non-www URLs
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^website\.no [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle www URLs
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.website\.no [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle URLs without .html extension
RewriteCond %{REQUEST_METHOD} ^(GET) [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.*) $1.html [L]

# End
Erik
  • 2,500
  • 6
  • 28
  • 49
  • What does `/invite` have to do with `/linked`? And how should whatever logic you are implementing _guess_ the event id if you do not specify it in the target URL you want to internally rewrite to? – arkascha Aug 27 '20 at 16:35
  • This reads as if you have not taken a single look at the documentation of the tool you want to use but just look around. _That won't work_ ... Go, read the documentation of the apache rewriting module. As typical for OpenSource software it is of _excellent_ quality and comes with great examples: http://httpd.apache.org/docs/current/mod/mod_rewrite.html – arkascha Aug 27 '20 at 16:36
  • 1
    And... this _may_ be a "pressing" issue for you. But please understand that it is not pressing for us because of that. We are here to answer qualified questions. We are not a free code writing service, not a replacement for basic tutorials or documentations. If Apple (why ever) makes such strict requests to their fans, then it is up to them to provide help for being able to match those requests. – arkascha Aug 27 '20 at 16:38
  • @arkascha that was a mistake - it's supposed to be `linked`. The event_id can be anything, e.g `event/43534` and then you'll be redirected to `linked`. You're right that I have gone straight for looking at possible solutions this time around, but I'll take a good look in the docs. Also I have no intention of bringing this question as "pressing" to anyone of this community. It was strictly as a remark of the various flags that I do not yet understand. I am taking the time to look into this – Erik Aug 27 '20 at 16:49
  • 1
    Thanks. I would like to point out again what I wrote about that ID, though: again: if you rewrite an incoming request with ID to some target without ID (`/linked`), then how should your logic get to now that ID? – arkascha Aug 27 '20 at 17:16
  • @arkascha That's the thing about not changing the address in the bar. So I'd show the content from `/linked` but keeping the address bar at `/event/`, so that when you tap "open in safari", hopefully it opens the original link in safari. – Erik Aug 27 '20 at 17:48
  • 1
    May be you just need this rule: `RewriteRule ^event/[\w-]+/?$ invite [L,NC]` – anubhava Aug 27 '20 at 18:08
  • @anubhava: you should add the P flag, the question is about keeping the original address in the browser bar, so a proxy is called for, not a redirect. – Nic3500 Aug 29 '20 at 04:17
  • @Nic3500: There is no redirect in my rule because there is no `R` flag. `P` (proxy) is not really needed here. – anubhava Aug 29 '20 at 10:56
  • @anubhava Your rule works great in showing the other page, but the address bar changes to the new address as well – Erik Aug 29 '20 at 11:01
  • @Nic3500 this is what I found as well, but it might prove difficult as I cannot access the httpd.conf with my web host provider – Erik Aug 29 '20 at 11:02
  • @Erik: Can you update question with your full .htacess. – anubhava Aug 29 '20 at 11:03

1 Answers1

0

Have it this way:

Begin

RewriteEngine on

<Files "apple-app-site-association">
ForceType 'application/json'
</Files>

# Enforce SSL
# Handle non-www URLs
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# Handle wildcard links
#RedirectMatch 301 ^/event/.*$ http://www.website.no/invite
RewriteRule ^event/[\w-]+/?$ invite [L,NC]

# Handle URLs without .html extension
RewriteCond %{REQUEST_METHOD} ^(GET) [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.*) $1.html [L]

# End

Make sure to clear your browser cache when you test this.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This works, but why remove the section on www urls? – Erik Aug 29 '20 at 14:58
  • After some testing here, I see that your original answer works well on all devices if I type the address to not include www. Such that https://website.no. When I include www, it appears the rule is overwritten by the rule to handle www uris – Erik Aug 29 '20 at 15:07
  • Both if your www and non-www rules are just redirecting `http -> https` so it can all be combined in a single redirect rule as in my answer. – anubhava Aug 29 '20 at 17:03