htaccess重定向如果文件夹存在,则重定向到另一个文件夹

时间:2015-01-20 09:37:12

标签: php .htaccess web

我想将htaccess重定向到某个文件夹'setup'(如果存在)。

否则它必须按原样执行股票标准重定向:

RewriteEngine on
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/$1 [L]

RewriteBase /
RewriteRule ^$ /site [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /api/index.php [L]

现在我的htaccess-Fu几乎不在它应该的位置。我阅读了我可以在SO上找到的帖子,但没有一个对我的具体案例有帮助。

然后我有一个子问题:在你的htaccess文件中永久保存这个是不好的做法吗?看来它只会在您第一次设置网站时存在?

1 个答案:

答案 0 :(得分:0)

我经常看到人们通过PHP这样做,但通过htaccess做这件事看起来确实是一个很好的选择!

所以你要找的是:

RewriteEngine on

# The part your looking for: If setup is present and you're not in it redirect there!
# If the setup directory exists in the document root...
RewriteCond %{DOCUMENT_ROOT}/setup -d 
# ...and you're not in setup..
RewriteCond %{REQUEST_URI} !(setup) [NC] 
# ...redirect to setup - with a 302 (temporary redirect) to make sure the browser won't cache the redirect.
RewriteRule ^(.*) /setup [L,redirect=302]

# the rest of your htaccess should go here... for instance:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]