The Power of Server-Level Redirection Redirection often happens within the application layer, but this forces the server to bootstrap the entire PHP environment and Laravel framework just to send a 301 or 302 header. This consumes memory and CPU cycles unnecessarily. By moving this logic to the Nginx level, the web server handles the request immediately. This results in faster response times for the user and less strain on your server's resources. Understanding the Rewrite Directive The core of this optimization is the `rewrite` directive. This command uses regular expressions to match incoming URIs and transform them. Unlike simple redirects, rewrites allow for complex pattern matching and data capture. ```nginx Simple exact match redirect rewrite ^/contact-us$ /contacts permanent; ``` In this snippet, the caret `^` ensures the match starts at the beginning of the URI, and the dollar sign `$` ensures it ends exactly there. This prevents accidental matches like `/company/contact-us`. Advanced Back-References and Captures You can capture parts of the original URI to reuse them in the destination. This is essential for moving entire sections of a site to a new domain or path. ```nginx Capturing sub-paths with back-references rewrite ^/laravel/(.*)$ https://laravel.com/$1 permanent; ``` The parenthesis `(.*)` create a capture group. The `$1` in the destination string acts as a placeholder, injecting whatever text was caught by the regular expression. This maintains the deep-link integrity of your URLs during a migration. Streamlining with Laravel Forge While you can manually edit configuration files, Laravel Forge provides a clean interface to manage these rules without touching the command line. Within the **Redirects** tab of your site settings, you can define the "From" pattern, the "To" destination, and the redirect type. Forge automatically validates the syntax, updates the configuration, and reloads the Nginx service to apply changes instantly. Temporary vs. Permanent Redirects Choosing the right HTTP status code is a critical SEO decision. * **Temporary (302):** Browsers do not cache these. Use them for short-term changes like seasonal promotions. If you change the destination later, the user's browser will fetch the new rule immediately. * **Permanent (311):** Browsers cache these locally. This is excellent for performance but dangerous if you make a mistake. If you change a permanent redirect, users who have already visited the link will continue to be sent to the old destination until they clear their browser cache. Syntax Notes and Best Practices Always use the `^` and `$` anchors to avoid "greedy" matching that might redirect more traffic than intended. When testing new rules, start with a **temporary** redirect. Once you verify the pattern captures exactly what you want and the back-references land in the right spot, flip it to permanent to gain the SEO and caching benefits.
Nginx
Software
Feb 2022 • 1 videos
High activity month for Nginx. Laravel among the most active voices, with 1 videos across 1 sources.
Feb 2022
- Feb 4, 2022