That’s a lovely little trick. When writing Nginx rules in Laravel Forge, it’s easy to forget that it matches everything in the string. Consider whether you want this redirection: /product-a -> /category-1/product-a
Solution
To match the beginning of the string, add. ^
^/product-a -> /category-1/product-a
Why is this so?
curl http://mydomain.com/product-a -v
// Everything works as anticipated.
// location: http://mydomain.com/category-1/product-a
// However...
curl http://mydomain.com/category-1/product-a
// Error too many redirects
Nginx rewrite rules matches all strings and stops at the first best fragment: /product-a -> /category-1/product-a it goes for that one.
Adding a slash works, but most of the time Nginx isn’t set to add an ending slash
Bonus
If you’re using Laravel Forge and want to look at a site’s rewrite rules, here is where you’ll find them:
/etc/nginx/forge-conf/mysite.com/server/redirect_rules.conf
Notes
- Don’t modify them by hand; instead, utilize Forge.
- If your changes are not reflected, remember to restart Nginx.