0

I'm using Angular 2 and just added a robots.txt to the root of my dist folder on production (for SEO purposes)

But the file isn't accessible since the url https://myrandomsite.com/robots.txt just redirects to my PageNotFoundComponent because of how my routing is setup.

How can I allow robots.txt to be accessed "normally" and prevent my Angular 2 routing config to trigger?

Edit: I am using Angular2 frontend with a Firebase backend.

Weblurk
  • 6,562
  • 18
  • 64
  • 120

1 Answers1

1

That's no Angular problem, its one of your webserver.

Your webserver shouldn't redirect an URL to an existing file back to /index.html.

See this IIS example configuration for redirecting with rules:

    <rule name="AngularJS" enabled="false" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.html" />
    </rule>

IIS won't redirect if its a file, directory or URL starts with /api!

UPDATE

Using firebase you should read this: https://firebase.google.com/docs/hosting/url-redirects-rewrites

slaesh
  • 16,659
  • 6
  • 50
  • 52
  • Ok, I have no idea how IIS works. I am using Firebase so I don't have access o a web server. I will update my post with this info. – Weblurk Feb 22 '17 at 09:56
  • Was just a hint where's the problem, shouldnt be a solution! :) See my updated answer.. – slaesh Feb 22 '17 at 10:14
  • 1
    See https://stackoverflow.com/questions/41628306/angular2-webpack-do-not-deploy-robots-txt – Joseph Simpson Nov 20 '17 at 12:16
  • Though there should be an entry in angular.json for the angular routing to be ignored. just like the way for /assets & images – Naga Jul 24 '22 at 17:55