I'm updating a website and have a bunch of old URLs that need to be rerouted to their new equivalents. I want to do this server-side (rather than with .htaccess)
It would be neat if in my config/routes.php I could just declare something like this:
$route['old_url_1'] = redirect('/new/url/1', 'location', 301);
$route['old_url_2'] = redirect('/new/url/2', 'location', 301);
$route['old_url_3'] = redirect('/new/url/3', 'location', 301);
Obviously it doesn't work, but is anything like this possible, ie. keep this code in my routes file (more logical) or do I need to go and set up functions in some controller?
Thanks.
Yes, You need to create something(method at controller), that will redirect browser with 301-code. Something like this:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Redirectme extends MY_Controller {
public function index($url=false) {
if($url===false)
redirect('/');
redirect('/new/url/'.$url,'location',301);
}
}
?>
and add to route.php
$route['old_url_(:any)'] = "redirectme/index/$1";
Anyway, best practice to redirect via .htaccess (because it will not call even php-module from webserver).
Related
I have used several codes for restricting direct access of controller page from url, but its not happening.
The below code in controller page is not preventing from direct url access. Is there any proper way to prevent from direct access from url?
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Cont extends CI_Controller
{
public function __construct()
{
parent ::__construct();
$this->load->model('test');
}
public function handle($function){
if($function=='abcd'){
$this->load->view('testpage');
}
}
}
You can use HTTP_REFERER which holds the information(address) about the page that referred you to the current page, if it's empty you can redirect it to your 404 page. Also, you should always check for $_SESSION and redirect if not set.
if( !isset($_SERVER['HTTP_REFERER'])) {
$this->load->helper('url');
redirect('/page404');
}
Alternatively, you can also use HTTP_X_FORWARDED_FOR, but it won't help you in AJAX request. Read more about it here and here.
You can use _remap() function inside your Controller, where we can define where to route the method.
The following code blocks all method call to that controller.
function _remap($method)
{
return false;
}
See the DOC here
From documentation
For the best security, both the system and any application folders should be placed above web root so that they are not directly accessible via a browser. By default, .htaccess files are included in each folder to help prevent direct access, but it is best to remove them from public access entirely in case the web server configuration changes or doesn’t abide by the .htaccess.
If you want to prevent direct access to specific methods then you can try
public function _handle(){ //adding _ before the method name
}
Or
private function handle(){ //private functions cannot access by url
}
I am working with CodeIgniter (V:2.2.6) and I have a simple class User with some basic methods like create, update, edit, delete and index. For the index function, I am using an argument $user (which is the second URI segment) in order to display some information regarding that user. So the default URL looks like:
/user/index/john
to display some information about the user 'john'.
Now, I want to remove the term 'index' from the URL, so that it looks like:
/user/john
For that purpose I have added the following rule in routes.php.
$route['user/(:any)'] = "user/index/$1";
It serves the purpose, but it prevents accessing other functions like /user/create and goes inside /user/index automatically. To solve this problem, I can not see any other way except manually adding routing rules like
$route['user/create'] = "user/create";
for each method of the User class, which is not cool at all. So, please can anyone suggest me a better way of routing under the current circumstances?
Here are my codes:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index($user = '') {
echo "index!";
}
public function create() {
echo 'create!';
}
}
Note: I have gone through the CodeIgniter documentation for URI Routing and another similar question here, but could not figure out a promising solution. And please don't suggest for CodeIgniter version update.
I'm not sure, if this is the answer you like, but i think it should work.
Just put this function in your user controller.
public function _remap($method)
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->index($method);
}
}
Hi im using codeigniter framework because its really cool when it comes to MVC thing. I have a question regarding in the url thing. Usually when saving a controller, lets say in the about us page it has to do something like this About_us extends CI_Controller. When that comes to the url thing it goes like this test.com/about_us. I want that my url is not underscore. I want to be dashed something like this test.com/about-us. How will i able to use the dash instead of using underscore???
any help is greatly appreciated
thanks!
Code Ignitor 3 has this built-in option. In your routes.php:
$route['translate_uri_dashes'] = FALSE;
Just change this to "TRUE" and you can use either _ or - in URL
Here is a detailed article how to fix it.
You will just have to create routes for your pages, as far as I am aware, there is no config directive to change the separating character, or replace '-' with '_' in CI_Router, (probably wouldn't be too hard to add this).
To allow for '-' instead of '_':
Create a file in application/core/MY_Router.php
Change 'MY_' to whatever value is specified in your config directive: $config['subclass_prefix']
insert code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function _set_request($segments = array()) {
if (isset($segments[0]))
$segments[0] = str_replace('-','_',$segments[0]);
if (isset($segments[1])) {
$segments[1] = str_replace('-','_',$segments[1]);
}
return parent::_set_request($segments);
}
}
I've created a "validation helper" that was supposed to have all my custom validation rules. The problem is that when I use them in my form validation, they seem to be ignored. If I move the functions in the controller that is doing the form validation, everything works like a charm. My validation helper is autoloaded.
Is there any reason why I can't seem to use these validation functions if I put them in a helper? Thanks.
A function in a helper and a controller are different obviously.
Create an extended MY_Form_validation.php in your libraries/, add the functions there and finally set the rules without callback_ and just their function name.
Example:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
/* set_rule('custom_require') */
function custom_require($str) {
return (bool)$str;
}
}
Robin's answer is the easiest way to deal with it; however, the why you can't is this:
look in your system/libraries/Form_Validation.php, line: 587
if ( ! method_exists($this->CI, $rule))
{
continue;
}
This check is done on all callbacks. Helpers are not classes & not loaded into the CI instance - and so not available from the Form_Validation library (because of the way it is specifically coded in this method)
I tried to encode the ID of a journal in the URL of my Code Igniter application and the retrieve it in my controller. My end goal is to access the page http://mysite.com/journal/3 and get to access a page containing details about the journal with ID 3.
In my journal.php controller file, I have
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Journal extends Controller {
public function index($journalId) {
$data['journalId'] = $journalId;
$this->view->load('journalPage', $data);
}
}
?>
In my journalPage.php view file, I have
This event has ID <?= $journalId ?>.
I wrote this rule in my routes.php file.
$route['journal/(:num)'] = 'journal/$1';
Here is the .htaccess file in my html public folder.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
However, when I go to mysite.com/journal/3, I get a 404 error. Why?
$route['journal/(:num)'] = 'journal/index/$1';
(:num) will become invalid if you encode
Edit:
If you use CI's encryption class to encode your ID(pointless)
you will need to modify it to make sure the string is uri safe.
Based on what I see above this is just begginers mistake on CI routing.
You .htaccess is ok (you are just removing index.php from url).
On other 2 steps (you have problem in your controller and in your routes config).
First in controller, when creating new controller you should extend CI_Controller
To make story short, this is how your journal.php file should like like:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Journal extends CI_Controller {
public function __construct() {
parent::__construct(); // This is MUST!
}
public function index($journalId) {
$data['journalId'] = $journalId;
$this->view->load('journalPage', $data);
}
}
Now when you have updated this, we come to routes config.
Config line you wrote only can confuse CI, nothing more, nothing less.
Structure of CI route shoud be like this:
$route['journal/(:any)'] = 'journal/index/$1';
This would redirect all traffic from journal/[ID] to controller named journal, to method named index with param [ID].
You must define index part in routing.
Try this, and everything should be working fine.
Cheers.
First of all: You don't need to create a empty constructor (This is must [nothing!]).
If you want to load some libraries, helpers or models at the beginning of all functions for that controller, so it is needed, otherwise it don't.
Other tip: Try to do not pass parameters on a controller function. You can catch and manipulate uri values with CodeIgniter's URI Class.
Just do this:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Journal extends CI_Controller
{
public function index()
{
$data['journalId'] = $this->uri->segment(2);
$this->view->load('journalPage', $data);
}
}
You better take a look on the URI Class. It is quite simple a handy!
http://codeigniter.com/user_guide/libraries/uri.html
PS: Don't need to fix anything on routes too.
Hug