各位朋友看下这个规则怎么转换呢?apache伪静态转nginx ,真心感谢,急需帮助!网上那些转换工具转换的没有用!
代码如下:
RewriteEngine on
RewriteRule ^sitemap.xml /xml.php
RewriteRule ^sitemap.html /sitemap.php
RewriteRule ^index.html /index.php
RewriteRule ^admin/$ /admin/index.php
RewriteRule ^cloud/$ /cloud/index.php
RewriteRule ^robots.txt /robots.php
RewriteRule ^(.*).html /article.php
RewriteRule ^(.*)/$ /article.php
要将 Apache 伪静态规则转换为 nginx,可以使用以下配置:
location /sitemap.xml {
rewrite ^/sitemap.xml /xml.php last;
}
location /sitemap.html {
rewrite ^/sitemap.html /sitemap.php last;
}
location /index.html {
rewrite ^/index.html /index.php last;
}
location /admin/ {
rewrite ^/admin/$ /admin/index.php last;
}
location /cloud/ {
rewrite ^/cloud/$ /cloud/index.php last;
}
location /robots.txt {
rewrite ^/robots.txt /robots.php last;
}
location / {
if ($request_uri ~ "\.html$") {
rewrite ^/(.*).html /article.php last;
}
}
location / {
if ($request_uri ~ "\/$") {
rewrite ^/(.*)/$ /article.php last;
}
}
在 nginx 中,该rewrite指令用于执行 URL 重写。该location块指定要匹配的 URI 模式,rewrite块内的指令指定替换 URI。该last标志表示这是要应用的最后一条规则,不应执行进一步的重写。
配置中的前六个location块匹配特定的 URI 模式并对相应的 PHP 脚本执行简单的重写。最后两个location块匹配任何以“.html”结尾或以斜线“/”结尾的 URI,并对article.php脚本执行重写。
若要在 nginx 中使用类似于 Apache 的伪静态规则,您可以使用 nginx 的 try_files 命令。
例如,如果您想将以下 Apache 伪静态规则转换为 nginx:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ index.php?page=$1&id=$2&title=$3 [L]
您可以使用以下 nginx 规则:
location / {
try_files $uri $uri/ /index.php?page=$1&id=$2&title=$3;
}
请注意,在上述 nginx 规则中,$1、$2 和 $3 是 nginx 中的变量,用于存储请求 URI 的前三个段。
在 Nginx 中使用伪静态规则类似于在 Apache 中使用伪静态规则。您可以在 Nginx 的 server 块中使用 rewrite 指令将 Apache 的伪静态规则转换为 Nginx 的伪静态规则。
例如,您可以使用以下代码转换上述 Apache 伪静态规则:
server {
...
rewrite ^/sitemap.xml /xml.php last;
rewrite ^/sitemap.html /sitemap.php last;
rewrite ^/index.html /index.php last;
rewrite ^/admin/$ /admin/index.php last;
rewrite ^/cloud/$ /cloud/index.php last;
rewrite ^/robots.txt /robots.php last;
rewrite ^/(.*).html /article.php last;
rewrite ^/(.*)/$ /article.php last;
}
请注意,在上述代码中,我们已删除 Apache 的 RewriteEngine 和 RewriteRule 指令,并使用 Nginx 的 rewrite 指令代替。此外,我们还在末尾添加了 last 标志,以表示这是匹配的最后一条规则。
这篇实例也许帮不上啥忙,但很有意思【Apache/Nginx伪静态规则匹配http://出现的问题与解决】,链接:https://zhang.ge/5020.html