简体   繁体   English

Nginx 转换为 Apache 重写规则

[英]Conversion of Nginx to Apache Rewrite Rule

I'm having to move nginx sites over to apache and I'm having an issue w/ mod rewrite rules.我不得不将 nginx 站点移至 apache,并且我遇到了 mod 重写规则问题。 These are my nginx rewrite rules and I'm not... quite... nailing it as I've tried various iterations on my .htaccess file and it's not quite working for me.这些是我的 nginx 重写规则,我不是......完全......因为我在我的 .htaccess 文件上尝试了各种迭代并且它对我来说不太有效。 I'm also not having an easy time finding a 1:1 conversion on the.net and regex has always confounded me.我也不容易在 .net 上找到 1:1 转换,正则表达式一直让我感到困惑。

 location / {
                index   index.php;
                try_files $uri /index.php?$uri&$args;
        }

        location ~ ^/(.*\.php)$ {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME $request_filename;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                fastcgi_param FWENV 'development';
        }

Primarily where I'm getting stuck is the主要是我被卡住的地方是

location ~ ^/(.*\.php)$ {

and how it interacts with以及它如何与

try_files $uri /index.php?$uri&$args;

inside of the .htaccess directives在 .htaccess 指令中

Any help would be appreciated.任何帮助,将不胜感激。

Where I left off was:我离开的地方是:

RewriteRule ~ /(.*.php) $1 [L,NC]重写规则 ~ /(.*.php) $1 [L,NC]

But I'm pretty sure I'm wayyyy off.但我很确定我已经离开了。 I've been making slight variations of adding ^ and $ around the rule and some give me errors upon apache restart, others do not, none, however, give me a proper rewrite in the browser.我一直在围绕规则添加 ^ 和 $ 做一些细微的变化,有些在 apache 重新启动时给我错误,其他人没有,没有,但是,在浏览器中给我一个正确的重写。

UPDATE:更新:

I've started to use this particular rule because it does catch some of the issues but I believe my original NGINX rule incorporates it better.我已经开始使用这个特定的规则,因为它确实捕获了一些问题,但我相信我原来的 NGINX 规则更好地结合了它。 If anyone has a better idea, please, let me know.如果有人有更好的主意,请告诉我。

RewriteEngine on
RewriteRule ^.*\.(gif|jpe?g|png|css|ttf|js)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/(.*)?$ /index.php?module=$1&controller=$2&process=$3&object_id=$4 [L]
RewriteRule ^(.*)/(.*)/(.*)?$ /index.php?module=$1&controller=$2&process=$3 [L]
RewriteRule ^(.*)/(.*)?$ /index.php?module=$1&controller=$2 [L]

So refining it a bit, now when I do the following, everything still works (as much as it did) but still doesnt work (urls like example.com/blah).所以稍微改进一下,现在当我执行以下操作时,一切仍然有效(就像它所做的那样)但仍然不起作用(像 example.com/blah 这样的 url)。 I'm confounded as to why.我很困惑为什么。

RewriteEngine on
RewriteRule ^.*\.(gif|jpe?g|png|css|ttf|js)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)?$ /index.php? [L]

Okay, found the answer eventually and it was very close to the what I had for nginx (although I still don't understand it all that well).好的,最终找到了答案,它非常接近我对 nginx 的答案(尽管我仍然不太理解)。

RewriteEngine on
RewriteRule ^.*\.(gif|jpe?g|png|css|ttf|js)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM