0

I have looked at this question which describes how to redirect a url in nginx, as shown below

# 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 was wondering how to do this

e.g. test.com\index , test.com\home , test.com\main to test.com\index.php

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

e.g. test.com\about to \test.com\about.php

e.g. test.com\contact to \test.com\contact.php

What is the best way to do this?

Community
  • 1
  • 1
svnm
  • 22,878
  • 21
  • 90
  • 105

1 Answers1

1

Found the answer... assuming the following server block for test.com

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

Add the appropriate regex location path, and rewrite or return to the redirect url.

for test.com\index , test.com\home , test.com\main to test.com\index.php

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

for test.com\about to \test.com\about.php

location /about {
    rewrite ^/.* http://$server_name/about.php permanent;
}
svnm
  • 22,878
  • 21
  • 90
  • 105