skip to Main Content

I have a SPA app with dynamic content for sharing on Facebook so I am redirecting Facebook crawlers to a nice static page using the following rule in htaccess:

<IfModule mod_rewrite.c>
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit.*$
RewriteRule ^(.*)$ https://sharing.mysite.tld/api/share/$1 [L]

This works great! But there is one problem… I can’t make my app live because Facebook requires a link to privacy policy, terms and conditions etc – and these get redirected too!!

I need to ignore a certain URLs – anything requested in /docs/ – from the above rule EDIT: so that urls containing /docs/ are followed as normal (no redirect, just served normally). I can’t get .htaccess to pick up on the ignore rule. I would have thought this would do it (with thanks to https://stackoverflow.com/a/1848579/4881971):

    RewriteRule ^(docs)($|/) - [L]

so I would have thought my .htaccess file would look like this :

<IfModule mod_rewrite.c>
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit.*$
RewriteRule ^(docs)($|/) - [L]
RewriteRule ^(.*)$ https://sharing.mysite.tld/api/share/$1 [L]

but when I use Facebook Object Debugger on https://mysite.tld/docs/privacy I get a 404! It redirecting to https://sharing.mysite.tld/api/share/docs/privacy

How do I retain the rule but ignore requests from mysite.tld/docs/* ? Thanks.

2

Answers


  1. Could you please try following, please make sure you clear your browser cache before testing your URLs. This considers your uri starts from docs.

    <IfModule mod_rewrite.c>
    RewriteEngine ON
    RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit.*$ [NC]
    RewriteCond %{REQUEST_URI} ^/docs [NC]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^(.*)$ https://sharing.mysite.tld/api/share/$1 [L]
    


    In case you want to pass URLs where docs could come anywhere in uri(not from starting what 1st solution looks for), then try following Rules.

    <IfModule mod_rewrite.c>
    RewriteEngine ON
    RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit.*$ [NC]
    RewriteCond %{REQUEST_URI} docs [NC]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^(.*)$ https://sharing.mysite.tld/api/share/$1 [L]
    
    Login or Signup to reply.
  2. Have it like this with a negated comdition:

    RewriteEngine On
    
    RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit [NC]
    RewriteRule %{THE_REQUEST} !s/+docs [NC]
    RewriteRule ^ https://sharing.mysite.tld/api/share%{REQUEST_URI} [L,R=301,NE]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search