CI controller url permalink - php

i've got a project in codeigniter and i want to modify its url link.
The current link is: http://fortin.agency/audit-seo/frtcrwl/647/bikearena.ro?/health_check/report/647/bikearena.ro
And i want to remove "health_check/raport/" while the page is still working.
So the new url must look like: http://fortin.agency/audit-seo/frtcrwl/647/bikearena.ro
I used some htaccess code for redirect and rewrite url but it doesnt work. So the current htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
Please, dont bother with htaccess, its only a rule to remove index.php and any other rules wont work.
I just need to know from controllers how to do that.
Is it possible?
EDIT for PACIO
this is located in health_check.php
class health_check extends Home
{
public function __construct()
{
parent::__construct();
}
public function index($base_site="")
{
$this->raport();
}
public function raport($id=0,$domain="")
{

Make use of routes for this. Something like this:
$route['(:num)'] = 'health_check/raport/$1';

You can define routes for controller url permalink

Related

Duplicate content issue due to Codeigniter controllers

I am assisting with a Codeigniter website and don't have a background with it. It has been noted that there is duplicate page content. When I dug in, they are not actual pages, but controllers pointing to the same views. I attempted to set up 301 redirects in an .htaccess file in the root of the website, but it is not taking. In the application/config/routes.php, the following is noted:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
For example, there is a page http://mywebsite.com/weddings, and then http://mywebsite.com/Home/weddings was another controller that pointed to the same view. I attempted to add a 301 redirect from Home/weddings after removing the additional controller pointing to it, but now Home/weddings just redirects to the home page, as it is set up with the 404 override. I hope I have provided enough information for some assistance.
.htaccess file:
RewriteEngine On
RewriteBase /
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# allow access to certain directories in webroot
RewriteCond $1
!^(index\.php|robots\.txt|css/|lib/|js/|images/|^(.*)/images)
# gets rid of index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# page redirects
RedirectMatch 301 ^Home/weddings/$ /weddings
Home.php is as follows:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Admin_model');
$this->load->model('Home_model');
$this->load->helper('captcha');
$this->load->library('recaptcha');
}
public function weddings()
{
$this->load->view('Template/Home/front_header');
$this->load->view('Home/weddings');
$this->load->view('Template/Home/front_footer');
}
Do not touch .htaccess. You have two options.
Create a controller Weddings.php and in your index.php return a
view for weddings page.
The other option is to use Home.php controller and then add weddings method, the method should return the weddings page view. Your route should look like below:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
$route['/weddings'] = 'home/weddings';
Hope this will help. Thank you
Edit
In your Weddings.php controller constructor check if there exist a "Home" and return 404 error. See below
function __construct()
{
parent::Controller();
if($this->uri->uri_segment(1) == 'Home') {
show_404();
}
}

Codeigniter routing throws 404 page not found error

When I am using url as
mysite.com/index.php/user/user the method is getting called.
But when I add below line in routes.php so that I can access the function using custom url.
it is throwing me error. 404 Not Found
I want to access the method using url as mysite.com/user
$route['user']['get'] = 'user/user';
user.php is present inside controller directory
<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';
class User extends REST_Controller {
function __construct()
{
// Construct the parent class
parent::__construct();
}
public function user_get(){
$this->set_response("request recieved", REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
}
[2nd question]
how to make a route to access the controller present inside certain folders in controller directory.
suppose a controller file is present in controllers/api/v1/ directory with file name user.php
Note:
I have tried all the solutions given by users on other posts but issue was not resolved.
EDIT: Issue Resloved
Routing is working just fine now. I think the problem was, I was calling mysite.com/user , instead I should have called mysite.com/index.php/user
Second issue of index.php being in the url is also resolved.
I was making the changes in .htaccess file which was present in Application folder instead.
I created a .htaccess file in root folder then added below mentioned line of code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
You have set your route as
$route['user']['get'] = 'user/user';
Which is directing to the user controller and the user method. But in your controller you have a method called user_get.
So the simplest fix here is to change your route to point to the correct method.
NOTE: You have no method called 'user' so why is that in the route?
So this...
$route['user']['get'] = 'user/user'; // the user method does not exist
Would become...
$route['user']['get'] = 'user/user_get'; // the user_get method does exist
Update: To remove index.php from your URL, your .htaccess might be
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And make sure you add in your base_url in applications/config/config.php
$config['base_url'] = 'http://example.com'; // Change to your sites URL
$config['index_page'] = ''; // remove index.php

PHP routers, difference between "tutorial routers" and the big one from frameworks

it's my first post, so tell me if i've done something wrong.
i'm trying to understand logic of big routers on github and frameworks.
I understand to routers that works like this:
folder structure:
*controllers/Home.php
*index.php
index.php file, orientation code:
$uri = www.xyz.com/home/default/param;
# get parts from url
$parts = explode("/", $uri);
# call controller
$controller_class = "controller_namespace\\".$parts[0];
$controller = new $controller_class;
call_user_func_array(array($controller, $parts[1]), $parts[2]);
.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
so on page www.xyz.com/home/default/param is called controller class home and func. default with params, in filestructure you are still on index.php file, only different controller classes is being called by url.
So, routers that i want understand works like:
class Home{
Router::get("/default", function(){
echo 'Hello';
});
Router::Dispatch();
}
From code that i found on github here etc. i think, the Router loads all the functions from controller and execute that one which match the url ...huh?
Q:
How i can execute the controller class above, when im still on index.php?

How to remove controller name from url making it clean in codeigniter

I have the following url..
http://localhost/ci/site_controller/home
I want to remove site_controller controller from url resulting in..
http://localhost/ci/home
How can I do this in CodeIgniter ?
Note: If you'll ask what I've tried than I've just done searching over Google as I don't know how to use mod_rewrite.
EDIT
I have this in my routes.php
$route['default_controller'] = "site_controller/home";
$route['ci/home'] = 'ci/site_controller/home';
$route['404_override'] = '';
but still not working!
.htaccess
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
ErrorDocument 404 /index.php
You can set your a route for each url:
In your config/routes.php file, just set each page like this:
$route['ci/home'] = "ci/site_controller/home";
This might help you to define a Default Controller for your CodeIgniter project.
https://codeigniter.com/user_guide/general/controllers.html#defining-a-default-controller
For instance, in your case, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'site_controller';
assuming you currently have your url http://localhost/ci/site_controller/home you can just write an explicit route like below to your /application/config/routes.php
$route['ci/home'] = 'site_controller/home';
or
$route['ci/home'] = 'ci/site_controller/home';
if your controller is namespaced to /application/controllers/ci/
This helped for me. In "routes.php" I added this:
$route['(:any)'] = "default_controller/$1";
try using the routes file to re-map url's
http://ellislab.com/codeigniter/user_guide/general/routing.html
I just found the solution in this link, it works for me: http://ellislab.com/forums/viewthread/148531/
$route['^(page1|page2|page3|page4)(/:any)?$'] = "YOURCONTROLLERNAME/$0";
Hope it helps you :)
In the case that your .htaccess file is set up like this (or similar), so index.php is already removed...
RewriteEngine On
RewriteCond $1 !^(index\.php|application|assets|images|js|css|uploads|favicon.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And your codeigniter code lies directly in the html folder...
To remove the controller name from URL, edit application/config/routes.php
Add lines like these:
$route['signin'] = 'web/signin';
The line above calls function signin() of controller Web when user requests yoursebsite.com/signin
To pass variables into said function, do the following:
$route['p/(:any)'] = 'web/p/$1';
This line calls function p() in controller Web, which has a parameter. Function p() was defined as:
public function p($param = "none") {
echo $param;
}
I hope this helps :)
It's so simple if you want to remove the controller name from URL in the Codeigniter 3.x
in my case URL was: http://localhost:8080/index.php/page/sometext
Where "Page" is the controller name
open application/config/routs.php
$urlParam = $this->uri->segment_array()[1];
$rout[$urlParam] = "page/index";
Result: http://localhost:8080/index.php/sometext
I assure you, it will work. tested 100%.
Drop the 'ci' from your routes. That is your project name and is never required. Routes always start on the controller level:
$route['home'] = "site_controller/home";
Will work. However, more importantly.. are you sure your design is correct? Why not just let home be a controller? Create a /application/controllers/home.php and you'll be able to access it's index() function with http://localhost/ci/home.
You're complicating things. No need for routes at all.

Using question mark in CodeIgniter routing

I need to change
http://mysite.com/profile?username=nick
to
http://mysite.com/user/nick
with CodeIgniter routing. I add the following line to routes.php but it doesn't work:
$route['user/(:any)'] = "profile?username=$1";
Here is the .htaccess file that I use:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
AddDefaultCharset utf-8
How can I solve this problem? Thanks in advance.
EDIT:
I mean URL structure changing. So after the routing it must redirect
http://mysite.com/user/nick
to
http://mysite.com/profile?username=nick
Htaccess rule to handle the redirect:
RewriteRule user/([^/?]+) /profile?username=$1 [L,R=301]
Route.php change:
<?php
$route['profile'] = 'profile/index';
Profile controller:
<?php
class Profile extends CI_Controller {
public function index()
{
$username = $this->input->get('username');
// do lookup based on username
}
}
HOWEVER: this sort of redirection only makes sense if you have a lot of cached links that don't make sense to change. It sounds from your question that you might be confusing the concepts of routing and redirecting.
EDIT: To "route" (rather than "redirect"), here are the steps:
Htaccess rule to internally re-route requests:
RewriteRule /profile?username=([^&]*) index.php/user/$1 [L]
Route.php:
<?php
$route['user/(:any)'] = 'user/index/$1';
Controller:
<?php
class User extends CI_Controller {
public function index($username)
{
// ...
}
}
If that doesn't work, then, well, you're doing a terrible job explaining your problem :).
The issue most likely is that the ? is a special character in regular expression language meaning either 1 or 0. You will need to escape it for it to match, something like so should solve your issue:
$route['user/(:any)'] = "profile\?username=$1";
Okay, you want to do it the other way around. Try:
RewriteRule user/([^/?]+) index.php/profile?username=$1 [L]
Sorry if this doesn't work, my Apache isn't cooperating.

Categories