Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: URL Rewriting with Apache 2.2 |
|
Author |
|
dhall
Joined: 17 Apr 2006 Posts: 1
|
Posted: Fri 21 Apr '06 16:41 Post subject: URL Rewriting with Apache 2.2 |
|
|
This *should* be an easy question, but for some reason I can not get this working.
Here's what I'm attempting to do:
In order to preserve a standard web site organizational structure and promote sharing of resources, I'm doing URL rewriting. I'm attempting two rewrites on the incoming request.
The first rewrite adds a trailing slash to the url when requesting any directory in the site (or anything that is not a document). I want to determine what is a directory and what is a document based on the fact that a document has an extension (i.e. .html, or .jpg, or .pdf). So, if the user requests 'http://www.domain.com/dir' Apache picks that up and does a rewrite to 'http://www.domain.com/dir/'. Similarly, a request for 'http://www.domain.com' would be rewritten to 'http://www.domain.com/'. (This is basically to force good form in http requests.)
The second rewrite will append '/home/index.jsp' to any request for the root of the site - i.e. a request for 'http://www.domain.com/' will rewrite to 'http://www.domain.com/home/index.jsp'.
I have mod_rewrite loading in Apache 2.2 - no problems there. One instance of Apache server is being used to host multiple virtual hosts, and the above behavior will apply to sites hosted on the server.
Now I'm not sure where to put the rewrite code exactly, so for the sake of discovery, I attempted to get this working in just one virtual host first. Here is the code from my vhosts.conf file. It does not work at all. (No url rewriting to /home/index.jsp takes place - at least not that I can see in my browser locator bar.)
<VirtualHost www.domain.org>
ServerAdmin webmaster@domain.com
DocumentRoot /deploy/var/www/htdocs/domain
ServerName www.domain.org
ServerPath /domain
ErrorLog logs/domain/www.domain.org-error_log
CustomLog logs/domain/www.domain.org-access_log common
# list of redirects
RewriteEngine on
RewriteRule \.org$ \.org/ [R]
RewriteRule \.org/$ \.org/home/index.jsp [R]
</VirtualHost>
This example is incomplete because the above behavior will need to work for .org, .com, and .net domains. I read that regular expressions would be the preferred way to handle what I was looking to do, so thats what I attempted. Also, since the behavior applies to all hosted sites, does the code need to be repeated per each virtual host or can it be moved to another location where it will apply to all hosted sites?
Can someone help me get this url rewriting to work properly - and also explain to me where in my conf files the code should appear? Also, should the rewrite appear in the browser locator bar? and if not, is there a way to make that happen so people can bookmark properly?
Thanks in advance!
Darren |
|
Back to top |
|
emjay
Joined: 24 Jan 2006 Posts: 6 Location: Berlin, Germany
|
Posted: Thu 04 May '06 21:53 Post subject: |
|
|
Hi Darren!
first you need to know that its is very important for tis to work, that you have mod_rewrite in the right place in httpd.conf.
Then it is more easy to test the hostname like this
Code: | RewriteCond %{HTTP_HOST} foo\.com$ |
I did this for many vhosts incuding subdomains on my production environment... it works like this:
RewriteCond %{HTTP_HOST} ^([^.]+)\.ath\.cx$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.ath\.cx /srv/vhosts/80/hosting/web-$1/_%{REQUEST_URI}
in my case i have some dyndns domains and every user has its own dyndns domain... the dir of each user is prefixed with "web-"...
hope that helps for time. if you want more help, you can contact me @ http://schneggerich.net. its the better way, cause im not here sooo often.
Regards Malte |
|
Back to top |
|
|
|
|
|
|