[英]How to convert rewrites from advanced .htaccess to nginx conf rules
最近从Apache切换到Nginx。 这在apache下工作得很好,但是不知道如何为nginx添加它。 曾尝试htaccess到nginx转换器,但它们使我重定向循环。
我在根目录中有WordPress,在子目录下有自定义代码。
这是Apache上的.htaccess文件。
# rewrite engine on and setting up base
RewriteEngine On
RewriteBase /leffa/
# replace + with _
RewriteRule ^(.+?)\+(.+)$ $1-$2 [R=301,L,NE]
# external redirect from action URL to pretty one
RewriteCond %{THE_REQUEST} /index(?:\.php)?\?q=([^\s&]+)&(kuvauksesta)= [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /index(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ %1? [R=302,L,NE]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual URL (extra parameter)
RewriteRule ^([^/.]+)/([^/.]+)?$ index.php?q=$1&$2=1 [L,QSA]
# internal forward from pretty URL to actual URL
RewriteRule ^(.+)/?$ index.php?q=$1 [L,QSA]
这会将所有网址(例如http://www.rollemaa.org/leffa/index.php?q=the+boy)重写为漂亮的网址,例如http://www.rollemaa.org/leffa/the-boy 。
情况是,我已经建立了这样的主文件:
server {
listen 80;
access_log /var/log/nginx/rollemaa.access.log;
error_log /var/log/nginx/rollemaa.error.log;
root /var/www/rollemaa.org/public_html;
index index.html index.htm index.php;
server_name rollemaa.org www.rollemaa.org;
include hhvm.conf;
include global/wordpress.conf;
# Ensure requests for pagespeed optimized resources go to the pagespeed
# handler and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
# Static File Caching
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
#location /leffa/ {
#try_files $uri $uri/ /leffa/index.php?q=$args;
#rewrite ^/leffa/(.+?)\+(.+)$ /leffa/$1-$2 redirect;
#rewrite ^/leffa/(.*)$ /leffa/%1/%2? redirect;
#rewrite ^/leffa/(.*)$ /leffa/%1? redirect;
#if (-e $request_filename){
# rewrite ^/leffa/([^/.]+)/([^/.]+)?$ /leffa/index.php?q=$1&$2=1 break;
#}
#rewrite ^/leffa/(.+)/?$ /leffa/index.php?q=$1 break;
#}
}
如您所见,我已经注释掉了重写部分,因为它不起作用。
那里有任何Nginx专家可以帮助我吗? 提前感谢!
您可以使用以下重写规则。
location /leffa/ {
index index.php index.html;
rewrite ^/leffa/([^/]*)/?$ /leffa/index.php?q=$1;
}
它将把/ leffa / the-boy /和/ leffa / the-boy之类的URL重写为/leffa/index.php?q=the-boy。 具有子子目录(例如/ leffa / the-boy / something-here)的URL将被忽略。
注意:URL中的+不会转换为空格(就像直接访问/leffa/index.php?q=the+boy时的情况一样)。 访问/ leffa / the + boy /将导致查询参数q为“ the + boy”。
如果要在查询字符串中使用空格,则必须对空格使用%20 URL编码格式。 / leffa / the%20boy和/ leffa / the%20boy /将导致查询参数q为“男孩”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.