具有Page Not Found问题的Htaccess重写规则

时间:2014-07-21 15:32:38

标签: php apache .htaccess mod-rewrite url-rewriting

关于重写规则的htaccess问题

我的文件夹中有3个文件

create.php
index.html
index.php

我把它作为重写规则的.htaccess

DirectoryIndex index.php
RewriteEngine On
RewriteBase /admin/

RewriteRule ^([^/]+)/([^/]+)/?$ permissions/index.php?action=$1&id=$2 [L,QSA]


RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

问题是我想访问该网站

http://member.mydomain.com/admin/permissions/create

但它告诉我

The requested URL /admin/create.php was not found on this server.

我的.htaccess有什么问题,重写规则主要是让index.php进行查询发送,我希望我的create.php可以通过

的路径访问
 http://member.mydomain.com/admin/permissions/create

谢谢 !!

更新了.htaccess

DirectoryIndex index.php
RewriteEngine On
RewriteBase /admin/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^([^/]+)/([^/]+)/?$ permissions/index.php?action=$1&id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

但它返回相同的错误

Not Found

The requested URL /admin/permissions/create was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

1 个答案:

答案 0 :(得分:5)

在你的代码上你只有:

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

然而,在重定向之前,只是没有考虑天气或文件实际存在与否,您需要以下内容:

# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/admin/$1\.php -f
RewriteRule /([^/]+)/?$ $1.php [L]

您还需要将此规则放在规则的顶部。

这是整套规则:

Options +FollowSymLinks -MultiViews

DirectoryIndex index.php

RewriteEngine On
RewriteBase /admin/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/admin/$1\.php -f
RewriteRule /([^/]+)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?action=$1&id=$2 [L,QSA]

您不需要做任何规则。