enemetch
enemetch

Reputation: 522

CodeIgniter: Rewrite URL with method parameter as subdomain?

I have a CI Project where I want to change the URL like below:

www.example.com/index.php/Controller/method/param1 to param1.example.com/index.php/Controller/method

Is this possible using just CI? Like routes. Or we need to .htaccess? If possible, are there any risks or is it a bad practice? Also does it affect SEO?

EDIT: This rewrite should happen only for Controller controller.

EDIT: I think the meaning came out wrong. When a user enters a URL like param1.example.com/Controller/method It has to fire the method in Controller and pass param1 as parameter.

Upvotes: 0

Views: 435

Answers (1)

Amit Kumar
Amit Kumar

Reputation: 477

  1. You need to create redirect controller (controllers/Redirect.php), that will redirect browser Something like this:
<?php
  defined('BASEPATH') OR exit('No direct script access allowed');

  class Redirect extends CI_Controller {

      public function index($method,$domain) {
          $redirect_url =  'http://www.' . $domain . '.example.com/index.php/Controller/' . $method;
          redirect($redirect_url,'refresh');
      }

  }
?>
  1. Add this line in application/config/route.php

$route['Controller/(:any)/(:any)'] = 'redirect/index/$1/$2';

You are done. Enjoy :)

Upvotes: 1

Related Questions