Link to home
Start Free TrialLog in
Avatar of mbeckman
mbeckman

asked on

Apache mod_rewrite and Virtual Directories

Greetz,

Because I'm an Apache n00b, only because I've never attempted to setup my own server, I'm in need of some help.  The guy who hosts my website will modify the Apache configuration to whatever I ask, but he needs to know the configuration entries because he doesn't normally do this himself.  

What I'm looking for is the entries I would need in the Apache configuration for my website, so that people can go to http://www.mydomain.com/Non_Existant_Directory/Non_Existant_Sub_Directory/ and be redirected through a PHP file I will write that will perform an action based on the value of the parsed URL.  We've tried just changing the DocumentRoot field to /absolute/path/nav.php, and this works to some extent, but of course we run into the problem of directories and files that are being directed to this file that we don't want to be (such as images).  

I've been checking around the internet, and the general idea that I surmised is to use mod_rewrite in some way with the rewrite engine to perform this.  However, since I don't have access to my own Apache server, I can't experiment to figure out all the ins-n-outs of the configuration design I need.  So, I'm hoping all ye l337 sysadmins can help me out here.

What I need in the Apache configuration:
o   Server takes non-existant directories (such as mentioned above) and sends them through nav.php.
o   If the above isn't possible (mapping non-existant), a way to include certain fake directories (such as /Fake_Dir/)
o   Exclude certain directories (such as /images/)
o   I don't want _everything_ parsed through the nav.php file, only the fake directories I specified.
o   ...oh and maybe some tips on how to use your configuration.  LoL!

Directories that need to be excluded from rewrite and able to be viewed normally:
/images/
/cgi-bin/
/cp/

Some directories that need to be included in the rewrite:
/Admin/
/Programming/ASPNET/

Examples of what I'm looking to do:
/Programming/VB/123456.html    ( will run through nav.php from where I can parse the URI and use ID #123456 )


I think that's all I need.  Although I'm not totally sure at the moment.  Thanks for the help, and I appreciate whatever input you can provide.


Matt Beckman
http://howtocode.net
Avatar of Mercantilum
Mercantilum
Flag of Japan image

Hi Matt,

There is another way you could explore is to change the default "Error 404" page to be displayed (i.e. put your own "fake" page).

Regarding mod_rewrite, I just participated to a similar but different thread about it's usage ; basically the links reference:

  http://httpd.apache.org/docs/misc/rewriteguide.html
  http://www.bugzilla.org/docs214/html/rewrite.html
  http://www.fluidthoughts.com/howto/mod_rewrite/

Regarding your problem in particular, I suggest an easy way in terms of reading and changing/updating:
- we will first list the directory that are not fake and that you want to keep
- then we will redirect all the others to a specific subdirectory.
(rules are read from top to bottom, a rule with [L] option indicates to stop evaluating the next rules, as for Last rule)

1. ensure the engine is on

RewriteEngine On

2. rules to true directories, images, cgi-bin and cp

RewriteRule  ^(http://www.mydomain.com/images/.*)$  $1  [L]    
RewriteRule  ^(http://www.mydomain.com/cgi-bin/.*)$  $1  [L]    
RewriteRule  ^(http://www.mydomain.com/cp/.*)$  $1  [L]

3. rule for faking, i.e. anything else

RewriteRule  ^http://www.mydomain.com/(.*)$  http://www.mydomain.com/cgi-bin/nav.php?$1  [L]    

I'm not sure this is exactly what you need, this will call nav.php in cgi-bin with the rest of the URL given by the client as argument.
This way you can parse it and take action.  The 3. could have been written taking your 2 sample dirs, Admin and Programming/ASPNET:
RewriteRule  ^http://www.mydomain.com/Admin/(.*)$  http://www.mydomain.com/cgi-bin/nav.php?$1  [L]    
RewriteRule  ^http://www.mydomain.com/Programming/ASPNET/(.*)$  http://www.mydomain.com/cgi-bin/nav.php?$1  [L]
Doing the same for only these two directories.

I don't think you need a "redirect" [R] since it is on the same server.

Regards
Avatar of mbeckman
mbeckman

ASKER

Thank you for the response.  I'll get a hold of my buddy and have him try out the configuration to make sure it works.  If that works, I'll make sure to give you the points.  Otherwise, I'll probably be back with some more questions.  :)  Give me a couple days, he's kind of hard to get a hold of.


Matt Beckman
http://howtocode.net
Tried the following:

RewriteEngine On
RewriteRule  ^(http://www.domain.com/images/.*)$  $1  [L]    
RewriteRule  ^(http://www.domain.com/cgi-bin/.*)$  $1  [L]    
RewriteRule  ^(http://www.domain.com/cp/.*)$  $1  [L]
RewriteRule  ^http://www.domain.com/(.*)$  http://www.domain.com/nav.php?$1  [L]

In place of www.domain.com we had our real domain name of course, but it didn't work.  The nav.php file wasn't getting called.  Any idea why?
1. are you using VirtualHost ?
If yes, all the lines/rules above have to be located inside your virtual host directives

2. please change the rules as

RewriteEngine On
RewriteRule  ^(/images/.*)$  $1  [L]    
RewriteRule  ^(/cgi-bin/.*)$  $1  [L]    
RewriteRule  ^(/cp/.*)$  $1  [L]
RewriteRule  ^/(.*)$  http://www.domain.com/nav.php?$1  [L,R]

wanted to get a safer url, but cannot match seemingly (tried on my server)
Note the ,R for the last rule.
The rule 1. is important as if you use virtual hosting none of mod_rewrite will work.  (only if you use virtual hosts)

3. logging
add

RewriteLog "/var/log/httpd/rewrite.log"
RewriteLogLevel 2

to your config if you want to see the log of what's happening.
If nothing gets logged you are likely to be in case 1. (virtual hosts)

Regards
Mercantilum:

In your RewriteRule for the "don't rewrite" directories where you used:   ^(/images/.")$  $1  [L]   Are you sure I can't use the full URLs, like ^(http://www.domain.com/images/.")$  The reason for this is that on my server, my root directory has about 13 separate folders for domains/sub-domains, and I think if the apache configuration was set in the root, that suddenly I would have all my other websites being affected by this rewrite engine.

I think it may actually be something with regard to the VirtualHost. That's one detail I didn't tell my friend (where to put the rewrite stuff), so I'll double-check to make sure he put it in that area.
Ooops one more IMPORTANT thing: by using R (last rule),

The rule
RewriteRule  ^/(.*)$  http://www.domain.com/nav.php?$1  [L,R]
May loop
Because it is a redirect, and it will be rechecked for matching... so the rule should be

RewriteRule  ^/(nav.php.*)$  $1  [L]
RewriteRule  ^/(.*)$  http://www.domain.com/nav.php?$1  [L,R]

This will match the nav.php and prevent a loop.
Ouch! not that simple, I think it will work like this.
Alright.  Too bad I didn't have access to my Apache configuration so I could play around with this myself.  :)


Matt
ASKER CERTIFIED SOLUTION
Avatar of Mercantilum
Mercantilum
Flag of Japan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of periwinkle
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:

    ACCEPT: Mercantilum

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Periwinkle
EE Cleanup Volunteer