开发者

.htaccess redirect "domain.tld/" to "domain.tld/home.html"

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.


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! ;)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