Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Basically I want users can access the page via intranet and internet.

If users access the page via intranet, they can typing intranet IP of server on their browser address bar to 192.168.x.x.

But when users access the page via internet, they can typing public IP of server, I will rewrite the URL to public IP of server.

I have tried this, but I get the page not properly redirected.

RewriteEngine   On
RewriteBase     /mypath/
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_URI}      !(.*)/$
RewriteCond     %{REQUEST_METHOD}   GET
RewriteCond     %{HTTP_HOST}        !192.168.0.1
RewriteRule     ^(.*)$          http://<public.ip.of.server>/mypath/$1/ [L,R=301]
RewriteCond     %{HTTP_HOST}        !<public.ip.of.server>
RewriteRule     ^(.*)$          http://192.168.0.1/mypath/$1/   [L,R=301]

I have tried this too, but I get the page 500 internal server error.

<If "%{HTTP_HOST} == '192.168.0.1'">
RewriteEngine   On
RewriteBase     /mypath/
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_URI}      !(.*)/$
RewriteCond     %{REQUEST_METHOD}   GET
RewriteRule     ^(.*)$          http://192.168.0.1/mypath/$1/   [L,R=301]
</If>
<If "%{HTTP_HOST} == 'public.ip.of.server'">
RewriteEngine   On
RewriteBase     /mypath/
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_URI}      !(.*)/$
RewriteCond     %{REQUEST_METHOD}   GET
RewriteRule     ^(.*)$          http://<public.ip.of.server>/mypath/$1/ [L,R=301]
</If>

Am I doing something wrong?

share|improve this question

migrated from stackoverflow.com Oct 15 '12 at 11:19

This question came from our site for professional and enthusiast programmers.

    
There's a typo in the second block: you have HTPP twice instead of HTTP –  Tom Smilack Oct 15 '12 at 3:26
    
What version of apache are you using? –  Jon Lin Oct 15 '12 at 3:31
    
@TomSmilack I'm sorry for typo, actually I have the right HTTP_HOST on my htaccess. I'll fix my code here. –  AMYunus Oct 15 '12 at 3:44
    
@JonLin I'm using Apache 2.2.3 –  AMYunus Oct 15 '12 at 3:45
    
Your ancient version of Apache has known security issues... Hope the script kiddies don't find you. –  Chris S Oct 17 '12 at 13:55

1 Answer 1

up vote 0 down vote accepted

I'm using Apache 2.2.3

The <If> Directive is part of Apache 2.4's core. Those directives will cause a 500 server error if you put them in 2.2. It's not clear at all what you're trying to do, but you can replace the <If> blocks with a simple RewriteCond.

The important concept here is that RewriteConds only apply to the immediately following RewriteRule directive, they won't apply to any other ones, only just that single one. So you need to duplicate conditions if you need them to be applied to multiple rules.

RewriteEngine   On
RewriteBase     /mypath/

RewriteCond     %{HTTP_HOST} ^192.168.0.1$
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_URI}      !(.*)/$
RewriteCond     %{REQUEST_METHOD}   GET
RewriteRule     ^(.*)$          http://192.168.0.1/mypath/$1/   [L,R=301]

RewriteCond     %{HTTP_HOST} ^public.ip.of.server$
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_URI}      !(.*)/$
RewriteCond     %{REQUEST_METHOD}   GET
RewriteRule     ^(.*)$          http://<public.ip.of.server>/mypath/$1/ [L,R=301]

But you can just get rid of all of that because you can redirect without using the http://hostname bit:

RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_URI}      !(.*)/$
RewriteCond     %{REQUEST_METHOD}   GET
RewriteRule     ^(.*)$  /mypath/$1/   [L,R=301]

And that would accomplish the above for any host.

share|improve this answer
    
Shame on me. I saw wrong documentation :lol: Thanks for correcting it. I've tried both, first code works. But second one does not work. I do not know why. Thank you very much. –  AMYunus Oct 15 '12 at 7:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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