Go Premium for a chance to win a PS4. Enter to Win


Apache mod_rewrite and Virtual Directories

Posted on 2004-04-16
Medium Priority
Last Modified: 2013-11-18

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:

Some directories that need to be included in the rewrite:

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
Question by:mbeckman
  • 4
  • 4
LVL 10

Expert Comment

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:


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.


Author Comment

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

Author Comment

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?
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 10

Expert Comment

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

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)


Author Comment

ID: 10865894

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.
LVL 10

Expert Comment

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.

Author Comment

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

LVL 10

Accepted Solution

Mercantilum earned 1200 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>
LVL 15

Expert Comment

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.

EE Cleanup Volunteer

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
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…
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)

927 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