CodeIgniter Forums
url_rewrite to subdomain - 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: url_rewrite to subdomain (/showthread.php?tid=8759)



url_rewrite to subdomain - El Forum - 05-30-2008

[eluser]danfreak[/eluser]
Hey guys,

I must rewrite with header [R=301] (namely redirect permanently) all my uri to say for exmple

Code:
http://www.mysite.com/example/any-file.php

to

Code:
http://example.mysite.com/any-file.php

Anyone has any suggestion about an .htaccess trick?

Dan


url_rewrite to subdomain - El Forum - 05-30-2008

[eluser]Michael Wales[/eluser]
Quote:RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^folder/(.*) http://folder.domain.com/$1 [R=301,L]

Not sure how to go about dynamically matching the condition but hopefully it gets you pointed in the right direction. This is just a google search result. Smile

http://www.webmasterworld.com/forum92/1763.htm


url_rewrite to subdomain - El Forum - 05-30-2008

[eluser]danfreak[/eluser]
cheers I'll try that out and I'll let you know!

I also found this great tutorial: .htaccess tips & tricks


url_rewrite to subdomain - El Forum - 05-30-2008

[eluser]Michael Wales[/eluser]
Thanks for the link - htaccess and regex is definitely something I need to work on.

Am I the only one that has to get some insane Googlefu and test 100 times before I get the right combination to work for me?


url_rewrite to subdomain - El Forum - 05-30-2008

[eluser]Michael Wales[/eluser]
Speaking of which - here's another link that has some great htaccess tips/tricks (plus many other things). I keep it in my Feed Reader:

http://www.askapache.com/


url_rewrite to subdomain - El Forum - 05-30-2008

[eluser]danfreak[/eluser]
Hey Michhael,

your rule works great but...

In the root directory I have the following .htaccess

Code:
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
</IfModule>

Namely the
Code:
RewriteCond %{REQUEST_FILENAME} !-d

tells "if a directory exists, don't do any rewrite"

I would like to tell:
Code:
if the directory exists but it is "example" (i.e. http://www.mysite.com/example/(anyUri)) redirect to http://example.mysite.com/(any-uri)

else (namely the directory don't exists) rewrite to index.php

Smile


url_rewrite to subdomain - El Forum - 05-30-2008

[eluser]Michael Wales[/eluser]
I believe mod_rewrite works from top to bottom, so just place the ruleset in the following order:

Quote:<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^folder/(.*) http://folder.domain.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
</IfModule>