Solved

Apache mod_rewrite and Virtual Directories

Posted on 2004-04-16
10
363 Views
Last Modified: 2013-11-18
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
0
Comment
Question by:mbeckman
  • 4
  • 4
10 Comments
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10848460
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
0
 
LVL 2

Author Comment

by:mbeckman
ID: 10856698
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
0
 
LVL 2

Author Comment

by:mbeckman
ID: 10864739
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?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 10

Expert Comment

by:Mercantilum
ID: 10865826
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
0
 
LVL 2

Author Comment

by:mbeckman
ID: 10865894
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.
0
 
LVL 10

Expert Comment

by:Mercantilum
ID: 10865905
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.
0
 
LVL 2

Author Comment

by:mbeckman
ID: 10865916
Alright.  Too bad I didn't have access to my Apache configuration so I could play around with this myself.  :)


Matt
0
 
LVL 10

Accepted Solution

by:
Mercantilum earned 300 total points
ID: 10865935
About your question:
Yes, but if you have many websites on the same server, you are likely to use <VirtualHost *>
In this case, the mod_rewrite rules will be set and true only for the virtual hosts in which you put the directives.

If you want the rewrite rules to apply to several virtual hosts, you have to put the rules in each <virtual host>
0
 
LVL 15

Expert Comment

by:periwinkle
ID: 11761203
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
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question