如果特定文件夹不存在,则htaccess重定向

时间:2012-06-16 11:53:41

标签: apache .htaccess redirect

我在root htaccess中使用它

RewriteEngine on
Options +FollowSymLinks

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule .* index.php?p=%{REQUEST_URI} [L]

这会成功重写http://example.com/notexistsfolder

但我还希望将http://example.com/user/notexistsfolder重定向到http://example.com/notexistsfolder

我应该添加什么代码来实现这一目标?

1 个答案:

答案 0 :(得分:0)

以下是.htaccess目录下DOCUMENT_ROOT所需的代码:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^user/(.*)$ $1 [L,NC]

RewriteRule (?!^user/)^(.*)$ index.php?p=$1 [L,NC,QSA]