CodeIgniter: Rewrite URL with method parameter as subdomain? - php

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.

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');
}
}
?>
Add this line in application/config/route.php
$route['Controller/(:any)/(:any)'] = 'redirect/index/$1/$2';
You are done. Enjoy :)

Related

Codeigniter route URL contact page

I'm new in Codeigniter I'm not sure how to use Codeigniter Routing. I've created the Contact.php in the controller folder and contact.php in the views folder.
In routes.php I have put $route['Contact'] = 'controller/contact'; but when I enter the url http://mytest.dev/contact/ it shows 404 Page Not Found. The page you requested was not found.
I want when I enter "http://mytest.dev/contact" it will show the contact page
Thanks in advance.
Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Contact extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('contact')
}
}
In CI there is an index.php in the URL ( by default ). So, you can access your page with this url http://mytest.dev/index.php/contact
For removing it from URL and have it like you want you need to add .htaccess file in your project directory
Check this answer for it
Also, you don't need to change your routes.php every time after creating a new page. Leave it like this
$route['default_controller'] = 'welcome'; // or contact
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I see you are using $route['Contact'] = 'controller/contact';
Tell us your controller's class name and which function/method did you use from the above you are referencing to contact() and that controller name doesn't make sense. You route will normally be in lowercase too.
If you named your class Contact (which seems like it) then you need to put a .htaccess file in the folder where your index.php or base_url resides (or root directory) and then remove the value in application/config.php as $config['index_page'] = ''; so that you can access it from http://mytest.dev/contact
To make it clearer. The format should be $route['AAA'] = 'BBB/CCC';
AAA is the url path of your choice
BBB is the name of your controller's class
CCC is the function/method of the page you want to show
If you didn't add the htaccess, you must put /index.php/ before your preferred path.

How to achieve this kind of url routing in codeigniter

i want to achieve url routing something like www.example.com/alicia
suppose alicia is not a class name or method name just something like passing data in url and with some class i want to access this and want to use it for further process.How i can use it ? Thanks in advance.
You can use Codeigniter's built-in routing, the file route.php is located in your config folder.
there you can add:
$route['alicia'] = 'welcome/index/something';
$route['alicia/:any'] = 'welcome/index/someotherthing/$1';
then in your controller, for example welcome you just create a function like:
public function index($page = null){
if($page=='something'){
// do what you need to do, for example load a view:
$this->load->view('allaboutalicia');
}
elseif ($page=='someotherthing'){
// here you can read in data from url (www.example.com/alicia/2017
$year=$this->uri->segment(2); // you need to load the helper url previously
}else{
// do some other stuff
}
}
documentation on routing and on urlhelper
edit after comment:
in case your uri segment is representing a variable, like a username, then you should use a uri scheme like www.example.com/user/alice and create your route like:
$route['user/:any'] = 'welcome/index/user';
then in your controller welcome
public function index($page=null){
if($page=='user'){
// do what you need to do with that user
$user=$this->uri->segment(2); // you need to load the helper url
}
else{
// exception
}
}
This could be tricky because you don't want to break any existing urls that already work.
If you are using Apache, you can set up a mod_rewrite rule that takes care to exclude each of your controllers that is NOT some name.
Alternatively, you could create a remap method in your base controller.
class Welcome extends CI_Controller
{
public function _remap($method)
{
echo "request for $method being handled by " . __METHOD__;
}
}
You could write logic in that method to examine the requested $method or perhaps look at $_SERVER["REQUEST_URI"] to decide what you want to do. This can be a bit tricky to sort out but is probably a good way to get started.
Another possibility, if you can think of some way to distinguish these urls from your other urls, would be to use the routing functionality of codeigniter and define a pattern matching rule in the routes.php file that points these names to some controller which handles them.
I believe that the default_controller will be a factor in this. Any controller/method situations that actually correspond to a controller::method class should be handled by that controller::method. Any that do not match will, I believe, be assigned to your default_controller:
$route['default_controller'] = 'welcome';

CodeIgniter Parameter in middle of route

