Solved

Apache mod_rewrite and Virtual Directories

Posted on 2004-04-16
10
365 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
Microsoft will be releasing the Windows 10 Creators Update in just a matter of weeks. Are you prepared? Follow these steps to ensure everything goes smoothly and you don't lose valuable data on your PC.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

726 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