Google Answers Logo
View Question
 
Q: mod_rewrite -> redirecting everything to a handler except a few folders ( Answered,   0 Comments )
Question  
Subject: mod_rewrite -> redirecting everything to a handler except a few folders
Category: Computers > Programming
Asked by: wiggum24-ga
List Price: $5.00
Posted: 11 Aug 2005 23:56 PDT
Expires: 10 Sep 2005 23:56 PDT
Question ID: 554812
G'day :)

I'm looking for some httpd.conf entries (specifically regular expressions).

I'd like to use mod_rewrite to send every request to handler.php,
except for a couple of folders. (images,includes,custom)
Answer  
Subject: Re: mod_rewrite -> redirecting everything to a handler except a few folders
Answered By: palitoy-ga on 12 Aug 2005 00:34 PDT
 
Hello wiggum24-ga

Thank-you for your question.

Using mod_rewrite can be complicated and usually requires a little
tweaking before you get the rules exactly how you wish them to be,
these things are notorious for not getting them exactly right first
time!  There is an excellent manual on mod_rewrite which I usually use
to try and solve these problems, this can be found here:

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

In your case we need to use conditional rewrites where if the request
is not one of 3 folders it is redirected.  To do this we need a
regular expression that checks if it IS one of the folders and then
reversing this.  The reversing is done with the use of an ! symbol in
the code given below.

In my attempt below this is what is going on:
1) The rewrite engine is switched on so that we can rewrite the URL
2) A trailing slash is added if necessary to the URL
3) Check it is not the images directory
4) Check it is not the includes directory
5) Check it is not the custom directory
6) Match the query string
7) Redirect the URL to the handler.php and attach any query string

==================================================

RewriteEngine on
RewriteRule ^(.*[^/])$ /$1/ [L]
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/includes/
RewriteCond %{REQUEST_URI} !^/custom/
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.+)$ /handler.php?args=%1 [L] 

==================================================

Please let me know how you get on with this, if it needs any tweaking
please ask for clarification and I will do my best to respond swiftly.

Request for Answer Clarification by wiggum24-ga on 12 Aug 2005 16:41 PDT
I added the lines to httpd.conf and still no joy.

That configuration gives me a 404 when I try and access 
http://www.mydomain.com/images/text.txt

If I comment it all out, restart apache, and then request the same
file, it retrieves it fine.

If I request:
http://www.mydomain.com/random/url
It gives me a 404 as well, instead of handler.php

Request for Answer Clarification by wiggum24-ga on 12 Aug 2005 17:01 PDT
I made some uneducated changes to your answer (hack and see if it
works) and I came up with this working combination.

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/includes/
RewriteCond %{REQUEST_URI} !^/custom/
RewriteRule ^(.*)$ /handler.php

The only querk I'm experiencing is because line 2 is missing

Requests to /images gets handler.php
Requests to /images/ gets /images/index.html

If I add line 2 though, everything goes pear shaped and stops working.

Clarification of Answer by palitoy-ga on 13 Aug 2005 00:27 PDT
What you have done so far is correct and is what I would have done
also!  From your clarification request I think the only thing we need
to solve now is when someone neglects to put a trailing slash on the
URL.  At the moment the solution is redirecting to the handler.php
file instead of the desired index.html file.

This can be achieved by adding the following lines:

RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteRule (.+) /$1/ [R=301,L]

An alternative solution would be something much more explicit for your
server and only add the trailing slash for images, includes or custom.

Let me know how you get along with this solution.  These types of
questions are always tricky to answer first time without access to the
server in question so thank-you for bearing with me whilst we solve
this together.

This should make the total solution now:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteRule (.+) /$1/ [R=301,L] 

RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/includes/
RewriteCond %{REQUEST_URI} !^/custom/
RewriteRule ^(.*)$ /handler.php
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy