Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Nginx将URL重写为index.php参数_Regex_.htaccess_Nginx_Url Rewriting - Fatal编程技术网

Regex Nginx将URL重写为index.php参数

Regex Nginx将URL重写为index.php参数,regex,.htaccess,nginx,url-rewriting,Regex,.htaccess,Nginx,Url Rewriting,我最近从apache切换到了nginx。 我的应用程序位于/var/www/html下,具有以下结构: # tree -a /var/www/html /var/www/html ├── .htaccess ├── folder1 │   ├── .htaccess │   └── index.php ├── folder2 │   ├── .htaccess │   └── index.php ├── index.php ├── file1.php └── file2.php 所有目录中的.

我最近从apache切换到了nginx。 我的应用程序位于/var/www/html下,具有以下结构:

# tree -a /var/www/html
/var/www/html
├── .htaccess
├── folder1
│   ├── .htaccess
│   └── index.php
├── folder2
│   ├── .htaccess
│   └── index.php
├── index.php
├── file1.php
└── file2.php
所有目录中的.htaccess文件都相同:

# cat /var/www/html/.htaccess 
AddDefaultCharset UTF-8
Options +FollowSymLinks
Options -Indexes
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(assets)/ - [L,NE]

RewriteRule ^/?$ index.php?param1=main [QSA,L]
RewriteRule ^([-a-zA-Z0-9_]+)/$ index.php?param1=$1 [QSA,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?param1=$1&param2=$2 [QSA,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?param1=$1&param2=$2&param3=$3 [QSA,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?param1=$1&param2=$2&param3=$3&param4=$4 [QSA,L]

RewriteRule ^file1.xml$ file1.php [L]
RewriteRule ^file2.xml$ file2.php [L]

ErrorDocument 400 /error/400/
ErrorDocument 401 /error/401/
ErrorDocument 403 /error/403/
ErrorDocument 404 /error/404/
ErrorDocument 500 /error/500/

RedirectMatch (.*)\.inc /error/404/
RedirectMatch (.*)\.tpl /error/404/
现在我正在尝试构建nginx配置。我的目标是进行以下重写:

example.com or example.com/ -> example.com/index.php?param1=main
example.com/anything or example.com/anything/ -> example.com/index.php?param1=anything
example.com/anything/else or example.com/anything/else/ -> example.com/index.php?param1=anything&param2=else
etc...
folder1和folder2也是如此

example.com/folder1 or example.com/folder1/ -> example.com/folder1/index.php?param1=main
example.com/folder1/anything or example.com/folder1/anything/ -> example.com/folder1/index.php?param1=anything
etc...
Nginx配置当前看起来像:

server {
    listen 443 ssl;
    server_name example.com;
    charset utf-8;

    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;

    root /var/www/html;
        index index.html index.htm index.php;

    location / {
    try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

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

我使用了在线转换器,并将规则放在
位置/{
下,但这不起作用。类似example.com/anything的Url显示404和example.com/anything/尝试下载文件

有多种方法可以做到这一点。但下面的示例使用命名位置来收集所有重写规则

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^/$ /index.php?param1=main? last;
    rewrite ^/([-a-zA-Z0-9_]+)/$ /index.php?param1=$1? last;
    rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ /index.php?param1=$1&param2=$2? last;
    rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ /index.php?param1=$1&param2=$2&param3=$3? last;
    rewrite ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ /index.php?param1=$1&param2=$2&param3=$3&param4=$4? last;

    rewrite ^/file1.xml$ /file1.php last;
    rewrite ^/file2.xml$ /file2.php last;
    return 404;
}
所有Nginx URI都以一个前导的
/
开头。我假设默认操作是,如果没有任何规则匹配,则返回404。尾部的
阻止将任何原始参数附加到重写的URI中。如果要附加原始参数,请删除尾部的
。有关详细信息,请参阅

注意:在您的问题中,您声明您希望接受
example.com/anything/else
example.com/anything/else/
。这不是现有的
.htaccess
所做的。要使尾随
/
可选,请在
$
之前的最后一个
/
之后添加一个
。例如:

rewrite ^/([-a-zA-Z0-9_]+)/?$ /index.php?param1=$1? last;

您可以对其他文件夹重复此模式,但它看起来非常混乱。此逻辑属于
index.php
,但下面给出了一个Nginx示例:

location /folder1 {
    try_files $uri $uri/ @rewrite1;
}
location @rewrite1 {
    rewrite ^/folder1/?$ /folder1/index.php?param1=main? last;
    rewrite ^/folder1/([-a-zA-Z0-9_]+)/$ /folder1/index.php?param1=$1? last;
    ...
}

请注意,正则表达式和重写的URI都需要包含文件夹名称。

请添加您使用的nginx配置文件,并更具体地描述“不起作用”的内容实际上意思是说。@arkascha uploadedThank you,这对根文件夹非常有效。我尝试用同样的方法添加folder1和folder2,比如
location/folder1{try_files$uri$uri/@rewrite;}location@rewrite_folder1{rewrite^/$/index.php?param1=main?last;rewrite^/([-a-zA-Z0-9})/$/index.php?param1=$1?last;…返回404;}
但是对于example.com/folder1/anything这样的请求,它会返回500个错误。如果您想展开您的问题,请使用编辑按钮。但是您还没有显示
folder1
.htaccess
文件的内容。谢谢。..htaccess所有目录中的文件都是相同的详细说明。通常我有3个位置://所有位置的folder1和/folder2重写是相等的,差异仅在url中:example.com、example.com/folder1和example.com/folder2