网址重写难度

时间:2014-04-08 17:51:24

标签: php apache .htaccess nginx rewrite

我想要实现的是,如果不存在直接路径,则重写为index.php。以下是它在apache语法中的外观

# Allow any files or directories that exist to be displayed directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d



# Rewrite all other URLs to index.php/URL

RewriteRule .* index.php/$0 [PT]

我的nginx conf看起来像那样:

location / {
        try_files $uri $uri/ /index.php$uri?$args;
}

但它查找文件,如果不存在则返回错误文件未找到。我做错了什么?

2 个答案:

答案 0 :(得分:0)

这对我有用:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?req=$1 [L]

答案 1 :(得分:0)

试试这个:

location / {
    rewrite ^(.*)$ /index.php/$1;
}