Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite: subdomain to controller

So I have mydomain.tld, www.mydomain.tld and res.mydomain.tld all pointing to the same directory: /var/www/mydomain. In that directory, there's my codeigniter application.

So what I'm trying to do is to forward all requests made through res.mydomain.tld to a specific controller called resources.

What I have:

RewriteCond %{HTTP_HOST} ^res\.mydomain\.tld$
RewriteRule ^(.*)$ /index.php?/resources/$1 [L]

This produces a server error, my rewrite log doesn't provide any clues about why; it just shows some very weird logic being applied to the request string.

Any idea of why this isn't working?

like image 426
thwd Avatar asked Mar 12 '26 16:03

thwd


2 Answers

Leave your .htaccess was before.

In your routes.php

if($_SERVER["SERVER_NAME"]=="res.mydomain.tld"){
    $route['default_controller'] = "resources";
}else{
    //$route['default_controller'] = Your default controller...
}
like image 58
Alfonso Rubalcava Avatar answered Mar 15 '26 04:03

Alfonso Rubalcava


You created a infinite loop. It keeps on rewriting, because the rules always match, and match again. Just add a rule like the following above your rule

RewriteRule ^index.php - [L]

This will prevent any remaining rules underneath it from executing if the (already rewritten) url starts with index.php

like image 38
Gerben Avatar answered Mar 15 '26 05:03

Gerben



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!