views:

43

answers:

2

Hello everybody.

I have a website here on my localhost :

http://localhost/mysite/www/index.php

I have some RewriteRules to redirect like this :

http://localhost/mysite/www/index.php?page=home
->  http://localhost/mysite/www/home.html

And now, I want to do a redirection like this :

http://localhost/mysite/www/
->  http://localhost/mysite/www/home.html

I have an environment variable named REWRITE_BASE containing /mysite/www/. So what I thought to do was to compare {REQUEST_URI} to %{ENV:REWRITE_BASE} ... like this:

RewriteCond {REQUEST_URI} =%{ENV:REWRITE_BASE}

RewriteRule . %{ENV:REWRITE_BASE}home\.html [R=301,L]

But it don't works well.

To help you understand what I want to do, here is the working code in PHP to do what I want:

$rewriteBase = getenv('REWRITE_BASE');

if ($rewriteBase === $_SERVER['REQUEST_URI'])
    header('Location: '.$rewriteBase.'home.html');

Thanks for help.

A: 

What you want to do won't work, because mod_rewrite doesn't expand any variables present in its test patterns. Therefore, when you do something like this...

RewriteCond %{REQUEST_URI} =%{ENV:REWRITE_BASE}

...What you're actually doing is comparing the value of %{REQUEST_URI} to the string "%{ENV:REWRITE_BASE}", instead of the value "/mysite/www/" like you wanted. Is there a reason that you can't specify the value directly in your RewriteCond or simply move the rules so that they're relative to the /mysite/www/ directory to begin with?

There might be another way to approach the problem, but unfortunately (and for some good reasons) you cannot perform that kind of comparison using mod_rewrite.

Tim Stone
+1  A: 

Okay... so, as I can't use a variable for my comparison, here is the way that I made it works :

# Redirect domain.tld/ to domain.tld/home.html
RewriteCond %{REQUEST_URI} =/www/ [OR]
RewriteCond %{REQUEST_URI} =/mysite/www/
RewriteRule ^(.*)$ %{REQUEST_URI}home\.html [R=301,L]

# RewriteBase equivalent - Production environment
RewriteRule . - [E=REWRITE_BASE:/www/]

# RewriteBase equivalent - Development environment
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITE_BASE:/mysite/www/,E=DEVELOPMENT_ENV:1]

# Website rewritings
RewriteRule ^([^/]*)(?:/([^/]*))?\.html$ %{ENV:REWRITE_BASE}index\.php?page=$1&view=$2 [QSA,L]

Now it's alright. Thanks for your answers! ;)

gnutix
Erf, I have another problem with this method. I can't have an index.html like a "website in construction".I tried to add a "RewriteCond %{REQUEST_URI}index.html !-f" before the first RewriteRule... didn't worked.Any idea ?
gnutix