Я два дня пытался преобразовать .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 не имеет $0
переменная, которая используется в Apache для всего соответствия шаблону регулярного выражения в Apache.
В nginx вы получаете эквивалентную строку, используя $request_uri
.
Итак, вы должны использовать такую конфигурацию:
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;
Я также исправил здесь регулярное выражение скрытых файлов.
Регулярное выражение Nginx rewrite не имеет переменной $ 0. Вы должны заменить его на то, что он поддерживает.
Пожалуйста, используйте
$uri or $request_uri
вместо 0 долларов.