Skip to content

Instantly share code, notes, and snippets.

@mobeigi
Last active August 20, 2024 10:32
Show Gist options
  • Save mobeigi/54d7914f80f64300e00824289ac39c76 to your computer and use it in GitHub Desktop.
Save mobeigi/54d7914f80f64300e00824289ac39c76 to your computer and use it in GitHub Desktop.
Mo Beigi's Website URL Structure Preference

Mo Beigi's Website URL Structure Preference

Websites normally employ several redirects to avoid duplicate content pages etc for SEO purposes. The following is the behaviour I found easiest to setup and most consistently used on the web. It also aligns well with the behaviour popular tools like nginx / apache / wordpress. The key is consistency.

HTTP to HTTPS (use HTTPs only)

http://(.*) -> 301 https://$1

Exceptions can be made of course for HTTP only sites sitting behind reverse proxies.

Canonical urls

Always trailing slash for root

<link rel="canonical" href="https://example.com/" />

Always trailing slash for folders

<link rel="canonical" href="https://example.com/images/" />
<link rel="canonical" href="https://example.com/blog/life/how-i-turned-water-into-gold/" />

Redirects

Avoid unneccessary hops

Always try to redirect in 1 hop if possible. Make sure you never hop to HTTP and then back to HTTPS! (common mistake with reverse proxy setups).

Always trailing slash for folders

https://example.com/image -> 301 https://example.com/images/
https://example.com/blog/life/how-i-turned-water-into-gold -> 301 https://example.com/blog/life/how-i-turned-water-into-gold/

No trailing slash for resources (i.e. static files)

https://example.com/image/logo.png?v=1 200
https://example.com/image/info.txt 200

Always preserve query strings

https://example.com/home/?lang=en 200
https://example.com/home?lang=en -> 301 https://example.com/home/?lang=en

Always preserve fragments

Nothing to do here, browser will auto handle this for us.

No trailing slash for resources that are not a folder or resource

https://example.com/thisdoesntexist 404 (no redirect hop to trailing slash)

No trailing slash for API endpoints

https://example.com/api/user/1 200
https://example.com/api/user/1/preferences 200

https://example.com/api/user/1/ 404 (should be no trailing slash!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment