Adr*_*ian 4 mod-rewrite nginx .htaccess
我已经尝试了两天将 .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]
Run Code Online (Sandbox Code Playgroud)
我想出了这个:
# 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;
}
Run Code Online (Sandbox Code Playgroud)
但是当我重新启动 nginx 时,我得到:
[emerg] unknown "0" variable
nginx: configuration file /etc/nginx/nginx.conf test failed
Run Code Online (Sandbox Code Playgroud)
我不明白这是为什么。有人可以帮我解决这个问题吗?
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;
Run Code Online (Sandbox Code Playgroud)
我还在这里修复了隐藏文件正则表达式。
| 归档时间: |
|
| 查看次数: |
3434 次 |
| 最近记录: |