Forum Moderators: phranque

Message Too Old, No Replies

Redirect to different pages

Is it possible to use .htaccess for conditional redirects

         

commbot

5:41 am on Jun 17, 2009 (gmt 0)

10+ Year Member



Hi there.

I have around 15 sites on my server that have recently been the target of scripts attempting RFI exploits and the like. Most are custom php/mysql, some are static html and some are Mambo/Joomla.

Based on the excellent advice found in the forum, I have created an .htaccess file to keep out these unwanted visitors with a combination of blocks for user-agent and IP, and all seems to be working.

For ease of maintenance, I have one .htaccess file which is linked to from each site folder so I can update centrally.

All well and good so far but I have run into a problem with the SEF rewrite required by the Mambo/Joomla sites. I have the correct redirect lines in the file so that works, and for the other php sites it works to redirect queries for non-existent pages to index.php so that's all good.

My problem arises with the static sites which do not have an index.php file, only index.html. Any false query throws up a 404 page, which again is not a major problem.

However, seeing as I'm so pleased at being able to centralise my .htaccess file what I'd like to do is improve on the redirect so it will check for index.php first and if that isn't there, will default to index.html

Sure I can put an index.php file in the half dozen folders that don't have one, or have two .htaccess files, one for each case, but I do like to keep things neat and tidy, as I'm sure you'll understanbd.

So, is this possible at all?

Here is the Joomla SEF redirect:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

commbot

7:41 am on Jun 17, 2009 (gmt 0)

10+ Year Member



Hmm, can't edit.

Anyway, just to update I did read the mod_rewrite manual and I tried to use the Skip directive like this,

RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*) index.php [S=1]
RewriteRule ^(.*) index.html

which worked fine on the static sites but managed to bork the original function so no joy there.

jdMorgan

1:22 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll find it quite helpful to use comments to document the purpose and function of your rules, and to implement the code based on that written description.

# If index.php does not exist, and index.html does
# exist, rewrite index.php requests to index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/index.html -f
RewriteRule ^index\.php$ /index.html [L]
#
# Rewrite requested URL=paths which do not resolve to existing
# files or directories to the index.php script
RewriteCond $1 !^index\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

Note that the new rule does not comprehend any possible complications in the filepath due to your ".htaccess centralisation" and may therefore need to be tweaked to correct the filepath.

Jim

g1smd

12:43 am on Jun 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** for the other php sites it works to redirect queries for non-existent pages to index.php so that's all good. ***

I find it much easier when the canonical root URL does not mention the index filename anywhere in the URL at all, but that's a whole new discussion.

commbot

1:28 am on Jun 18, 2009 (gmt 0)

10+ Year Member



@jdmorgan
many thanks, that works perfectly. just a question of getting things in the right order, as always.

thanks for the tip on comments, I generally do use them - time and experience has taught me that much - but didn't include them here for the sake of simplicity.

now if i can only figure out why mod_rewrite isn't working the same on my new test server as it does on the live site, i could be taking the weekend off...

cheers