我已经尝试了两天将.htaccess转换为nginx重写原始文件如下所示:
# Turn on URL rewriting RewriteEngine On RewriteBase // # Protect hidden files from being viewed <Files .*> Order Deny,Allow Deny From All </Files> # Protect application and system files from being viewed RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L] # List of files in subdirectories will not be displayed in the browser Options -Indexes # Allow any files or directories that exist to be displayed directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d AddDefaultCharset utf-8 AddCharset UTF-8 .htm .html .txt AddType "text/html; charset=UTF-8" .htm .html .txt AddType "text/css; charset=UTF-8" .css AddType "text/javascript; charset=UTF-8" .js # Rewrite all other URLs to index.php/URL RewriteRule .* index.php/$0 [PT]
我设法想出了这个:
# nginx configuration charset utf-8; autoindex off; location /application { rewrite ^/(?:application|modules|system)\b.* /index.php/$0 break; } location /modules { rewrite ^/(?:application|modules|system)\b.* /index.php/$0 break; } location /system { rewrite ^/(?:application|modules|system)\b.* /index.php/$0 break; } location / { if (!-e $request_filename){ rewrite ^(.*)$ /index.php/$0; } } location ~ \.* { deny all; }
但是当我重新启动nginx时,我得到:
[emerg] unknown "0" variable nginx: configuration file /etc/nginx/nginx.conf test failed
我不明白这是为什么。 有人可以请求帮助我吗?
nginx没有$0variables,它在Apache中用于Apache中的整个正则expression式模式匹配。
在nginx中,通过使用$request_uri来获得等价的string。
所以,你应该使用这样的configuration:
charset utf-8; autoindex off; location ~ /\.* { deny all; } location ~ /(?:application|modules|system) { return 301 /index.php$request_uri; } # Try first the actual files, if they do not exist, then try $request_uri via `index.php`. try_files $uri $uri/ /index.php/$request_uri;
我也在这里修复了隐藏文件的正则expression式。
Nginx重写正则expression式没有$ 0variables。 您必须用支持的东西来replace它。
请用
$uri or $request_uri
而不是$ 0。