Solved

Apache mod_rewrite and Virtual Directories

Posted on 2004-04-16
10
349 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 10

Expert Comment

by:Mercantilum
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 2

Author Comment

by:mbeckman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to fix error ""Failed to validate the vCentre certificate. Either install or verify the certificate by using the vSphere Data Protection Configuration utility" when you are trying to connect to VDP instance from Vcenter.
VM backup deduplication is a method of reducing the amount of storage space needed to save VM backups. In most organizations, VMs contain many duplicate copies of data, such as VMs deployed from the same template, VMs with the same OS, or VMs that h…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now