将 htaccess 转换为 Nginx ReWrite(自动转换不起作用)

将 htaccess 转换为 Nginx ReWrite(自动转换不起作用)

大家好,有人知道 Nginx 重写规则吗?

我正在尝试将此 Apache .htaccess 转换为 Nginx Rewrite,我已经尝试过在线转换器

登录后,主页和仪表板可以正常工作,但新闻、列表、视频、音乐、民意调查和测验部分无法正常工作,出现 404 错误

我连续 32 个小时没睡觉设置服务器,这真是让我抓狂

任何帮助将不胜感激

这是我想要转换的 Apache .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^$ index.php?link1=home [NC,QSA]
RewriteRule ^news/(.*)$ index.php?link1=news&id=$1 [NC,QSA]
RewriteRule ^lists/(.*)$ index.php?link1=lists&id=$1 [NC,QSA]
RewriteRule ^polls/(.*)$ index.php?link1=polls&id=$1 [NC,QSA]
RewriteRule ^quiz/(.*)$ index.php?link1=quiz&id=$1 [NC,QSA]
RewriteRule ^videos/(.*)$ index.php?link1=videos&id=$1 [NC,QSA]
RewriteRule ^music/(.*)$ index.php?link1=music&id=$1 [NC,QSA]
RewriteRule ^edit-post/(.*)$ index.php?link1=edit-post&id=$1 [NC,QSA]
RewriteRule ^delete-post/(.*)$ index.php?link1=delete-post&id=$1 [NC,QSA]
RewriteRule ^settings/(.*)/(.*)$ index.php?link1=settings&page=$1&user=$2 [NC,QSA]
RewriteRule ^settings/(.*)$ index.php?link1=settings&page=$1 [NC,QSA]
RewriteRule ^admin-cp$ admincp.php [NC,QSA]
RewriteRule ^admin-cp/(.*)$ admincp.php?page=$1 [NC,QSA]
RewriteRule ^admincp/(.*)$ index.php?link1=admincp&page=$1 [NC,QSA]
RewriteRule ^admin-cdn/(.*)$ admin-panel/$1 [L]
RewriteRule ^tags/(.*)$ index.php?link1=tags&tag=$1 [NC,QSA]
RewriteRule ^feeds/rss(/?|)$ index.php?link1=feeds&page=home [NC,QSA]
RewriteRule ^post_data/(.*)/(.*)(/?|)$ index.php?link1=post_data&post_type=$1&id=$2 [NC,QSA]

RewriteRule ^terms/(.*)$ index.php?link1=terms&type=$1 [NC,QSA]
RewriteRule ^go_pro(?:\/{0,1}|)$ index.php?link1=go_pro [NC,QSA]

RewriteRule ^latest-(.*)/(\d+)$ index.php?link1=latest-$1&c_id=$2 [NC,QSA]
RewriteRule ^latest-(.*)/rss(/?|)$ index.php?link1=rss&page=$1 [NC,QSA]

