aboutsummaryrefslogtreecommitdiff
path: root/content/blog/2021-10-09-apache-redirect.org
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2024-03-29 01:42:38 -0500
committerChristian Cleberg <hello@cleberg.net>2024-03-29 01:42:38 -0500
commit00b2726e0561f174393ae600f0f11adb8afebaab (patch)
treea4733d553ce68f64277ffa3a52f800dc58ff72de /content/blog/2021-10-09-apache-redirect.org
parent8ba3d90a0f3db7e5ed29e25ff6d0c1b557ed3ca0 (diff)
parent41bd0ad58e44244fe67cb36e066d4bb68738516f (diff)
downloadcleberg.net-00b2726e0561f174393ae600f0f11adb8afebaab.tar.gz
cleberg.net-00b2726e0561f174393ae600f0f11adb8afebaab.tar.bz2
cleberg.net-00b2726e0561f174393ae600f0f11adb8afebaab.zip
merge org branch into main
Diffstat (limited to 'content/blog/2021-10-09-apache-redirect.org')
-rw-r--r--content/blog/2021-10-09-apache-redirect.org43
1 files changed, 43 insertions, 0 deletions
diff --git a/content/blog/2021-10-09-apache-redirect.org b/content/blog/2021-10-09-apache-redirect.org
new file mode 100644
index 0000000..25fb7ba
--- /dev/null
+++ b/content/blog/2021-10-09-apache-redirect.org
@@ -0,0 +1,43 @@
+#+title: Apache Redirect HTML Files to a Directory
+#+date: 2021-10-09
+#+description: A guide on redirecting HTML files to directory in Apache.
+#+filetags: :apache:
+
+* The Problem
+After recently switching static site generators (SSG), my blog URLs
+changed with no option to preserve the classic =.html= extension at the
+end of my blog post URLs.
+
+I really disliked using my old SSG ([[https://jekyllrb.com][Jekyll]])
+and prefer my new tool ([[https://www.getzola.org][Zola]]) much more, so
+I was determined to figure out a way to get the proper redirect set up
+so that people who find my posts online aren't constantly met by 404
+errors.
+
+* The Solution
+To solve this problem, I really needed to solve two pieces:
+
+1. Redirect all blog post URL requests from =/blog/some-post.html= to
+ =/blog/some-post/=.
+2. Ensure that no other =.html= files are redirected, such as
+ =index.html=.
+
+After /a lot/ of tweaking and testing, I believe I have finally found
+the solution. The solution is shown below.
+
+#+begin_src conf
+RewriteEngine On
+RewriteCond %{REQUEST_URI} !\index.html$ [NC]
+RewriteRule ^(.*).html$ https://example.com/$1 [R=301,L]
+#+end_src
+
+This piece of code in the Apache =.conf= or =.htaccess= file will do the
+following:
+
+1. Turn on the RewriteEngine so that we can modify URLs.
+2. Ignore any =index.html= files from the rule we are about to specify.
+3. Find any =.html= files within the website directory and redirect it
+ to exclude the file extension.
+4. The final piece is adding the trailing slash (=/=) at the end of the
+ URL - you'll notice that I don't have an Apache rule for that since
+ Apache handles that automatically.