Welcome Guest, Not a member yet? Register   Sign In
virtual subdomain with htaccess
#1

[eluser]harmstra[/eluser]
I try to create a virtual subdomain, but no workie workie.

When i put this in htaccess,

Code:
RewriteCond %{HTTP_HOST} ^subdomain[NC]
RewriteRule ^(.*)$ http://www.domain.com/test/$1 [QSA,L]

things work fine. test is a physical subdir, and the contents of /test/index.html show up when i point the browser to subdomain.domain.com.
Important: the addressbar keeps the subdomain.domain.com, while it shows www.domain.com/test/index.html

So far, so good.

Now i want to do the same thing, but redirect to a CI controller

Code:
RewriteCond %{HTTP_HOST} ^subdomain[NC]
RewriteRule ^(.*)$ http://www.domain.com/controllername/$1 [QSA,L]

Then i get a Internal Server Error, because of a redirect loop. So i add 1 line.

Code:
RewriteCond %{HTTP_HOST} ^subdomain[NC]
RewriteCond %{REQUEST_URI} !^/controllername[NC]
RewriteRule ^(.*)$ http://www.domain.com/controllername/$1 [QSA,L]

Then everything works fine, subdomain.domain.com shows the contents of www.domain.com/controllername.

But the annoying thing is, the url in the address bar changes to www.domain.com/controllername

Is there a way to avoid this?
#2

[eluser]TheFuzzy0ne[/eluser]
That's what it's meant to do when you specify a host name as the target. If you want an internal redirect, just specify the directory relative to the htaccess file.
#3

[eluser]harmstra[/eluser]
Unfortunatly, that is not the case, as you can see in the first piece of code.
Here i specify the hostname + path and the visible address is not rewritten.
#4

[eluser]TheFuzzy0ne[/eluser]
OK, I think we might be getting out wires crossed here, so I'll try explaining again.

Code:
RewriteCond %{HTTP_HOST} ^subdomain[NC]
RewriteRule ^(.*)$ http://www.domain.com/test/index.php/$1 [QSA,L] # This will redirect the browser

Code:
RewriteCond %{HTTP_HOST} ^subdomain[NC]
RewriteRule ^(.*)$ test/index.php/$1 [QSA,L] # This won't
#5

[eluser]harmstra[/eluser]
Maybe that's how things are in theory.
This is how tings are on my server in real life

/test is a physical directory
Code:
RewriteCond %{HTTP_HOST} ^subdomain[NC]
RewriteRule ^(.*)$ http://www.onderdelenzoeker.com/test/$1 [QSA,L] #no change in address bar



/test is a NOT a physical dir, but a controller
Code:
RewriteCond %{HTTP_HOST} ^subdomain[NC]
RewriteCond %{REQUEST_URI} !^/index.php/test[NC] # to prevent loop
RewriteRule ^(.*)$ http://www.doamin.com/index.php/test/$1 [QSA,L] #address bar changes

So what's the difference? I think the problem is this

The code used for CI

Code:
RewriteCond $1 !^(index\.php|images|robots\.txt|src|ext)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA]

In the first case, the dir exists, in de second it doesn't
So in the second case, the above RewriteRule will be executed, in the first case it won't.
#6

[eluser]TheFuzzy0ne[/eluser]
In your first example, please substitute [QSA,L] for [R]. Does it redirect the browser then? If it doesn't, them the rule is not matching.
#7

[eluser]harmstra[/eluser]
The URL does not change,
the browser says


Found

The document has moved here.

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
#8

[eluser]harmstra[/eluser]
But when i remove this

Code:
RewriteCond $1 !^(index\.php|images|robots\.txt|src|ext)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA]

it gets redirected to www.domain.com/test
#9

[eluser]harmstra[/eluser]
Nobody?
#10

[eluser]Jelmer[/eluser]
I do it by first redirecting to the directory from the root .htaccess file:
Code:
RewriteCond %{HTTP_HOST}    ^sub.domain.com$
RewriteCond %{REQUEST_URI}  !^/subdomain/
RewriteRule (.*)            /subdomain/$1 [last]

And then put a .htaccess file with the /subdomain/ directory that takes care of the CI rewriting. Like the htaccess in your code above (#7), just remember that the htaccess in your subdomain directory has a rewritebase of "/" and not "/subdomain/".




Theme © iAndrew 2016 - Forum software by © MyBB