【问题标题】:htaccess to nginx rewrite issuehtaccess 到 nginx 重写问题
【发布时间】:2018-04-06 20:17:24
【问题描述】:

在我的代码中,我尝试使用在线转换器将 Apache .htaccess 转换为 Nginx。我什至编辑了这个文件etc/nginx/conf.d/domainname.conf,但重写 URLS 不起作用。我有两个 htaccess 文件,其中一个在根文件夹中使用,第二个在我的脚本的管理文件夹中使用。

这里是root的htaccess文件内容

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?slug=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?slug=$1

为 Nginx 转换的 htaccess 代码

location / {
  rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
  rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
}

这里是管理文件夹htaccess文件内容

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^(.*)/$ /$1.php [L,R=301]

管理文件夹 htaccess 为 Nginx 转换的代码

location /administration {
  if (!-e $request_filename){
    rewrite ^(.*)$ /$0.php break;
  }
  rewrite ^/(.*)/$ /$1.php redirect;
}

我将转换后的内容粘贴到 domainname.conf 中(域名是我的域)并重新启动 ngnix,但它根本不起作用。我不知道我转换后的代码是否准确或我遗漏的任何其他内容。

【问题讨论】:

    标签: apache .htaccess nginx


    【解决方案1】:

    只需编辑文件,即etc/nginx/conf.d/yourdomain.tld.conf,然后查找以下代码:

    #location / {
    #Uncomment 3 lines and set your rules here!
    #}
    

    您需要取消注释这 3 行代码或粘贴此代码:

    location / {
        if (!-e $request_filename) {
        rewrite ^/([a-zA-Z0-9-/]+)$ /index.php?slug=$1;
        rewrite ^/([a-zA-Z0-9-/]+)/$ /index.php?slug=$1;
        }
    }
    
    location /administration/ {
        if (!-e $request_filename){
        rewrite ^(.*)$ $1.php last;
        }
    }
    

    那么你必须要求重新启动Nginx,如果重新启动没有任何错误它将正常工作,否则Nginx将停止工作,这一步很重要。

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多