不要重写特定的子文件夹

时间:2014-08-28 15:12:07

标签: regex .htaccess mod-rewrite

我使用以下.htaccess重写我的index.php文件的所有网址,如果它们不是实际的文件或文件夹:

Options +FollowSymLinks
RewriteEngine On

// This bit redirect www.mydomain.com to mydomain.com
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

// This bit redirects all requests to index.php if a file or directory
// bearing that name doesn't exist
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php

我现在在我的网站上添加admin部分,我想重写此特定网址:

mydomain.com/admin

我希望将此案件视为默认情况(在此情况下默认为mydomain.com/admin/index.php

我试图自己解决这个问题,但regex对我来说仍然是陌生的......

1 个答案:

答案 0 :(得分:1)

您可以使用:

Options +FollowSymLinks
RewriteEngine On

// This bit redirect www.mydomain.com to mydomain.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE]

// This bit redirects all requests to index.php if a file or directory
// bearing that name doesn't exist OR it doesn't start with /admin/
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule !^admin/ ./index.php [L,NC]