.htaccess URL重写在Nginx中不起作用

.htaccess URL重写在Nginx中不起作用,.htaccess,nginx,ubuntu-12.04,.htaccess,Nginx,Ubuntu 12.04,URL重写在Nginx中不起作用,操作系统是Ubuntu 12.4 Lts 打开时,它正在工作 但当我试着打开时,却没有工作 404找不到 nginx/1.1.19 .htaccess <IfModule !mod_rewrite.c> ErrorDocument 500 "mod_rewrite must be enabled" </IfModule> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f R

URL重写在Nginx中不起作用,操作系统是Ubuntu 12.4 Lts

打开时,它正在工作 但当我试着打开时,却没有工作

404找不到

nginx/1.1.19

.htaccess

<IfModule !mod_rewrite.c>
    ErrorDocument 500 "mod_rewrite must be enabled"
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?u=$1

@NanheKumar的答案改写正确,但它忽略了htaccess中的前2条规则

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
这意味着检查请求是否与文件和目录不匹配,要模拟这种行为,您可以像这样使用
try\u files

location / {
    try_files $uri $uri/ /index.php?u=$request_uri;
}
这将确保首先服务直接指向资产或目录的请求,如果两者都不将请求传递到
index.php


编辑:除非
index.php
能够为资产提供服务,否则这将导致所有资产(图像、css、javascript等)显示错误,因为
index.php
将接收到它不期望的参数。想一想像这样的
http://example.com/index.php?u=/images/background.jpg

我知道这是很久以前的事了,但你能解释一下吗?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
location / {
    try_files $uri $uri/ /index.php?u=$request_uri;
}