htAccess 重定向(如果文件夹存在),否则重定向到另一个文件夹


htaccess redirect if folder exists else redirect to another folder

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

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

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]

现在我的傅已经快到它应该在的地方了。我阅读了我在SO上可以找到的帖子,但没有一个对我的具体情况有帮助。

然后我有一个子问题:将其永久保存在您的htaccess文件中是不好的做法吗?看到它仅在您第一次设置站点时存在?

我经常看到人们通过 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]