• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Nginx for Newbie: setting up rewrite directive

December 18, 2023

Nginx for Newbie: Setting up Rewrite Directive

Nginx is a powerful web server and reverse proxy server that is known for its high performance, scalability, and flexibility. One of the key features of Nginx is its ability to handle URL rewriting, which allows you to modify or redirect URLs based on certain conditions. In this article, we will explore how to set up the rewrite directive in Nginx for beginners.

What is URL rewriting?

URL rewriting is the process of modifying or redirecting a URL based on specific rules. It is commonly used to create user-friendly and search engine-friendly URLs, as well as to redirect old URLs to new ones. With Nginx, you can achieve URL rewriting using the rewrite directive.

Setting up the rewrite directive

To set up the rewrite directive in Nginx, you need to modify the server block in your Nginx configuration file. The server block is where you define the configuration for a specific virtual host or website. Here’s an example of how to set up the rewrite directive:

server {
    listen 80;
    server_name example.com;

    location / {
        rewrite ^/blog/(.*)$ /articles/$1 permanent;
        rewrite ^/about$ /about-us permanent;
        rewrite ^/contact$ /contact-us permanent;
        # Add more rewrite rules here if needed
    }

    # Other server configuration directives
}

In the above example, we have defined three rewrite rules. The first rule redirects any URL starting with “/blog/” to “/articles/”. For example, “/blog/post-1” will be redirected to “/articles/post-1”. The second and third rules redirect “/about” and “/contact” to “/about-us” and “/contact-us” respectively.

It’s important to note that the rewrite directive uses regular expressions to match and modify URLs. The “^” character denotes the start of the URL, while the “$” character denotes the end of the URL. The “(.*)” part captures any characters after the matched pattern and can be referenced using the “$1” syntax in the replacement URL.

Testing the rewrite rules

After setting up the rewrite rules, it’s crucial to test them to ensure they are working as expected. You can use tools like curl or a web browser to test the URLs and check if they are being redirected correctly.

Conclusion

Nginx’s rewrite directive is a powerful tool for modifying and redirecting URLs. By understanding how to set up and use the rewrite directive, you can create user-friendly and search engine-friendly URLs, as well as redirect old URLs to new ones. Experiment with different rewrite rules to meet your specific requirements and improve the overall user experience of your website.

Summary

In conclusion, Nginx’s rewrite directive is a valuable feature for URL rewriting. By setting up the rewrite directive in your Nginx configuration file, you can modify or redirect URLs based on specific rules. This allows you to create user-friendly and search engine-friendly URLs, as well as redirect old URLs to new ones. To learn more about Nginx and its features, consider exploring Server.HK, a leading VPS hosting company that offers reliable and high-performance hosting solutions.

Recent Posts

  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)
  • VPS Hosting vs Shared Hosting: Why the Upgrade Is Worth It for Asia-Facing Websites
  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot