skip to Main Content

I am making an inventory management site using Angular and Firebase. Because this is angular, there are problems with web crawlers, specifically Slack/Twitter/Facebook/.etc crawlers that grab meta information to display a card/tile. Angular does not do well with this.

I have a site at https://domain.io (just the example) and, because of the angular issue, I have a firebase function that created a new site that I can redirect traffic to. When it gets the request (onRequest), I can grab whatever query parameters I’ve sent it and call the DB to render the page, server-side.

So, The three examples that I need to redirect are:

From: https://domain.io/item/ABC123
To:   https://us-central1-domain.cloudfunctions.net/metaTags?item=ABC123

--

From: https://domain.io/bench/USERNAME
To:   https://us-central1-domain.cloudfunctions.net/metaTags?bench=USERNAME

--

From: https://domain.io/bench/USERNAME/ITEMTYPE
To:   https://us-central1-domain.cloudfunctions.net/metaTags?bench=USERNAME&type=ITEMTYPE

I can’t seem to get the right combination. This is what I have right now. Note: The item redirect is working, but the other two are not…

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^index.html$ - [L]

  #  If a bot goes to an Item
  RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|Slackbot|Slack-ImgProxy|Slackbot-LinkExpanding|Site Analyzer|SiteAnalyzerBot|Viber|Whatsapp|Telegram [NC,OR]
  RewriteCond %{REQUEST_URI} ^/item/
  RewriteRule ^item/(.+)$ https://us-central1-domain.cloudfunctions.net/metaTags?item=$1 [NC,L]

  #  If a bot goes to a simple bench
  RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|Slackbot|Slack-ImgProxy|Slackbot-LinkExpanding|Site Analyzer|SiteAnalyzerBot|Viber|Whatsapp|Telegram [NC,OR]
  RewriteCond %{REQUEST_URI} ^/bench/
  RewriteRule ^bench/(.+)$ https://us-central1-domain.cloudfunctions.net/metaTags?user=$1 [NC]

  #  If a bot goes to a bench of a type
  RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|Slackbot|Slack-ImgProxy|Slackbot-LinkExpanding|Site Analyzer|SiteAnalyzerBot|Viber|Whatsapp|Telegram [NC,OR]
  RewriteCond %{REQUEST_URI} ^/bench/
  RewriteRule ^bench/(.+)/(.+)$ https://us-central1-domain.cloudfunctions.net/metaTags?user=$1&type=$2 [NC]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

It’s also important to note that I would like this to work on any sub-domain (I have my dev and staging environments set up with subdomains) as well as make sure it’s always directed to https

Any thoughts?

2

Answers


    1. Use [NC,L] flags also for both bench RewriteRules
    2. Use ([^/]+) instead of (.+) in regex patterns
    3. Change [NC,OR] to [NC] in user-agent RewriteCond
    Login or Signup to reply.
  1. You can try this below code. (Not tested)

    #  If a bot goes to a simple bench
      RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|Slackbot|Slack-ImgProxy|Slackbot-LinkExpanding|Site Analyzer|SiteAnalyzerBot|Viber|Whatsapp|Telegram [NC,OR]
      RewriteCond %{REQUEST_URI} ^/bench/(.*)
      RewriteRule ^bench/(.*)$ https://us-central1-domain.cloudfunctions.net/metaTags?user=$1 [NC]
    
      #  If a bot goes to a bench of a type
      RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|Slackbot|Slack-ImgProxy|Slackbot-LinkExpanding|Site Analyzer|SiteAnalyzerBot|Viber|Whatsapp|Telegram [NC,OR]
      RewriteCond %{REQUEST_URI} ^/bench/(.*)/(.*)
      RewriteRule ^bench/(.*)/(.*)$ https://us-central1-domain.cloudfunctions.net/metaTags?user=$1&type=$2 [NC]
    

    If it doesn’t work, try [NC] with [L].

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search