htaccess 重定向如果文件夹存在否则重定向到另一个文件夹
htaccess redirect if folder exists else redirect to another folder
我想使用 htaccess 重定向到某个文件夹 'setup'(如果存在)。
否则它必须按原样执行股票标准重定向:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/ [L]
RewriteBase /
RewriteRule ^$ /site [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /api/index.php [L]
现在我的 htaccess-Fu 离它应该的位置还很远。我阅读了可以在 SO 上找到的帖子,但其中 none 对我的具体案例有所帮助。
然后我有一个子问题:将它永久保存在您的 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]
我想使用 htaccess 重定向到某个文件夹 'setup'(如果存在)。
否则它必须按原样执行股票标准重定向:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/ [L]
RewriteBase /
RewriteRule ^$ /site [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /api/index.php [L]
现在我的 htaccess-Fu 离它应该的位置还很远。我阅读了可以在 SO 上找到的帖子,但其中 none 对我的具体案例有所帮助。
然后我有一个子问题:将它永久保存在您的 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]