将 apache htaccess 转换为 nginx
converting apache htaccess to nginx
我正在尝试将 apache htaccess 规则转换为 nginx 配置,但它不起作用。以下是规则和 nginx 配置的详细信息:
.htaccess 规则
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
上面的 Nginx 配置
server {
listen ip_address:80;
server_name www.example.com;
access_log /var/log/nginx/example.com-access.log;
error_log /var/log/nginx/example.com-error.log;
root /var/www/html/example.com/;
index index.php;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/ break;
}
}
当我访问网站时它会打开主页,当我单击其他菜单时它显示 404 未找到。
错误日志如下:
2015/03/18 05:31:56 [error] 3550#0: *7 open() "/var/www/html/example/index.php//contents.html" failed (20: Not a directory), client:
1.2.3.4, server: www.example.com, request: "GET /contents.html HTTP/1.1", host: "www.example.com", referrer: "http://example.com/"
有什么想法吗???
Shoaib..
你的 nginx 重写是错误的——因为最后的斜杠,nginx 认为 index.php 是一个文件夹。你可以这样做:
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?url= break;
}
}
然后在您的 PHP 代码中解析 $_REQUEST['url']
还有一种可能,很多站长都是这样做的:
location / {
try_files $uri $uri/ /index.php?$args;
}
意思——首先尝试打开一个文件(如果存在),尝试打开一个文件夹(如果存在),最后一招——传递给index.php。 $args 将包含查询字符串,您可以通过 $_SERVER 访问原始 URL。
问题已解决,我们在网站的 config.php 文件中启用了以下功能,
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
nginx配置如下,
location / {
try_files $uri $uri/ /index.php?$args;
}
有效...
感谢@Denis 和@SuddenHead 的回复。
我正在尝试将 apache htaccess 规则转换为 nginx 配置,但它不起作用。以下是规则和 nginx 配置的详细信息:
.htaccess 规则
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
上面的 Nginx 配置
server {
listen ip_address:80;
server_name www.example.com;
access_log /var/log/nginx/example.com-access.log;
error_log /var/log/nginx/example.com-error.log;
root /var/www/html/example.com/;
index index.php;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/ break;
}
}
当我访问网站时它会打开主页,当我单击其他菜单时它显示 404 未找到。
错误日志如下:
2015/03/18 05:31:56 [error] 3550#0: *7 open() "/var/www/html/example/index.php//contents.html" failed (20: Not a directory), client:
1.2.3.4, server: www.example.com, request: "GET /contents.html HTTP/1.1", host: "www.example.com", referrer: "http://example.com/"
有什么想法吗???
Shoaib..
你的 nginx 重写是错误的——因为最后的斜杠,nginx 认为 index.php 是一个文件夹。你可以这样做:
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?url= break;
}
}
然后在您的 PHP 代码中解析 $_REQUEST['url']
还有一种可能,很多站长都是这样做的:
location / {
try_files $uri $uri/ /index.php?$args;
}
意思——首先尝试打开一个文件(如果存在),尝试打开一个文件夹(如果存在),最后一招——传递给index.php。 $args 将包含查询字符串,您可以通过 $_SERVER 访问原始 URL。
问题已解决,我们在网站的 config.php 文件中启用了以下功能,
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
nginx配置如下,
location / {
try_files $uri $uri/ /index.php?$args;
}
有效...
感谢@Denis 和@SuddenHead 的回复。