Apache重写规则到Nginx

时间:2017-11-21 23:59:39

标签: php apache .htaccess nginx

我希望将htaccess规则重写为nginx规则

这是我需要转换的htaccess:

RewriteEngine On
RewriteRule ^/?c/([^/]+)/?$ search_result.php?category_id=$1 [L,QSA]
RewriteRule    ^d/([^/.]+)/?$    detail.php    [NC,L]

我尝试this site,然后插入我的vhost domain.conf:

location / {
    rewrite ^/?c/([^/]+)/?$ /search_result.php?category_id=$1 break;
}

location /d {
    rewrite ^/d/([^/.]+)/?$ /detail.php break;
}

但出于某种原因,当我浏览详细信息页面(重写)时,nginx提供源代码php文件而不是执行它..

这是我的原始位置部分

    # Main Settings
location / {
    root   /home/sites/mysite.com;
    index  index.php;

    try_files $uri $uri/ /index.php$is_args$args;

    location ~ \.php$ {
        fastcgi_pass  unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_read_timeout 300; 
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
        gzip_vary on; 
    } 
}

1 个答案:

答案 0 :(得分:1)

将URI重写为last需要使用break而不是.php,因为location在不同的location /块中处理。有关详细信息,请参阅this document

您可以将规则放在现有location /块内或其外部。不要创建新的nginx -T块,因为这会导致错误。

使用rewrite ^/c/([^/]+)/?$ /search_result.php?category_id=$1 last; rewrite ^/d/([^/.]+)/?$ /detail.php last; location / { root /home/sites/mysite.com; index index.php; try_files $uri $uri/ /index.php$is_args$args; location ~ \.php$ { ... } ... } 测试您的配置。

例如:

public void onPageFinished(WebView view, String url) {
    CookieSyncManager.getInstance().sync();
}