将重写从htaccess转换为nginx

时间:2017-05-24 13:49:07

标签: .htaccess nginx

我有.htaccess

RewriteRule ^thumb/(.*)x(.*)/r/(.*) thumb.php?w=$1&h=$2&src=$3
RewriteRule ^medias/(.*) files.php?file=$1

RewriteRule ^sitemap\.xml$ index.php [L]
RewriteRule ^(.*)(\.html|\.htm)$ index.php [L]
RewriteRule ^(.*)(\.rss|\.atom|\.txt)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]

Nginx重写是:

rewrite ^/thumb/(.*)x(.*)/r/(.*) /thumb.php?w=$1&h=$2&src=$3;
rewrite ^/medias/(.*) /files.php?file=$1;
rewrite ^/sitemap\.xml$ /index.php break;
rewrite ^/(.*)(\.html|\.htm)$ /index.php break;
rewrite ^/(.*)(\.rss|\.atom|\.txt)$ /index.php break;

访问类别时,我会看到主页。

请帮助

由于

1 个答案:

答案 0 :(得分:0)

Nginx配置通常有点不同,有很多位置块。

我很难对此进行测试,但这可能有助于你前进:

# Nginx excludes the query string (part after the ?) before trying to match
location = /index.php {
    # FastCGI PHP stuff here
}

location ~ ^/thumb/([^/]+)x([^/]+)/r/([^/]+) {
    rewrite ^ /thumb.php?w=$1&h=$2&src=$3 break;
}

location ~ ^/medias/(.*) {
    rewrite ^ /files.php?file=$1 break;

    # To be honest I would try to avoid serving files through PHP. It's much more efficient to let Nginx serve them directly with something like:
    root /path/to/media's/parent;
    try_files $uri =404;
}

location ~ ^/sitemap\.xml$ {
    rewrite ^ index.php break;
}

location ~ (\.html|\.htm|\.rss|\.atom|\.txt)$ {
    rewrite ^ index.php break;
}

# Catch-all
location / {
    try_files $uri index.php;
}

你必须使用正则表达式来测试扩展,这意味着它们必须使用正则表达式。这是因为具有正则表达式的那些块在最长前缀块之前被评估。请务必阅读Nginx' documentation about location blocks以获得更好的解释,并附上示例。