• notice
  • Congratulations on the launch of the Sought Tech site

How nginx redirects URLs to specific pages

I have seen this question which describes how to redirect urls in nginx as follows

# main server block for www.test.com
server {
    listen       80;
    server_name  www.test.com;    ...}# redirect test.com to www.test.com
server {
        server_name test.com;        return 301 $scheme://www.test.com$request_uri;}

What I need to do is redirect a set of individual pages, so would like to know how to do this

For example test.com\index, test.com\homecometest.com\maintest.com\index.php

Then I have other pages that simply redirect to the .php extension

For example test.com\aboutto\test.com\about.php


What's the best way to do this?
Found the answer... assuming the following server block test.com

server {
   listen       80;
   server_name  www.test.com;    ...}


Add the appropriate regex location path to the redirect URL, rewriteor returnadd it to the redirect URL.

for test.com\index, test.com\home, test.com\mainwithtest.com\index.php

location ~ ^/(index|home|main) {
   rewrite ^/.* http://$server_name/index.php permanent;}



for test.com\aboutthe\test.com\about.php

location /about {
    rewrite ^/.* http://$server_name/about.php permanent;}

Example configuration

    location /titanium-stainless-jewelry  {
      rewrite ^/.*   https://$server_name/stainless-stainless-jewelry permanent;


path modification redirect  

pre.weifeng.com/en/ start path url redirect to pre.weifeng.com path
      location ~ /en {
         rewrite ^/en/(.*) https://pre.weifeng.com/$1 permanent;


Technical otaku

Sought technology together

Related Topic


Leave a Reply