Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Routing - changing to handle a subdomain?

I have my site built in CodeIgniter. The url format is

domain.com/lang/id/descriptive-text 

e.g.

domain.com/en/12/article-on-codeigniter-routing

One particular page (Tours) has all of the tour information (retrieved according to id number) to display. HOWEVER, i now have a booking system written in Ruby on Rails that i need to integrate. The way to do that is going to have to be have a subdomain called booking.domain.com and each tour page to display on that subdomain so that the in page booking system can run properly.

That means coding the Tours page in ruby and passing in the info from codeigniter. The way i know whether a page is a Tour or just other pages is that the Tour pages have id greater than 20 but less than 40.

Below is my current routing code:

$route['default_controller'] = "content";
$route['en/(:num)/(:any)'] = "content/en/$1";
$route['de/(:num)/(:any)'] = "content/de/$1";
$route['es/(:num)/(:any)'] = "content/es/$1";
$route['it/(:num)/(:any)'] = "content/it/$1";

My question is how would i change this to now reflect this new change?? I'm at a loss.

Thanks

like image 877
Tom Chambers Avatar asked Feb 05 '18 11:02

Tom Chambers


3 Answers

Solution (without modifying the CodeIgniter)

Use .htaccess to do the redirection.

<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteRule    ^([^/]+)/([20][0-9]|[30][0-9]|[40]+)/([^/]+)?$    http://sub.maindomain.foo/$1/$2/$3    [L]
</ifModule>

This will redirect to the sub domain if the URL pattern matches the rule

id greater than 20 but less than 40

If your main domain and sub domain have same URL pattern,

http://maindomain.foo/en/30/article-on-codeigniter-routing

will automatically redirects to

http://sub.maindomain.foo/en/30/article-on-codeigniter-routing

like image 148
Praneeth Nidarshan Avatar answered Sep 23 '22 13:09

Praneeth Nidarshan


if you don't have same range of id like the subdomain, you can try this

$route['default_controller'] = "content";

$route['(.+)'] = function ( $params ){
                        $param = explode("/", $params);
                        if(20 >= (int) $param[1] =< 40){
                            return "subdomain";
                        }
                        else{
                            return "content/" . $param[0] . "/" . $param[1];
                        }
                    };
like image 23
elddenmedio Avatar answered Sep 20 '22 13:09

elddenmedio


I might be being stupid here - but isn't the solution to amend your controller and do the following?

public function content($lang,$id){
    if ($id > 20 && $id < 40){
        $post = base64_encode(serialize($post));
        redirect('subdomain.domain.com/'.$lang.'/'.$id.'/'.$post);
    } else {
        // carry on processing ....
    }
}

I have amended my answer based on your query about posting data. I've serialized and base64 encoded it. To unserialize it you will need https://github.com/jqr/php-serialize (I am not a ruby expert so know nothing about this) and from research I believe you would need Base64.decode64() for the base64 decoding.

like image 38
Antony Avatar answered Sep 20 '22 13:09

Antony