I'm looking to use .htaccess to redirect to one page or another based on if a file exist开发者_开发知识库s in the directory.
Basically I need all visitors to index.php to be sent to either home.php page if splash.php does not exist or splash.php page if splash.php does exist in the directory, so far i have this...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} index.php
RewriteCond %{DOCUMENT_ROOT}/splash.php -f
RewriteCond %{SCRIPT_FILENAME} !splash.php
RewriteRule ^.*$ /splash.php [L]
This sends visitors to mysite.com/splash.php if it exists only if I specifically have mysite.com/index.php as my URL, it doesn't work if just use the mysite.com URL.
I understand the first 3 lines of my code, but not the !splash.php -f bit (i got that from some other redirect code i found on SO).
How do i redirect to home.php if splash.php not present?
Cheers!
Try this rule. It should work on mysite.com/index.php, mysite.com/ or mysite.com
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/splash.php -f
RewriteCond %{SCRIPT_FILENAME} !splash.php
RewriteRule ^(index\.php|/|)$ /splash.php [L]
You'll need to break it into multiple blocks, so try this (goes to splash.php if it exists, regardless of whether or not they have index.php in the file name, and then if splash doesn't exist goes to home.php):
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/splash.php -f
RewriteCond %{SCRIPT_FILENAME} !splash.php
RewriteRule ^.*$ /splash.php [L]
RewriteCond %{DOCUMENT_ROOT}/splash.php !-f
RewriteRule ^.*$ /home.php [L]
mate, I added a quick .htaccess and it works perfectly for me. I'm not sure what might be different in your configuration though. To avoid any confusion, here's the Apache (2) vhost that I'm using:
<VirtualHost *:80>
DocumentRoot "/usr/local/zend/apache2/htdocs/test-setup"
ServerName test-setup
ErrorLog /var/log/apache2/lps-version-one_test-setup_error_log
LogLevel warn
CustomLog /var/log/apache2/lps-version-one_test-setup_access_log combined
<Directory "/usr/local/zend/apache2/htdocs/test-setup">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
When I have both files in the directory, I'm redirected to splash.php (even though the name of the file in the url doesn't change - internally I'm redirected to splash.php). When I rename splash.php (or remove it) I only see index.php. When I put it back, the redirection works as normal. When I specifically put index.php in the url, with both files available, I'm redirected to splash.php.
Fwiw, one of the best things to remember about mod_rewrite is this:
Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo. -- Brian Moore bem@news.cmc.net
精彩评论