CodeIgniter Forums
virtual subdomain with htaccess - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: virtual subdomain with htaccess (/showthread.php?tid=18035)

Pages: 1 2


virtual subdomain with htaccess - El Forum - 04-23-2009

[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?


virtual subdomain with htaccess - El Forum - 04-23-2009

[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.


virtual subdomain with htaccess - El Forum - 04-23-2009

[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.


virtual subdomain with htaccess - El Forum - 04-23-2009

[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



virtual subdomain with htaccess - El Forum - 04-23-2009

[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.


virtual subdomain with htaccess - El Forum - 04-23-2009

[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.


virtual subdomain with htaccess - El Forum - 04-23-2009

[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.


virtual subdomain with htaccess - El Forum - 04-23-2009

[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


virtual subdomain with htaccess - El Forum - 04-26-2009

[eluser]harmstra[/eluser]
Nobody?


virtual subdomain with htaccess - El Forum - 04-26-2009

[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/".