将apachehtaccess转换为nginx
我正在尝试将ApacheHTAccess规则转换为nginx配置,但它不起作用。以下是规则和nginx配置的详细信息: .htaccess规则将apachehtaccess转换为nginx,apache,.htaccess,nginx,Apache,.htaccess,Nginx,我正在尝试将ApacheHTAccess规则转换为nginx配置,但它不起作用。以下是规则和nginx配置的详细信息: .htaccess规则 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
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/$1 break;
}
}
RewriteBase/
RewriteCond%{REQUEST\u FILENAME}-f
RewriteCond%{REQUEST\u FILENAME}-d
RewriteRule^(.*)$index.php/$1[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/$1 break;
}
}
当我访问该网站时,它会打开主页,当我单击其他菜单时,它会给出404 not found
以下是错误日志:
2015/03/18 05:31:56[错误]3550#0:*7 open()“/var/www/html/example/index.php//contents.html”失败(20:不是目录),客户端:
1.2.3.4,服务器:www.example.com,请求:“GET/contents.html HTTP/1.1”,主机:“www.example.com”,推荐人:http://example.com/“
有什么想法吗
Shoaib..您的nginx重写是错误的–由于最后的斜杠,nginx认为index.php是一个文件夹。你可以这样做:
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?url=$1 break;
}
}
然后在PHP代码中解析$\u请求['url']
另一种可能性是,许多网站管理员都是这样做的:
location / {
try_files $uri $uri/ /index.php?$args;
}
意思是——首先尝试打开一个文件(如果存在),然后尝试打开一个文件夹(如果存在),最后一种方法是将其传递给index.php$args将包含查询字符串,您可以通过$\服务器访问原始URL 问题已经解决,我们在网站的config.php文件中启用了以下功能
$config['index_page']='';
$config['uri_protocol']='REQUEST_uri'代码>
和nginx配置,如下所示
位置/{
try_files$uri$uri//index.php?$args;
}
它起作用了
感谢@Denis和@SuddenHead的回复。/var/log/nginx/hostname.com-error.log可以说明问题所在。我为回复Denis添加了错误日志。我以前使用过它,但它不适用于网站上的其他链接,日志文件中也没有错误。和我现在的规则一样。主页工作正常,但当我单击其他链接时,它不工作。@Shoaib你的链接是什么样的?你能举1-2个例子吗?确定它像,(主页)(当前在apache for members页面上显示的实际url)@Shoaib和你的apache重写规则(RewriteRule^(.*)$index.php/$1[L])正在工作吗?实际上,它不应该起作用:)它起作用了。当我禁用该规则时,它给出404 not found。我已经和开发者谈过了,他说在这个网站上,router.php文件完成了其余的重写。我们将共同探讨这个问题。