• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / nginx server tutorials (installation, configuration, performance tuning, security) / nginx rewrite rules with examples 301 redirection

nginx rewrite rules with examples 301 redirection

htaccess rewrite rules easy to generate, but nginx as new thing from ,2004,  many people struggles at beginning.

Table of Contents

Toggle
  • Nginx 301 reduction rewrite rule
    • nginx non www to www redirection
  • sub directory to sub domain redirection in nginx
    • rewrite vs return vs try_files
    • Return
  • nginx rewrite module
    • Nginx regex modifiers

Nginx 301 reduction rewrite rule

nginx old url to new url
generally we use wordpress plugins like redirect.
sub directory to parent directory redirection

nginx non www to www redirection

server {    listen      80;    server_name example.org;    return      301 http://www.example.org$request_uri;}
server {    listen      80;    server_name www.example.org;    # …}
One server block for redirection.
other sever block for application

sub directory to sub domain redirection in nginx

location ^~ /syllabus/ {
rewrite ^/syllabus/?(.*)$  https://syllabus.sarkariresultpro.in/$1;
}
location ^~/admit-card/ {
rewrite ^/admit-card/?(.*)$ https://admitcard.sarkariresultpro.in/$1;
}

 

location ^~ /blog {
    rewrite ^/blog/?(.*)$ https://blog.example.com/$1 permanent;
}

rewrite vs return vs try_files

try files directive
directs the application server
nginx checks directories and file
and returns the $uri.(current uri request)(URI stands for Uniform Resource Identifier)
try files based  on files index.php, index.html etc
try file $uri $uri/
if user requested web.com/text
then nginx looks text if not text/

Return

return just a simple form less programming scope less memory intensive
if pattern find just return this page.
useful to redirect old domain to new, old url to new.
server { listen 80;
server_name www.old-name.com;
return 301 $scheme://www.new-name.com$request_uri;
}
or
server{
root /var/www/html/oldsite
servername old.com
location\ {
return 301 http://google.com/$1
}
}
return 403 “service unavailable”
or
return 403;
return 3x, 1x,5x,4x codes
rewrite only 301, 302
return useful when it matches the server or location block.
for url forwardings

nginx rewrite module

when you use regex use this.
rewrite regex URL (flag);
regex (nginx has process before it’s going for server block)
nginx rewrite module,
1st process regex, and look for directives like break,last, permanent,if,return,set
flags, rewrite etc
permanent =301
last; tells nginx to stop current block and search new block(server, location)
break=
Wildcard redirection in nginx
converting htaccess rules to nginx rewrite rules
nginx variables
$scheme protocol http or https
$request_uri  full uri including arguments
The $1 and $2 variables capture the path elements that aren’t changing.
$request_uri vs $1 variables
set $someword $1 ;
custom variable ($1) that in fact is equivalent to the built‑in $request_uri variable

Nginx regex modifiers

location =/test
not applies to site.com/test/tdd/
no modifier
location /test
applies to site.com/test/,
~ operator
case-sensitive match
location ~ ^/test$
^ starts with ,
$ ends with
not applies to /TEST
~* operator (case insensitive)
location ~* ^/abcd$
applies to /TEST
^~ modifier
string should be match at beginning if the requested uri
starts with specific & case sensitive
~ or ~*  requested uri & location block should be match
location ~* ^/document$ {
[…] # requests exactly matching “/document”
location ~* /myapp/.+\.php$ { #… }
asterisk after the tilde (~*) makes the match case insensitive.
URI ending with myapp/file.php, such as /test/myapp/hello.php and /myapp/hello.php.
case‑insensitive test of the regex (.*myapp)/(.+\.php)$ against the URI /myapp/hello.php:
location ~* (.*/myapp)/(.+\.php)$ {    #…}
understanding nginx regex operators
^ matches starts with
$ matches empty string at the end
~
(.*)regex group
.* matches all
\* matches everything after \
?
rewrite ^/(.*)\.(png|jpg|gif)$
startswith/anystringgouped&endswith.(png|jpg|gif.
~^(www)\.example\.com$; # $1 = www
amp to non amp conversion
location

Primary Sidebar

nginx server tutorials (installation, configuration, performance tuning, security)

  • Letsencrypt SSL Installation on apache/Nginx ubuntu / debian wordpress
  • fix error 520 522 524 on cloudflare wordpress godaddy nginx etc
  • nginx fastcgi cache wordpress how to configure
  • install LEMP Stack on 22.04 LTS Nginx MySQL PHP fpm #wordpress #digital ocean
  • Apache vs nginx (connection handling, modules, memory usage)
  • Pagespeed module install, configure, monitor, errors ft nginx &apache
  • nginx errors (504,502, 413, unable to start, syntax errors)
  • nginx conf explained best config file performance tuning tips nginx.conf location errors tutorial
  • use nginx as reverse proxy and load balancer for apache wordpress
  • nginx rewrite rules with examples 301 redirection
  • nginx modules list (enable, disable, upgrade, install dynamic module)
  • php fpm pool manager configuration settings based on server spike high cpu wordpress
  • php fpm restart nginx ubuntu enable status page, monitor etc
  • what is TTFB & how to Reduce it (server response time) Google pagespeed
  • letsencrypt install configure on ubuntu / debian nginx
  • Top 10 tips to improve nginx server security
  • nginx performance tuning connections, buffers file descriptors
  • enable brotli compression nginx brotli vs gzip
  • nginx installation on ubuntu 20.04 LTS
  • monitor nginx request with nginx status amplify datadog new relic
  • SSL faster reduce TLS hand shake improve https performance
  • nginx rate limiting explained by location time specific url
  • datadog nginx integration installation process
  • newrelic nginx integration process and errors fix and metrics
  • php fpm seems busy fixed warning and max children reached to handle max connections / requests
  • Php fpm configuration for 1000 concurrent connections server busy max children reached
  • php fpm ondemand vs dynamic vs Static (the dynamic pool problem)
  • nginx upstream response is buffered to a temporary file
  • php fpm install ubuntu 20.04 nginx
  • install phpmyadmin ubuntu nginx 22.04
  • upgrade php fpm ubuntu nginx 7.4 to 8.2
  • nginx add last modified header (remove, php wordpress)
  • php fpm dynamic pool manager settings
  • nginx fastcgi cache purge
  • nginx open file limit connections ulimits sysctl
  • php fpm high cpu usage WordPress 4 solutions nay work for you
  • nginx buffer size for wordpress
  • Cloudflare error code 524 nginx a timeout error occured
  • server configuration for 1000 concurrent users

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy