What is the best approach for redirection of old pages in Jekyll and GitHub Pages?

前端 未结 8 505
萌比男神i
萌比男神i 2020-12-07 09:38

I have blog on github pages - jekyll

What is the best way to solve url strategy migration?

I found the best practice in common is create htaccess like so

相关标签:
8条回答
  • 2020-12-07 10:04

    This solution allows you to use true HTTP redirects via .htaccess — however, nothing involving .htaccess will work on GitHub pages because they do not use Apache.

    As of May 2014 GitHub Pages supports redirects, but according to the jekyll-redirect-from Gem documentation they are still based on HTTP-REFRESH (using <meta> tags), which requires full a page load before redirection can occur.

    I don't like the <meta> approach so I whipped up a solution for anyone looking to provide real HTTP 301 redirects within an .htaccess file using Apache, which serves a pre-generated Jekyll site:


    First, add .htaccess to the include property in _config.yml

    include: [.htaccess]
    

    Next, create an .htaccess file and be sure to include YAML front matter. Those dashes are important because now Jekyll will parse the file with Liquid, Jekyll's templating language:

    ---
    ---
    DirectoryIndex index.html
    
    RewriteEngine On
    RewriteBase /
    
    ...
    

    Make sure your posts that require redirects have two properties like so:

    ---
    permalink: /my-new-path/
    original: blog/my/old/path.php
    ---
    

    Now in .htaccess, just add a loop:

    {% for post in site.categories.post %}
      RewriteRule ^{{ post.original }} {{ post.permalink }} [R=301,L]
    {% endfor %}
    

    This will dynamically generate .htaccess every time you build the site, and the include in your config file ensures that .htaccess makes it into _site directory.

    RewriteRule ^blog/my/old/path.php /my-new-path/ [R=301,L]
    

    From there, it's up to you to serve _site using Apache. I normally clone the full Jekyll repo into a non-webroot directory, then my vhost is a symlink to the _site folder:

    ln -s /path/to/my-blog/_site /var/www/vhosts/my-blog.com
    

    Tada! Now Apache can serve the _site folder from your virtual root, complete with .htaccess-powered redirects that use whichever HTTP response code you desire!

    You could even get super fancy and use a redirect property within each post's front matter to designate which redirect code to use in your .htaccess loop.

    0 讨论(0)
  • 2020-12-07 10:10

    Since github doesn't allow 301 redirects (which isn't surprising), you'll have to make a decision between moving to your new URL structure (and taking a search engine hit) or leaving the URLs the way they are. I suggest you go ahead and make the move. Let the search engine chips fall where they may. If someone hits one of your old links via the search engine, they'll be redirected to the new location. Over time, the search engines will pick up your changes.

    Something you can do to help matters is to create a Sitemap where you only list your new pages and not the old ones. This should speed up the replacement of old URLs with the new ones. Additionally, if all your old URLs are in your '/programovani' directory, you can also use a robots.txt file to tell future crawls they should ignore that directory. For example:

    User-agent: *
    Disallow: /programovani/
    

    It will take a little while for the search engines to catch up with the changes. This isn't really a big deal. As long as the old URLs still exist and redirect actual people to the active pages, you'll be fine.

    0 讨论(0)
提交回复
热议问题