nginx 如何将 url 重定向到特定页面
nginx how do I redirect a url to a specific page
我看过this question,它描述了如何在nginx中重定向一个url,如下所示
# 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;
}
我需要做的是重定向一组单独的页面,所以想知道如何做到这一点
例如test.com\index
, test.com\home
, test.com\main
到 test.com\index.php
然后我有一些其他页面可以简单地重定向到 .php 扩展名
例如test.com\about
到 \test.com\about.php
例如test.com\contact
至 \test.com\contact.php
最好的方法是什么?
找到答案...假设以下服务器块用于 test.com
server {
listen 80;
server_name www.test.com;
...
}
添加适当的正则表达式位置路径,并将rewrite
或return
添加到重定向url。
对于 test.com\index
, test.com\home
, test.com\main
到 test.com\index.php
location ~ ^/(index|home|main) {
rewrite ^/.* http://$server_name/index.php permanent;
}
对于 test.com\about
到 \test.com\about.php
location /about {
rewrite ^/.* http://$server_name/about.php permanent;
}
我看过this question,它描述了如何在nginx中重定向一个url,如下所示
# 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;
}
我需要做的是重定向一组单独的页面,所以想知道如何做到这一点
例如test.com\index
, test.com\home
, test.com\main
到 test.com\index.php
然后我有一些其他页面可以简单地重定向到 .php 扩展名
例如test.com\about
到 \test.com\about.php
例如test.com\contact
至 \test.com\contact.php
最好的方法是什么?
找到答案...假设以下服务器块用于 test.com
server {
listen 80;
server_name www.test.com;
...
}
添加适当的正则表达式位置路径,并将rewrite
或return
添加到重定向url。
对于 test.com\index
, test.com\home
, test.com\main
到 test.com\index.php
location ~ ^/(index|home|main) {
rewrite ^/.* http://$server_name/index.php permanent;
}
对于 test.com\about
到 \test.com\about.php
location /about {
rewrite ^/.* http://$server_name/about.php permanent;
}