Reputation: 5824
I'm using Apache 2.4 with mod_rewrite
, and I have a problem I can't solve.
I have a .htaccess
file containing
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
... RewriteRules ...
</IfModule>
This works nicely. Now I need to do the following: If somebody accesses my website as http://my.web.page.com, I need RewriteBase /
as in the example above. But if somebody accesses the website as http://192.168.1.10/xyz, I need RewriteBase /xyz
instead.
I guess I can use <If expression>...</If>
to achieve this, but I've not been able to write the expression correctly.
The xyz
is a fixed string. It doesn't have to be copied from the URL, but can be hard-coded in the RewriteBase /xyz
command.
How can I do this?
EDIT
@anubhava suggested an expression, which I couldn't get to work. So I tried some very simple <If...>
statements that simply use the RewriteBase /
statement.
I am now very, very confused.
Attempt 1
RewriteEngine On
RewriteBase /
<If "false">
RewriteBase /
</If>
This works. So far so good. Now let's enable the conditional:
Attempt 2
RewriteEngine On
RewriteBase /
<If "true">
RewriteBase /
</If>
This does not work. So enabling the conditional makes a difference. Perhaps the two RewriteBase
statements cause the code to fail.
Attempt 3
RewriteEngine On
<If "true">
RewriteBase /
</If>
Nope, this still doesn't work. Perhaps it is the mere presence of the conditional that is a problem?
Attempt 4
RewriteEngine On
RewriteBase /
<If "true">
#RewriteBase /
</If>
This works. So the conditional itself is harmless. I just can't write RewriteBase
inside it.
For completeness sake, the rewrite rules I'm using are:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
The Apache error log contains no error information.
SECOND EDIT
Based on the suggestions from @anubhava, I managed to get this to work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# case 1
RewriteCond %{HTTP_HOST} =my.web.page.com
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
# case 2
RewriteCond %{HTTP_HOST} ^192\.168\.
RewriteRule ^(.*)$ xyz/index.php?/$1 [L,QSA]
Upvotes: 5
Views: 3212
Reputation: 785146
Based on your edited question these rules should work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# case 1
RewriteCond %{HTTP_HOST} =my.web.page.com
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
# case 2
RewriteCond %{HTTP_HOST} ^192\.168\.
RewriteRule ^(.*)$ xyz/index.php?/$1 [L,QSA]
Upvotes: 5