.htaccess RewriteCond有多个服务器变量

时间:2014-08-25 17:50:08

标签: regex apache .htaccess mod-rewrite web

如何使用htaccess做这样的事情?

if (is_file($_SERVER['DOCUMENT_ROOT'].'/my_dir'.$_SERVER['REQUEST_URI']))
{
    // send file $_SERVER['DOCUMENT_ROOT'].'/my_dir'.$_SERVER['REQUEST_URI']
}
else
{
    // forward to front-end controller
}

1 个答案:

答案 0 :(得分:1)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On
RewriteBase /

# load /my_dir/<file> if exists
RewriteCond %{DOCUMENT_ROOT}/my_dir/$1 -f [NC]
RewriteRule ^(.*)$ /my_dir/$1.php [L]

# otherwise use front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]