CodeIgniter Routing - change to handle subdomain?

I have a site created in CodeIgniter. URL format

domain.com/lang/id/descriptive-text 

eg.

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

On one specific page (Tours) displays all the travel information (obtained by id) for display. HOWEVER, now I have a reservation system written in Ruby on Rails that I need to integrate. To do this, you need to have a subdomain called booking.domain.com, and each page of the tour to display on this subdomain so that the page reservation system can work properly.

This means encoding the Tour page in ruby โ€‹โ€‹and transmitting information from codeigniter. The way I know if a Tour page or just other pages is that the tour pages have an identifier 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 can I change this to reflect this new change now? I'm at a loss.

thanks

+5
source share
3 answers

Solution (without changing 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 result in a redirect to the secondary domain if the URL pattern matches the rule

id greater than 20 but less than 40

If your primary domain and secondary domain have the same URL pattern,

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

automatically redirected to

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

+3
source

if you do not have the same range of identifiers, such as a 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]; } }; 
+2
source

I might be stupid here - but not a decision to change 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 changed my answer based on your request for data publication. I serialized and base64 encoded it. To non-esterize this, you will need https://github.com/jqr/php-serialize (I'm not an expert on ruby, so I donโ€™t know anything about it), and from the studies that I believe you will need Base64.decode64() for base64 decoding.

+1
source

Source: https://habr.com/ru/post/1275171/


All Articles