diff --git a/README.md b/README.md index 70cdbf3..cfd6e12 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Have trailing slash problems after deploying a static website in production? This repo explains factually the behavior of: - [Static Site generators](docs/Static-Site-Generators.md) - [Hosting Providers](docs/Hosting-Providers.md) +- [Self-hosted HTTP servers](docs/Self-Host.md) We also suggest some [possible solutions](docs/Solutions.md) diff --git a/docs/Self-Host.md b/docs/Self-Host.md new file mode 100644 index 0000000..cb73370 --- /dev/null +++ b/docs/Self-Host.md @@ -0,0 +1,79 @@ +# Self-hosted HTTP servers + +We will serve the [static](../static) folder using different HTTP servers. + +## Apache + +### Default settings + +The Apache server has the following configuration in `sites-available`: + +```apache + + ServerName trailing-slash-guide-apache-default.sida-chen.com + ServerAdmin webmaster@localhost + DocumentRoot /var/www/html/static + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + +``` + +**Deployment**: [trailing-slash-guide-apache-default.sida-chen.com](http://trailing-slash-guide-apache-default.sida-chen.com) + +| Url | Result | +| ------------------ | ----------- | +| /file | 💢 404 | +| /file/ | 💢 404 | +| /file.html | ✅ | +| /folder | ➡️ /folder/ | +| /folder/ | ✅ | +| /folder/index.html | ✅ | +| /both | ➡️ /both/ | +| /both/ | ✅ | +| /both.html | ✅ | +| /both/index.html | ✅ | + +### How do I... + +- ...make the `/file` path accessible? + + A: Add the following to your configuration: + + ```apache + RewriteEngine On + RewriteCond %{REQUEST_URI} !(\.[A-Za-z0-9]*$|/$) + RewriteRule ^(.*)$ $1.html + ``` + + This appends the `.html` extension to any requests to routes without trailing slash or extension. Check out [trailing-slash-guide-apache-no-extension.sida-chen.com](http://trailing-slash-guide-apache-no-extension.sida-chen.com) for this configuration in action. + + | Url | Result | + | ------------------ | ----------- | + | /file | ✅ | + | /file/ | 💢 404 | + | /file.html | ✅ | + | /folder | 💢 404 | + | /folder/ | ✅ | + | /folder/index.html | ✅ | + | /both | ✅ | + | /both/ | ✅ | + | /both.html | ✅ | + | /both/index.html | ✅ | + + Note the caveat: the `/folder` path is no longer accessible without an explict trailing slash. + +- ...enforce a trailing slash policy? + + A: + +- ...make all routes accessible? + + A: + +## Nginx + + + +## Vercel/serve + +This is a simple HTTP server run in Node.js. It has the same configuration options and implementation as [Vercel](./Hosting-Providers.md#Vercel), so read more about it there. diff --git a/static/index.html b/static/index.html index c0b6469..2607680 100644 --- a/static/index.html +++ b/static/index.html @@ -1,4 +1,7 @@ + + +