将htaccess转换为nginx,寻求解决方案

时间:2016-11-05 12:44:26

标签: .htaccess nginx

我在所有网站上都有这个nginx设置。如果我改变太多,通常网站不起作用。现在的问题是我有一个我用htaccess文件购买的网站。我尝试使用在线转换器进行转换,但没有任何作用......

我的NGINX配置

server {
    server_name doutor.pt www.doutor.pt;

    access_log   /var/log/nginx/doutor.pt.access.log;
    error_log    /var/log/nginx/doutor.pt.error.log;

    root /var/www/doutor.pt/htdocs;
    index index.php index.html;

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

    location ~ .php$ {
            include fastcgi_params;
            fastcgi_pass 127.0.0.1:9000;
    }

}

htaccess的

################################
#         MAIN SETTINGS        #
################################

# Remove index
Options -Indexes

# Set directory indexes
DirectoryIndex index.html index.php under-construction.html parking-place.html

################################
#       APACHE REWRITES        #
################################

RewriteEngine On

# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap-doctors-([0-9]+).xml$ index.php?page_name=sitemap&category=doctors&page=$1 [L,QSA]

# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap-doctors.xml$ index.php?page_name=sitemap&category=doctors&page=0 [L,QSA]

# Domain page parser for sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sitemap.xml$ index.php?page_name=sitemap [L,QSA]

# Domain page parser for category
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^category/([0-9]+)-([^/]+)$ index.php?page_name=doctors&category=$1 [L,QSA]

# Domain page parser for doctor page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^medico/([^/]+)$ index.php?page_name=medico&doctor_url=$1 [L,QSA]

# Domain page parser for secondary level pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ index.php?page_name=$2&page_category=$2 [L,QSA]

# Domain page parser for regular pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !sitemap$
RewriteRule ^([^/]+)$ index.php?page_name=$1 [L,QSA]

################################
#            CUSTOM            #
################################

2 个答案:

答案 0 :(得分:1)

Nginx是一件很棒的事情,几乎所有时间都可以使用位置块而不是可能难以理解/调试重写。这个htaccess文件很容易转换,当我被问到是否可以在其他网站上提供帮助时,我已经测试了一点它的输出(结果)。

正如Richard的代码所示,apache的奇怪否定逻辑-d和-f很容易转换为nginx中的try_files $uri $uri/,您可以在其中检查uri对Web根路径的请求。

默认情况下,在nginx中禁用目录索引,因此必须启用它才能使用它......没有任何东西可以添加(再次)apache的奇怪的负面逻辑。这是索引的样子......

index index.php index.html under-construction.html parking-place.html;

根据我的理解,以下是站点地图重写转化为的内容:

 # If the URI is bang on /sitemap.xml
 location = /sitemap.xml {
     try_files $uri $uri/ /index.php?page_name=sitemap;
 }

 # If the URI is bang on /sitemap-doctors.xml
 location = /sitemap-doctors.xml {
     try_files $uri $uri/ /index.php?page_name=sitemap&category=doctors&page=0;
 }

 # Enclosing this in the ~* /sitemap block isn't mandatory but it only
 # helps creating cleaner, readable code while also making sure we contain
 # all /sitemap*s requests thus taking care of the !sitemap directive.

 location ~* /sitemap {
     location ~ ^/sitemap-doctors-([0-9]+).xml {
         try_files $uri $uri/ /index.php?page_name=sitemap&category=doctors&page=$1;
     }
 }

遵循相同的逻辑,下一行将看起来像这样:

# It seems we are only using the first capture group ( $1 ) here so the
# regex could be modified for something simpler but to make sure we are
# not catching stuff we don't want, leaving this precision will save troubles

location ^/category/([0-9]+)-([^/]+)$ {
    try_files $uri $uri/ /index.php?page_name=doctors&category=$1;
}

再次,对于更多相同的......

 # The ^ and $ are regex delimiters for start and end, so you know...

 location ^/medico/([^/]+)$ {
     try_files $uri $uri/ /index.php?page_name=medico&doctor_url=$1;
 }

现在我们已经摆脱了所有精确的东西,是时候让一些通用的uri捕获并传递给php ...

  

我觉得这个htaccess不合适......我很乐意得到解释..

这里发送捕获块($ 2)两次,在我看来这是非常不寻常的,没有意义。话虽这么说,但没有看到它在行动或查看源代码,这是一个棘手的...

 RewriteRule ^([^/]+)/([^/]+)$ index.php?page_name=$2&page_category=$2 [L,QSA]

http://hungry.man/delicious/pizza的网址将被视为php http://hungry.man/index.php?page_name=pizza&page_category=pizza,而不提及 美味 ...

对于剩下的东西,我会想要只使用@rewrite并按原样写出来,而不是过多地挑战我的大脑。

希望它有所帮助,Mat

答案 1 :(得分:0)

在我看来.htaccess文件试图做这样的事情:

location / {
    try_files $uri $uri/ @rewrite;
}
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;
}
location @rewrite {
    rewrite ^/sitemap-doctors-([0-9]+).xml$ /index.php?page_name=sitemap&category=doctors&page=$1 last;
    rewrite ^/sitemap-doctors.xml$ /index.php?page_name=sitemap&category=doctors&page=0 last;
    rewrite ^/sitemap.xml$ /index.php?page_name=sitemap last;
    rewrite ^/category/([0-9]+)-([^/]+)$ /index.php?page_name=doctors&category=$1 last;
    rewrite ^/medico/([^/]+)$ /index.php?page_name=medico&doctor_url=$1 last;
    rewrite ^/([^/]+)/([^/]+)$ /index.php?page_name=$2&page_category=$2 last;

    rewrite ^/([^/]+)$ /index.php?page_name=$1 last;

    # some default action???
    return 404;
}

Apache和nginx中重写的主要区别在于后者需要URI上的前导/。我没有实现RewriteCond %{REQUEST_URI} !sitemap$规则或实施了最终的默认操作。您需要确定哪些有效:return 404或将所有内容重写为/index.php