views:

362

answers:

4

Environment: Apache 2 shipped with Mac OS X Leopard; with no big changes in the default httpd.conf.

I am setting up some rewrites through my .htaccess but I am not able to use target paths relative to the current directory.

# URL: http://localhost/~davis/rewrite/.htaccess
# File: /Users/davis/Sites/rewrite/.htaccess

RewriteRule go.php target.php

# Not working

I am expecting the rule to redirect users to http://localhost/~davis/rewrite/target.php. It's resolving to the correct physical path but gives me a 404 saying that the path /Users/davis/Sites/rewrite/target.php does not exist.

It works all good if I give the full URL.

# URL: http://localhost/~davis/rewrite/.htaccess
# File: /Users/davis/Sites/rewrite/.htaccess

RewriteRule go.php /~davis/rewrite/target.php

# Works

Am I missing something here? Anything to be setup in httpd.conf?

Thanks!

Edit: The relative URLs are working properly on my live Linux server. It's in my local Mac box that I am having problems.

A: 

Try to adjust the base URL:

RewriteBase /~davis/rewrite/
Gumbo
Thanks for the suggestion Gumbo. But my goal is to make the .htaccess independent of the server directory where the app is installed. If we are using RewriteBase, we will still need to set it to the correct directory. On the other hand, if I can give URLs relative to the current directory, that pretty much solves the problem.
Davis John
A: 

You can try RewriteBase, but other than that I don't think there's anything you can do.

RewriteBase /~davis/rewrite/
Prody
The thing is, relative paths are working correctly on my Linux server and also on my local Windows box. I wonder if this has something to do with the way user directories are setup (~davis etc.).
Davis John
A: 

Got it resolved, finally!

It starts working if I set DocumentRoot to my ~/Sites directory or create a VirtualHost for that path. Apparently in the user directory setup, Apache was not able to map the physical path to the ~username alias.

Thanks everyone for your inputs!

Davis John
A: 

Can you explain that solution in more detail?

trnsfrmr
By default, I will be accessing my local sites using the URL http://localhost/~davis/ where davis is my username. But under this setup, I was not able to use relative RewriteRules. What I did was to change the DocumentRoot setting in the Apache httpd.conf to directly point to my sites folder: DocumentRoot "/Users/davis/Sites"And this solved the issue. Now, I am accessing my local sites directly at http://localhost/.You can find the the conf file under /etc/apache2/Hope this explanation helps :)
Davis John