I want to create a mod_rewrite RewriteRule which is independent from the location where the web page is installed. I want to define the rewrite rule in a .htaccess file. Let's take this as an example:

RewriteEngine on
RewriteRule ^(.*)\.html html.php

With this rule I want to map all *.html requests to a html.php script which is located in the web root. The problem is, the public base url of the webroot can change. So the web root could be located on http://www.somewhere.tld/ or in some sub directory at http://www.somewhere.tld/foo/bar/.

But using a relative path in a rewrite rule doesn't work. So I have to write one of these:

/html.php (When web is located in root directory of the web)
/foo/bar/html.php (When web is located in foo/bar sub directory)

Alternatively I can set a RewriteBase but I simply don't want to configure this path at all. I want apache to automatically do the right thing so I can just copy the web to some directory and it just works without telling the rewrite rules where the web is located in. How can I do this.?

link|improve this question

feedback

3 Answers

up vote 3 down vote accepted

There is no way as in my knowledge that you can achieve this. You have to configure RewriteBase. One way is to automate setting up of RewriteBase using a PHP script maybe? But that will need write permission (at least) on the .htaccess. But you will have to configure the RewriteBase in .htaccess.

link|improve this answer
1  
I accept this answer because no solution appeared for a long time. So I simply have to live with the fact that it is not possible without setting RewriteBase or using absolute URLs in the rewrite targets. – kayahr Sep 13 '11 at 11:36
feedback

How about something like

RewriteEngine on
RewriteRule ^(.*)\.html $1/html.php

My example preserves the filename part of html file, though -- for example, page1.html would get redirected to page1/html.php. (Note: I did not test this at all, try at your own risk :))

Also, mod_rewrite guide has tons of examples similar to your problem. Did you take a look at that already?

link|improve this answer
This doesn't work. When the .htaccess file is located in the directory which is publicly accessible via the path /foo/bar and I access the file /foo/bar/test.html then $1 only contains /test.html. The path which is outside of the scope of the .htaccess file is stripped away by mod_rewrite. – kayahr Jun 9 '11 at 11:48
@kayahr first of all, these rules don't need to be in a .htaccess file. Secondly, the .htaccess file does not need to live in the directory $path/foo/bar ... it can live in $path/. Each directory path is checked by Apache for .htaccess – Phil Jun 9 '11 at 12:49
@Phil: I know that the rules don't need to be in .htaccess but I WANT them in .htaccess and I WANT it to be inside the directory of the web and not somewhere in a parent dir. The reason is "easy deployment". Simply unzip the web somewhere and it works out of the box. When I have to manually configure a RewriteBase or manually create some .htaccess file elsewhere or manually edit the global apache configuration then this is counter-productive. – kayahr Jun 9 '11 at 13:03
@kayahr, have you seen the way Wordpress isntall? They automatically setup .htaccess but it is generated depending on the Wordpress location from DocumentRoot. This is the only way to achieve this. add a install script that generates .htaccess, write it if the script have write permission, or else show it to use so they can manually add it to .htaccess. – Hameedullah Khan Jun 9 '11 at 15:20
@kayahr Think outside the box, there are other ways to deploy your application but they involve work. Welcome to the wonderful world of devops! :) – Phil Jun 9 '11 at 19:31
feedback

I find Apache's documentation to be misleading:

When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substutions.

But the prefix it adds back is completely different (path on disk instead of original URL). I can't think of a situation where this would be the correct behavior.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.