RewriteRule ^ads(?:\/?|)$ index.php?link1=ads [NC,QSA]
RewriteRule ^ads/create-new(?:\/?|)$ index.php?link1=create_ad [NC,QSA]
RewriteRule ^ads/edit/([0-9]+)(?:\/?|)$ index.php?link1=edit_ad&ad_id=$1 [NC,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^@([^\/]+)(\/|)$ index.php?link1=profile&u=$1 [QSA]
RewriteRule ^@([^\/]+)(\/|)/(.*)$ index.php?link1=profile&u=$1&page=$2 [QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)(\/|)$ index.php?link1=$1 [QSA]

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>

这是自动转换的文件(经过我 3 小时阅读 Nginx 文档后,部分工作正常)

location / {
  if (!-e $request_filename){
    rewrite ^/$ /index.php?link1=home;
  }
  rewrite "^/go_pro(?:\/{0,1}|)$" /index.php?link1=go_pro;
  if (!-e $request_filename){
    rewrite ^/@([^\/]+)(\/|)$ /index.php?link1=profile&u=$1;
  }
  rewrite ^/@([^\/]+)(\/|)/(.*)$ /index.php?link1=profile&u=$1&page=$2;
  if (!-e $request_filename){
    rewrite ^/([^\/]+)(\/|)$ /index.php?link1=$1;
  }
}


(**_not working from here and below news, list, video, music, polls and quiz sections_**)

location /news {
  rewrite ^/news/(.*)$ /index.php?link1=news&id=$1;
}

location /lists {
  rewrite ^/lists/(.*)$ /index.php?link1=lists&id=$1;
}

location /polls {
  rewrite ^/polls/(.*)$ /index.php?link1=polls&id=$1;
}

location /quiz {
  rewrite ^/quiz/(.*)$ /index.php?link1=quiz&id=$1;
}

location /videos {
  rewrite ^/videos/(.*)$ /index.php?link1=videos&id=$1;
}

location /music {
  rewrite ^/music/(.*)$ /index.php?link1=music&id=$1;
}

location /edit {
  rewrite ^/edit-post/(.*)$ /index.php?link1=edit-post&id=$1;
}

location /delete {
  rewrite ^/delete-post/(.*)$ /index.php?link1=delete-post&id=$1;
}

location /settings {
  rewrite ^/settings/(.*)/(.*)$ /index.php?link1=settings&page=$1&user=$2;
  rewrite ^/settings/(.*)$ /index.php?link1=settings&page=$1;
}

(**_admin panel and the other admin, term, ads, latest,tags and feed and all rest sections below this line work fine_**)

location /admin {
  rewrite ^/admin-cp$ /admincp.php;
  rewrite ^/admin-cp/(.*)$ /admincp.php?page=$1;
}

location /admincp {
  rewrite ^/admincp/(.*)$ /index.php?link1=admincp&page=$1;
}

location /admin-cdn/ {
  alias /admin-panel/;
}

location /tags {
  rewrite ^/tags/(.*)$ /index.php?link1=tags&tag=$1;
}

location /feeds {
  rewrite ^/feeds/rss(/?|)$ /index.php?link1=feeds&page=home;
}

location /post_data {
  rewrite ^/post_data/(.*)/(.*)(/?|)$ /index.php?link1=post_data&post_type=$1&id=$2;
}

location /terms {
  rewrite ^/terms/(.*)$ /index.php?link1=terms&type=$1;
}

location /latest {
  rewrite ^/latest-(.*)/(\d+)$ /index.php?link1=latest-$1&c_id=$2;
  rewrite ^/latest-(.*)/rss(/?|)$ /index.php?link1=rss&page=$1;
}

location /ads {
  rewrite ^/ads(?:\/?|)$ /index.php?link1=ads;
  rewrite ^/ads/create-new(?:\/?|)$ /index.php?link1=create_ad;
  rewrite ^/ads/edit/([0-9]+)(?:\/?|)$ /index.php?link1=edit_ad&ad_id=$1;
}

如果我在问题中犯了任何错误,请原谅我,我刚刚才醒,我是一名电子工程师,正在尝试为我的 IoT 设备设置服务器前端和后端;我不是程序员,但我非常钦佩他们,学过一些基本的 WebDeV,但这是我第一次使用 Nginx 编码不是我的强项,并不是每个人都对我很好https://stackoverflow.com/questions/63078446/converting-htaccess-to-nginx-rewrite-autoconverting-not-working?noredirect=1#comment111550833_63078446

任何帮助都将不胜感激提前致谢

答案1

您的部分应遵循以下模式:

location / {
    try_files $uri /index.php?link1=home;
}

location ~ ^/go_pro(?:\/{0,1}|)$ {
    try_files /index.php?link1=go_pro =404;
}

location ~ ^/news/(?<newsid>.*)$ {
    try_files /index.php?link1=news&id=$newsid =404;
}

这种写位置的方式更容易阅读和理解,而且不需要if以下语句:邪恶的

需要注意的location加工订单

相关内容