Rewriting and redirecting URLs in Nginx

in Nginx configuration Tags: Nginx

Hypernode makes use of Nginx. Nginx does not use .htaccess files like Apache does. This means that configuration previously done in .htaccess files now has to be done in a different format, explained in the nginx documentation.

Generally you want rewrite rules if you have moved (parts of) your site to a different folder or URL.

The rewrite rules below should be added to a file called server.rewrites in the nginx folder on your Hypernode account.

Internal rewrites

They are used when you want to rewrite within the same domain. For example, to rewrite all URLs in the format of /invoice/345232.pdf to /invoice.php?id=345232 you would use this rewrite line:

rewrite ^/invoice/(\d+).pdf$ /invoice.php?id=$1 break;

This will handle the rewrite internally, so it is transparent for browsers and search engines.

External redirects

If you have moved content between domains, or want to signal (for example to search engines) that something has moved, you should use an external redirect.

For example, you want every URL starting with /fr to redirect to

rewrite ^/fr/(.*)$$1 permanent;

This will also maintain subfolders and query strings (such as

If the move is only temporary, you should use redirect instead of permanent.


See also