.htaccess for SEO bots crawling single page applications without hashbangs

后端 未结 4 1679
后悔当初
后悔当初 2021-01-12 09:21

Using a pushState enabled page, normally you redirect SEO bots using the escaped_fragment convention. You can read more about that here

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 09:41

    I'm using Symfony2, and although I'm told by other devs that Googlebot and Bingbot execute Javascript well enough to generate their own HTML snippets, I don't feel confident. I also feel that serving static resources is a better alternative for ppl running with JS turned off (however unlikely that is) and so am interested in serving HTML snippets anyway, so long as it's not a hassle. Below is a method I'm thinking of using but haven't tried:

    Here are other SO questions that are similar (one is mine).
    Angularjs vs SEO vs pushState
    HTML snippets for AngularJS app that uses pushState?

    Here's a solution I posted in that question and am considering for myself in case I want to send HTML snippets to bots. This would be a solution for a Symfony2 backend:

    1. Use prerender or another service to generate static snippets of all your pages. Store them somewhere accessible by your router.
    2. In your Symfony2 routing file, create a route that matches your SPA. I have a test SPA running at localhost.com/ng-test/, so my route would look like this:

      # Adding a trailing / to this route breaks it. Not sure why.
      # This is also not formatting correctly in StackOverflow. This is yaml.
      NgTestReroute:
      ----path: /ng-test/{one}/{two}/{three}/{four}
      ----defaults:
      --------_controller: DriverSideSiteBundle:NgTest:ngTestReroute
      --------'one': null
      --------'two': null
      --------'three': null
      --------'four': null
      ----methods: [GET]

    3. In your Symfony2 controller, check user-agent to see if it's googlebot or bingbot. You should be able to do this with the code below, and then use this list to target the bots you're interested in (http://www.searchenginedictionary.com/spider-names.shtml)...

      if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot"))
      {
      // what to do
      }

    4. If your controller finds a match to a bot, send it the HTML snippet. Otherwise, as in the case with my AngularJS app, just send the user to the index page and Angular will correctly do the rest.

    Also, if your question been answered please select one so I and others can tell what worked for you.

提交回复
热议问题