Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Apache mod_rewrite help

This topic is 5515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If you know how to use mod_rewrite very well, then I need your help. I have two conditions, and both of them have almost identical rules. If you see a way of merging both of them into one, please let me know. Thanks. Here is the rewrite engine part of my config file:
RewriteEngine on

RewriteMap lowercase int:tolower

### EX: google.com (defaults to www.google.com)
# Use this to map somthing like nan2d.com (not www.nan2d.com, or any other
# subdomain) to /nan2d.com/www/. In other words, make the WWW subdomain default
# if none is given
# The magic here is done with the ``!'''', meaning NOT matching the pattern.
RewriteCond ${lowercase:%{SERVER_NAME}} !.*\.[a-zA-Z0-9]*\.[a-zA-Z0-9]*
RewriteRule (.*) ${lowercase:%{SERVER_NAME}}$1 [C]
RewriteRule ([a-zA-Z0-9]*\.[a-zA-Z0-9]*)/(.*) "C:/server/vhosts/$1/www/$2"

### EX: www.google.com, poop.nullsoft.com, ars.foo.yoyo.microsoft.com
# Only use this rule of the request is ([a-zA-Z0-9]*)\.foo\.com and not foo.com
RewriteCond ${lowercase:%{SERVER_NAME}} .*\.[a-zA-Z0-9]*\.[a-zA-Z0-9]*

# concatenate the virtual host name onto the start of the URI
# the [C] means do the next rewrite on the result of this one
RewriteRule (.*) ${lowercase:%{SERVER_NAME}}$1 [C]

# now create the real file name
# If the requested url is somthing like www.nan2d.com/yo/, then that
# would map to /nan2d.com/www/yo/
# This makes all of the lower-part subdomains part of the directory,
# so a URL like ars.shit.cunt.nan2d.com/whoa/ will map to
# /nan2d.com/ars.shit.cunt/whoa/
RewriteRule (.*)\.([a-zA-Z0-9]*\.[a-zA-Z0-9]*)/(.*) "C:/server/vhosts/$2/$1/$3"

Share this post

Link to post
Share on other sites
Apache 2

It works, but I just thought that it would be better if I had one condition instead of two, because both of them are very alike. The first one defines the rules needed if the second one (the second condition) does not match (via the `!'').

What this does is this:

If I get a request to, say http://test .nan2d.com then this will server files from the directory //server/vhosts/nan2d.com/test/ . The `test'' in that URL is the sub domain for the nan2d.com site, and can have multiple sub domains to it. So, for example if I request http://this.is.a.test .nan2d.com then that will server from the //server/vhosts/nan2d.com/this.is.a.test/ directory. All of that is taken care of in the second condition/rule set.

The first condition/rule set dais that if the request does not contain a sub domain, say http://nan2d.com, then that will be served from the default directory //server/vhosts/nan2d.com/www/ , likewise for http://www .nan2d.com.

The cool thing is, if I want to have a sub domain, all I have to do is just create a folder for the nan2d.com vhost and mod_rewrite will automatically tell Apache to server from there upon having the name if that folder as the sub domain for the request.

The settings do not limit this to nan2d.com.

This is the cool part:
What I have here is a fully functional mass virtual hosting solution with unlimited sub domains.

Nervo: I assume you have no idea what that text is :|

Share this post

Link to post
Share on other sites
Original post by CWizard
Have you looked at mod_vhost_alias?

I''m looking at your rules, and see what can be done, although I am not really a mod_rewrite wizard.

I have, actually. I have, in great depth too!

It does not give me the power and control that I want though.

For example, I cant get http://nan2d.com and http://www.nan2d.com to map to the same directory. This is literally impossible in Windows. http://nan2d.com Would map to //…../nan2d.com/_/, creating a symbolic link (Cygwin) did not help, nor did creating a shortcut.

There is now way of having multi-level sub domains either.

Share this post

Link to post
Share on other sites
Hah, finally got the syntax right

If you would do one slight modification in your semantics you could use mod_vhost_alias. This would be to do an external redirect from foo.bar to www.foo.bar, which I think is a good thing to do, so the same site doesn't have several names. I have that convention on all sites I host.

This configuration would do it

<VirtualHost *>
ServerName fuckyou
RewriteEngine On
# If host is only two part, we add www and ask visitor to come again
RewriteCond %{SERVER_NAME} ^([\w-]+\.[\w-]+)$
RewriteRule (.*) http://www.%1$1 [R]
# ...
VirtualDocumentRoot c:/server/vhosts/%-2.0.%-1/%-3+

[edited by - CWizard on November 10, 2003 9:18:07 PM]

Share this post

Link to post
Share on other sites
Original post by aftermath
Original post by CWizard
ServerName fuckyou

Does this mean that it doesnt matter what `ServerName' is?
Actually I'm not sure... If this is your first virtual host (or you're not using virtual hosts) it doesn't matter, as it will be a catch-all anyway. Otherwise, you will need some mechanism so that Apache can determine when to use this virtual host, usually by matching the ServerName or the IP it listens to.

[edited by - CWizard on November 10, 2003 10:15:29 PM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!