How to Add and Sync a Redirects File in Publii for Cloudflare Pages

3 min read

Managing redirects and ensuring smooth updates to your website is crucial for maintaining SEO and providing a seamless user experience. This blog post will guide you through adding a _redirects file in Publii, syncing changes to GitHub, and automatically pushing updates to Cloudflare Pages.

Adding the _redirects File in Publii

Step 1: Open Publii File Manager

  • Open Publii and navigate to your site.
  • In the left sidebar, click on the “File Manager” option.

Step 2: Create the _redirects File

  • In the File Manager, locate the output directory. This is where Publii stores the generated static files.
  • Inside the output directory, create a new file named _redirects.

Step 3: Add Redirect Rules

  • Open the _redirects file you just created. Add your redirect rules. For example:
/old-page /new-page 301
/another-old-page /another-new-page 302
  • Save the _redirects file.

Important Note: This does not work on an Apache server and is specific to Cloudflare Pages

Syncing Changes to GitHub

Step 1: Configure GitHub in Publii

  • In Publii, go to the settings of your site.
  • Navigate to the “Server” tab.
  • Select “GitHub Pages” as your sync option.
  • Enter your GitHub repository details (username/repo).

Step 2: Sync Your Site to GitHub

  • Click on the “Sync” button in Publii.
  • Publii will build your site and push the changes, including the _redirects file, to your GitHub repository.

Pushing Updates to Cloudflare Pages

Step 1: Set Up Cloudflare Pages

  • Log in to your Cloudflare account and navigate to the “Pages” section.
  • Click “Create a project” and connect it to your GitHub repository where your Publii site is hosted.
  • Follow the prompts to complete the setup.
  • Add an optional custom domain.

Step 2: Automate Deployments

  • Cloudflare Pages will automatically deploy your site whenever you push changes to the connected GitHub repository.
  • Ensure that the _redirects file is included in the repository, so Cloudflare Pages can use it during deployment.

How the _redirects File Works

The _redirects file is a simple yet powerful way to manage URL redirects for static sites. Each line in the file specifies a redirect rule in the format:

/source-path /destination-path [status-code]
  • /source-path: The URL path to redirect from.
  • /destination-path: The URL path to redirect to.
  • [status-code]: The HTTP status code for the redirect (optional, defaults to 301).

Example Redirect Rules

/old-blog-post /new-blog-post 301
/outdated-page /updated-page 302

Benefits

  • SEO Management: Helps maintain search engine rankings by properly redirecting old URLs.
  • User Experience: Ensures visitors land on the correct pages, reducing 404 errors.

Further notes...

Using Cloudflare and Publii is an excellent combination for managing static websites. Adding a _redirects file within Publii simplifies the process of managing old URLs, making website migration and URL management much more efficient.