【发布时间】:2014-02-19 01:20:03
【问题描述】:
我需要在 nginx 服务器中创建 url 重写规则(服务器块),就像我以前的 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/page1 、www.domain.com/username/page2、www.domain.com/username/page3。 这是我需要在 nginx 中实现的。因为现在是没有功能的完整后端。当我登录到后端时,我被重定向到 www.domain.com/username 但在屏幕上我只能看到 File not found. 只有当我手动时才能在后端工作添加 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;
}
}
当我尝试将我的块更改为:
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...
如果有任何帮助,我将不胜感激! 非常感谢。
【问题讨论】:
-
如何区分后端和前端 url?还是只是后端?
-
@Mohammad AbuShady 前端就像 www.example.com/frontend-pages 并且在登录到后端 www.example.com/username 后后端页面 www.example.com/username/pages-backend
-
@MohammadAbuShady 有问题可用于赏金stackoverflow.com/questions/20018951/…
-
username是任何随机用户名吗?喜欢example.com/makromat? -
@MohammadAbuShady 是的。谢谢!
标签: apache .htaccess mod-rewrite nginx