gpt4 book ai didi

apache - Nginx 对 cms 后端的重写规则

转载 作者:行者123 更新时间:2023-12-03 03:41:49 24 4
gpt4 key购买 nike

我需要在 nginx 服务器中制定 url 重写规则(服务器 block ),就像我之前的 apache 服务器一样。
这是 .htaccess 中的代码,我需要将其实现(转换)到我现有的代码中:

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ admin/index.php?hotelname=$1&do=$2 [QSA]

RewriteRule ^(([A-Za-z0-9-/]+)+)$ admin/index.php?hotelname=$1 [L]

此代码位于我的网站中,因为我需要隐藏在登录后文件所在的地址栏文件夹(/admin/)中。当有人已经登录时,地址栏就像www.domain.com/username,当您单击菜单时,地址就像www.domain.com/username/page1www.domain.com/username/page2www.domain.com/username/page3。这就是我需要在 nginx 中实现的目标。因为现在是完整的后端,没有功能。当我登录后端时,我被重定向到 www.domain.com/username 但在屏幕上我只能看到 找不到文件。 仅当我手动时才在后端工作添加www.domain.com/admin/index.php

这是我的 nginx 实际配置:

server_names_hash_bucket_size  64;

server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}

server {
listen 80;
root /usr/share/nginx/www;
index index.php;
server_name www.example.com;
error_page 404 http://www.example.com/404.php;
autoindex off;



location / {
rewrite ^([^\.]*)$ /$1.php;
}

location = / {
rewrite ^ /index.php;
}


location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}

location ~ /\.ht {
deny all;
}
}

当我尝试将我的 block 更改为:

location / {
rewrite ^([^\.]*)$ /$1.php;
rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ /admin/index.php?hotelname=$1&do=$2;
rewrite ^/(([A-Za-z0-9-/]+)+)$ /admin/index.php?hotelname=$1 break;
}

我的每个 css 文件都有错误 500...

如果有任何帮助,我将非常感激!非常感谢。

最佳答案

您将其放入您的/位置,这意味着在您放到此处之前,您的所有请求均不匹配。您必须在位置/

之前创建特定于此条目的位置角色
location ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ {
rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-/_]+)$ /admin/index.php?hotelname=$1&do=$2;
}

关于apache - Nginx 对 cms 后端的重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21393803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com