I'm currently learning codeigniter but I'm having a bit of an issue with routing. My routes file is as below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['images/(:num)/upload/'] = 'image/upload/$1';
$route['images/(:num)'] = 'image/index/$1';
$route['yoyo/(:num)'] = 'yoyo/view/$1';
$route['default_controller'] = 'yoyo';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
My issue is with the first route; I'm unable to get it to work without moving (:num) to the end of the route. Based on other routes I've seen after browsing stackoverflow it get the impression that this should work. Any ideas?
So just to be clear it works if I add the follow route instead:
$route['images/upload/(:num)/'] = 'image/upload/$1';
$route['images/(:num)/upload/'] = 'image/$1/upload';
Edit: What was I thinking..sorry.
Of course you can't do the code I told you, because the route images/$1/upload means that you will access the controller images ...and the function $1.
Obviously, you can't do that. This is what you should do:
$route['images/upload/(:num)'] = 'image/upload/$1';
public class image extends CI_Controller
{
public function upload($arg0) {}
}
And then if you want to pass more args, you just need to continue the route path.
$route['images/upload/(:num)/(:any)'] = 'image/upload/$1/$2';
public function upload($arg0, $arg1) {}

How to remove controller name from the URL in codeigniter?

I have controller named “profile” and the URL for user profile is
www.example.com/profile/user
I am already using the rerouting of codeigniter in the routes file
$route['profile/(:any)'] = "profile/index";
what I am looking is to remove the profile controller name from the URL so it will be SEO and user friendly.
e.g
www.example.com/user
any suggestions?
You may try any one of these
// url could be yourdomain/imran
$route['(:any)'] = 'profile/index/$1';
// url could be yourdomain/10
$route['(:num)'] = 'profile/index/$1';
// url could be yourdomain/imran10
$route['([a-zA-Z0-9]+)'] = "profile/index/$1";
Your class may look like this
class Profile extends CI_Controller {
public function index($id)
{
// $id is your param
}
}
Update : (Be careful)
Remember that, if you have a class Someclass and you use url like yourdomain/Someclass then this will be routed to profile/index/$1 if you have $route['(:any)'] or $route['([a-zA-Z0-9]+)'].
You can use this in a route file:
$route['(:any)'] = "controller_name/$1";
$route['(:any)/(:any)'] = "controller_name/$1/$1";
$route['(:any)/(:any)/(:any)'] = "controller_name/$1/$1/$1";
Try this:
$route['user'] = 'profile/user';
Dont know any generic way to do it
Possible duplicate of How to hide controller name in the url in CodeIgniter?
If I understand your question correctly, you just want to add a catchall to the very end of your routes file. Like:
$route['(:any)'] = 'profile/index';
This will catch everything that isn't caught by another route, so make sure this script contains logic to determine when a 404 should be presented.

CodeIgniter "default_controller" functions without controller name in URL

Forgive me if this is a "duh" question or if you need more information to answer, I am new to CodeIgniter and still haven't figured out a few things with best practices and such...
In routes.php I have $route['default_controller'] = "home"; so my default_controller is obviously "home".
Inside my home.php controller I have:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct() {
// blah
}
function index(){
// blah
}
function login(){
// blah
}
}
Which works fine and everything, there is no problems with that. The only thing I can't figure out is if I want to access the login function, I currently have to go to www.blah.com/home/login. How can I change it so it just goes to www.blah.com/login, without creating a new controller (I would like to keep some of these one time base urls all in my default controller)? Is that even possible or do I just have to create a new controller?
If I just have to create a new one, is there a best practices for how many controllers you have, etc.
Documentation says: This route will tell the Router what URI segments to use if those provided in the URL cannot be matched to a valid route.
So use $route['login'] = 'home/login';
The way I have it set up is to have the index function act as the gatekeeper: if not logged in, show a login form view, if logged in, redirect to your (protected) home page. The login function is only accessed via post, when the user submits his login/pwd, and performs the login logic.
You need add a line to your application/config/routes.php file
$route['login'] = "home/login";

Categories