Php 从htaccess迁移到NGINX config
今天,我从Apache迁移到Nginx+FPM,这导致htaccess不再工作,我尝试了使用Nginx转换器的htaccess,但这并不是我之前想要的100% 我的目标是在/index.php?menu=之后发送任何参数,其中也可以有参数:Php 从htaccess迁移到NGINX config,php,.htaccess,nginx,Php,.htaccess,Nginx,今天,我从Apache迁移到Nginx+FPM,这导致htaccess不再工作,我尝试了使用Nginx转换器的htaccess,但这并不是我之前想要的100% 我的目标是在/index.php?menu=之后发送任何参数,其中也可以有参数: accessing : example.com/index.php will change URL to example.com accessing : example.com/test.php will change URL to example
accessing : example.com/index.php will change URL to example.com
accessing : example.com/test.php will change URL to example.com/test
- server receive menu=test
accessing : example.com/fakepageaa.asd
- server receive receive menu=fakepageaa.asd
accessing : example.com/register
- server receive menu=register
accessing : example.com/welcome&args1=usa&args2=john
- server receive receive menu=welcome, args1=usa, args2=john
我的工作访问代码
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?menu=$1 [QSA]
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
已更新
经过多次尝试,现在我的完整nginx配置如下
server {
listen IP:80;
server_name example.com;
root /home/admin/web/example.com/public_html;
index index.php index.html index.htm;
access_log /var/log/nginx/domains/example.com.log combined;
access_log /var/log/nginx/domains/example.com.bytes bytes;
error_log /var/log/nginx/domains/example.com.error.log error;
autoindex off;
location / {
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?menu=$1;
}
if (!-e $request_filename){
rewrite ^/(.*?)/?$ /$1.php break;
}
location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
error_page 403 /error_page.php;
error_page 404 /error_page.php;
error_page 500 502 503 504 /error_page.php;
location ~* "/\.(htaccess|htpasswd)$" {
deny all;
return 404;
}
if ($request_uri ~* "^(.*/)index\.php(.*)") {
return 307 $1$2;
}
}
它几乎解决了我的问题,当前的问题:
accessing : example.com/fakepageaa.asd will direct to error page
- server should be receive menu=/fakepageaa.asd
accessing : example.com/index will change URL to example.com
- server should be receive menu=/index
accessing : example.com/test.php will change URL to example.com/test (solved)
- server receive menu=/test (/ can be fix using str_replace)
accessing : example.com/welcome&args1=usa&args2=john (solved)
- server receive menu=/welcome, args1=usa, args2=john (/ can be fix using str_replace)
accessing : example.com/register (solved)
- server receive menu=/register (/ can be fix using str_replace)
accessing : example.com/index.php will change URL to example.com (solved)
如果有人能帮助我,那就太好了。
谢谢。我不明白您发布的Apache规则与它们下面的nginx规则有什么关系。请您将您尝试过的导致无限重定向的规则包括在内,最好是删除尽可能多的不相关部分,这样您就有了一个更新的。对不起,我想做的(请求帮助)是将htaccess更改为nginx格式。我不太了解nginx配置,无法完全回答,但可能对您有所帮助的是修复.htaccess规则的布局-每组RewriteCond行仅适用于下一个RewriteRule,而RewriteEngine和RewriteBase则适用于所有规则,因此当前间距非常混乱。然后,您可以了解每个规则的作用,也许可以在每个规则旁边添加注释。然后我的普遍建议是:把问题分解;在这种情况下,一次只处理一条规则。我不知道您发布的Apache规则与它们下面的nginx规则有什么关系。请您将您尝试过的导致无限重定向的规则包括在内,最好是删除尽可能多的不相关部分,这样您就有了一个更新的。对不起,我想做的(请求帮助)是将htaccess更改为nginx格式。我不太了解nginx配置,无法完全回答,但可能对您有所帮助的是修复.htaccess规则的布局-每组RewriteCond行仅适用于下一个RewriteRule,而RewriteEngine和RewriteBase则适用于所有规则,因此当前间距非常混乱。然后,您可以了解每个规则的作用,也许可以在每个规则旁边添加注释。然后我的普遍建议是:把问题分解;在这种情况下,一次处理一条规则。