简体   繁体   English

htaccess到nginx重写问题

[英]htaccess to nginx rewrite issue

In my code I am trying to convert Apache .htaccess into Nginx using online convertor. 在我的代码中,我尝试使用在线转换器将Apache .htaccess转换为Nginx I even edited this file etc/nginx/conf.d/domainname.conf but the rewrite URLS won't work. 我什至编辑了这个文件etc/nginx/conf.d/domainname.conf但是重写URL无效。 I have two htaccess files, one of which used in root folder and the second one used in administration folder of my script. 我有两个htaccess文件,其中一个用于脚本的根文件夹,另一个用于脚本的管理文件夹。

Here is the root's htaccess file content 这是根目录的htaccess文件内容

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

htaccess converted code for Nginx htaccess为Nginx转换的代码

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

Here is the administration folder htaccess file content 这是管理文件夹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]

Administration folder htaccess converted code for Nginx 管理文件夹htaccess为Nginx转换的代码

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

I pasted the converted content into domainname.conf (domainname is my domain) and restart the ngnix but it won't work at all. 我将转换后的内容粘贴到domainname.conf(域名是我的域)中,然后重新启动ngnix,但它根本无法工作。 I don't know whether my converted code is accurate or not or any thing else I am missing through it. 我不知道转换后的代码是否正确,或者我是否错过了其他任何事情。

Simply edit the file ie etc/nginx/conf.d/yourdomain.tld.conf and look for the following code like this: 只需编辑文件即etc/nginx/conf.d/yourdomain.tld.conf并查找以下代码即可:

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

You need to un-comment these 3 lines of code or paste this code instead: 您需要取消注释这三行代码或粘贴以下代码:

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;
    }
}

Then you must require to restart Nginx , if restarted without any errors it will work accurately otherwise Nginx will stop working, this step is important. 然后,您必须要求重新启动Nginx ,如果重新启动没有任何错误,它将可以正常工作,否则Nginx将停止工作,这一步很重要。

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

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