Url rewriting for codeigniter - php

How to rewrite particular url of website using .htaccess or CI routing
http://www.domain.com/user_controller/newfunction/username
as
http://www.domain.com/username

$route['/(:any)'] = "/user_controller/newfunction/$1";
or try htaccess
RewriteEngine On
RewriteRule ^([^/]*)$ /user_controller/newfunction/$1 [L]

/application/config/routes.php
$route['/all/other/routes/must/come/first!'] = "wherever/you/want";
$route['/(:any)'] = "/user_controller/newfunction/$1";

Its easier to use CI routing system found at application/config/routes.php
$route['(:any)'] = 'user_controller/newfunction/$1';
Don't forget to set your base url at application/config/config.php
$config['base_url'] = 'http://www.domain.com/';
Add remember to remove the index.php in the url. Edit your .htaccess file at the root of your site
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|_searches|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
now in your user_controller, the function newfunction will look like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// load things like models, libraries, helpers
}
function newFunction($slug)
{
echo 'hello my name is'.$slug;
}
}
when you visit http://www.domain.com/nash, your code should print out nash

Related

Put language index into URL Multilanguage CodeIgniter site

My site is created in three languages https://anto-nguyen.com.
It works well to translate from one language to another one.
But.. I can't put the language indication into the URL.
I looked through all questions and answers that I could find here and have tried to use them, but unfortunatelly it doesn't work..
I use Controller LangSwitch
class LangSwitch extends CI_Controller {
public function __construct() {
parent::__construct();
}
function switchLang($language = "") {
$this->session->set_userdata('site_lang', $language);
redirect($_SERVER['HTTP_REFERER']);
}
}
The language is called by following code integrated into menu
<div>
<?= anchor(base_url("langSwitch/switchLang/english"), 'En'); ?>
<?= anchor(base_url("langSwitch/switchLang/french"), 'Fr'); ?>
<?= anchor(base_url("langSwitch/switchLang/russian"), 'Ру'); ?>
</div>
My .htaccess lookes like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]
And seems to be modified, but all suggestions I found doesn't work
Could you help me what to do that my link will appeared as /mysite/fr /mysite/en /mysite/ru?
All translation are in the folders language/english language/french language/russian
Mayby to change something in routes?
$route['default_controller'] = 'site';
$route['work'] = 'work/index'; //the URL 'work' will redirect to 'work/index'
$route['work/(:any)_(:num)'] = 'work/article/$2';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'site/$1';

Codeigniter subdomain 404 Not Found

I'm new to codeigniter and I'm working on a project that is done on this framework. When I try to access a url such as "mysite.com/about", it gives me and an error
404 Not found,
I tried many .htaccess file on wamp and a live host but the result is the same.
I can Access these file by using "mysite.com/index.php/about" but I want to access as this "mysite.com/about" this not only for about page but all so very page that I try to access by using like this "mysite.com/contact-us"
.How do I fix this?
Write this code in your .htaccess file and in wamp server click on rewrite_module
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
IndexIgnore *
</IfModule>
By default, Codeigniter will call the about controller, unless a method is provided in the second uri segment, the index() method will be loaded.
If the index() method doesn't exist, it will 404.
The other way of doing it is to add a line in your application/config/routes.php file. Such as:
$route['about'] = 'main/about';
This means, a request where the url matches /about will map to main/about
Main being the controller and about being the method within that for example.
See this part of the documentation for a more detailed explaination
https://www.codeigniter.com/userguide3/general/routing.html
Hope this helps
Make sure that you have named the Class for your controller as the controllers file name. Then try to access the page as shown in the code below.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Pages extends CI_Controller {
function about()
{
$data['title'] = 'About us Page';
$this->load->view('about', $data); //about is in views
}
}
?>
The file name should be pages.php in this case.
To access the about page type in this link mysite.com/index.php?/controllerfolder/pages/about.
If you want clean url and remove index.php? edit your htaccess. Use this link to know how to edit your htaccess
https://www.formget.com/codeigniter-htaccess-remove-index-php/.
Hope this is helpful. Thanks

Codeigniter: 404 on existing controller

When I set $route['default_controller'] to 'blog', CI opens my controller without problems. When I want to access it via URL, CI returns a 404.
My controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Saved at
application/controllers/Blog.php
Some other config things:
$route['default_controller'] = 'blog';
$route['404_override'] = '';
$config['base_url'] = '';
URL I try to use:
http://localhost:63342/project/public_html/blog/
.htaccess to hide index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
How can I fix this issue? I tried a lot of solutions from similar questions without success
If your project is in project folder in main directory of the local server, change your address to:
http://localhost:63342/project/blog
You configured that page as your default controller, so it should work even by:
http://localhost:63342/project

URI multilingual CodeIgniter

We're using Codeingiter library i18n (link) to create a multilingual site.
Before this, we had for example www.thedomain.com/register and register was a function in our controller. Now when we put this library it grabs the domain, the language string, the controller's name and the functions name: www.thedomain.com/es/homegf/register (where homegf is our controller).
We want this URI's to work without the name of our controller on it (www.thdomain.com/es/register) like in the librarie's examples but we think the problem is in our routes.php.
This is what we have in routes.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "homegf";
$route['404_override'] = '';
$route['^(en|es|de)/(.+)$'] = "$2";
$route['^(en|es|de)$'] = $route['default_controller'];
This is our .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|files|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You can find our code to review it at https://bitbucket.org/ticketcomunicacion/grinfood/src/17ddde60e340a1f2bc389f54ec579e1e903ee86b?at=multilenguaje
as i understand , you want the controller url without controller name .
if you want to change the URL from your controller name to other ,
in your routes.php write the following
$route['homegf/register'] = "register";
and you can access
domain.com/es/register
The problem was in our routes.
This is how we managed to make it work:
$route['^es/(.+)$'] = "homegf/$1";
$route['^en/(.+)$'] = "homegf/$1";
$route['^es$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];
$route['(:any)'] = 'homegf/$1';

codeigniter redirect from a view

I am building a site entirely on codeigniter .I have set my default controller as cuff.
so whenever users type the domain name it takes the control to that controller.
class Cuff extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('index');
}
public function navigate()
{
echo "test";
exit;
}
}
In my index view I want an anchor to navigate to a function in my default controller .
so when I write
our collection , it says page not found .
I have even set the base url like this
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
Cant seem to figure out the issue.
You missed the controller name
our collection
Set $config['base_url'] ='';
In your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|favicon|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Make sure your controller is in controller's folder (not any subfolder) and is properly named cuff.php
Try navigating to http://yoursite/cuff/navigate
If you still see errors try rewriting the last line in .htaccess to:
RewriteRule ^(.*)$ /index.php/$1 [L]
(without question mark).
Please tell if that helps!
EDIT: and your links better all be in this form:
Link
The problem is in your routing.
Edit your routes.php (located in the folder ../application/config/) and add the code below:
$route['navigate'] = "Cuff/navigate";
For more information regarding Codeingiter URI Routing:
http://codeigniter.com/user_guide/general/routing.html
Hope this helped you.
Simply you use
"<?php echo site_url();?>/cuff/navigate">our collection</a>

Categories