Sunday, March 6, 2011

.htaccess Rewrite Rules for subdomain

I use codeigniter as my main install on the main domain. I have created a subdomain and a folder called live e.g. live.domain.com maps to public/live . However in public I use codeigniter.

I now have the dynamic codeigniter url:

http://domain.com/api/

which I want to map to my subdomain:

https://live.domain.com

So going to:

https://live.domain.com/api/functioname

would be using the script:

http://domain.com/api/apifunctioname

and possibly:

http://domain.com/api/apifunctioname/parameter1/parameter

Everything is on the same server so no redirects are needed.

Anyone have any ideas on which rewrite rules to use?

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^live\.domain\.com [NC]
RewriteRule (.+)$ "http://domain.com/api/$1" [L]

The above works great as a rewrite but redirects to http://domain.com/api/functionname instead I want it to route; so that when going to:

https://live.domain.com/api/functioname

It stays at that url but uses the script of

http://domain.com/api/functionname

Thank you very much,

Ice

From stackoverflow
  • How about something like the following:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^live\.domain\.com$ [NC]
    RewriteRule (.+)$ "https://domain.com/api/$1" [L,P]
    
    Simon Jensen : I should probably mention that it is untested. Have you tried incresing the mod_rewrite loglevel, and analyzed what exactly is going on?
    Tomalak : You should have "^live\.domain\.com" as the RewriteCond, and there should be a second RewriteCond checking for /api/ URLs.
    Simon Jensen : @Tomalak: Agreed, he should definitely do that. @Ice: You can specify the log file with the RewriteLog-directive.
    Simon Jensen : Could you possibly post what you've ended up with so far?
    Simon Jensen : Instead of [L] as flags to RewriteRule, try [L,P], iirc, that should perform a proxy request internally.
    Simon Jensen : Do you have mod_proxy enabled?

0 comments:

Post a Comment