htaccess重写规则不适用于某些页面

时间:2013-09-25 20:28:22

标签: regex .htaccess mod-rewrite url-rewriting rewrite

我对重写规则有一些困难。我的.htaccess看起来像这样:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule admin/add-category$ adm_add_category.php [NC]
RewriteRule admin/categories-management$ adm_categories_management.php [NC]
RewriteRule admin/user-settings/(.*)$ adm_user_settings.php?update=$1 [NC]
RewriteRule admin/website-settings/(.*)$ adm_website_settings.php?update=$1 [NC]
RewriteRule admin/users-management$ adm_users_management.php [NC]
RewriteRule admin/websites-management$ adm_websites_management.php [NC]

RewriteRule settings/profile$ changesettings.php [NC]
RewriteRule settings/websites$ my_websites.php [NC]
RewriteRule settings/password$ changepassword.php [NC]
RewriteRule settings/logout$ logout.php [NC]

RewriteRule pages/index$ index.php [NC]
RewriteRule pages/access$ access.php [NC]
RewriteRule pages/submit-url$ submit_url.php [NC]
RewriteRule pages/online-users$ online_users.php [NC]
RewriteRule pages/register$ register.php [NC]
RewriteRule pages/websites$ websites_list.php [NC]
RewriteRule pages/websites/(.*)$ websites_list.php?page=$1 [NC]
RewriteRule pages/contact$ contact.php [NC]

RewriteRule search/(.*)$ search.php?term=$1 [NC]
RewriteRule category/(.*)$ category.php?name=$1 [NC]    

RewriteRule profile/(.*)$ profile.php?username=$1 [NC]
RewriteRule website/(.*)$ website.php?name=$1 [NC]


除了以下内容之外的所有重写都在起作用:

RewriteRule search/(.*)$ search.php?term=$1 [NC]
RewriteRule category/(.*)$ category.php?name=$1 [NC]    

RewriteRule profile/(.*)$ profile.php?username=$1 [NC]
RewriteRule website/(.*)$ website.php?name=$1 [NC]

尝试加载特定的配置文件时,例如/ profile / exampleguy,会显示404错误。有谁知道会导致什么?

404看起来像:

The requested URL /profile/exampleguy was not found on this server. 

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

如果您需要更多信息,请告诉我,我非常感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

在每个规则中添加L标记,使其成为[NC,L],然后添加:

Options +FollowSymLinks -MultiViews

位于.htaccess

的顶部

答案 1 :(得分:1)

您是否尝试过在某些重写条件下重定向到默认页面?

RewriteCond %{REQUEST_FILENAME} !-d  //Rewrites if the directory doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f  //Rewrites if the file doesn't exist

RewriteRule ^(.*)$ index.php?$1 [L,QSA]  // Change this to suit your needs

另外,我猜你得到的是404,因为像/ profile /这样的目录实际存在,但不是“exampleguy”,如果这些目录中没有另一个.htaccess来捕获问题,那么服务器将尝试找到页面(并失败......)