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.
Related
Hi Dear Stackflow Community i am newbie in Codeignite coding so i hope you guys give me the time to fix this error thanks is advance :)
So basically i followed a tutorial here and i disabled index.php from URL that problem was fixed but here is my .htaccess right now and i still have the problem of example.com/?home/signin or example.com/?browse/ Or /?admin/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{QUERY_STRING} ^(.*)i=[^&]+(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1%2 [R=301,L]
</IfModule>
AddType text/vtt .vtt
But the problem is i followed every single tutorial here in stackoverflow on disabling the name of the controller from URL like /?home/ or /?browse/ and every single controller like /?admin/ show in my URL i want a clean url directly with no controller name or the ? mark in it i tried going to my routes.php to change things in it following tutorials but still same issue remains here is how my routes.php looks like right now
*/
$route['default_controller'] = 'Home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Please help me i attempted every single solution but nothing yet, I want to disable the name of the controller from URL like /?home/ or /?browse/ and every single controller like /?admin/ shows in my URL i want a clean url directly with no controller name or the ? mark my controllers are Home, Admin, Browse, General, Payment, Updater can you give examples with them exactly so i can try please to fix the problem according to my controllers
Modify your route file with this and my advice will be to read more about CodeIgniter routing method
$route['signin'] = '/home/signin';
routing work like this, update the route file as you need
$route['YOUR_METHOD_NAME'] = '/YOUR_CONTROLLER_NAME/YOUR_METHOD_NAME';
Try with this:
this my htaccess
// .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
$route['404_override'] = 'my404';
$route['translate_uri_dashes'] = false;
$route['default_controller'] = '/Home';
$route['home'] = '/Home/index';
$route['user-list'] = '/Admin/users';
$route['setting'] = '/General/account';
$route['payment-paypal'] = '/Payment/paymentByPaypal';
and access this way: http://localhost/ (this accesses a Home controller with the index method)
or http://localhost/payment-paypal (this accesses a Payment controller with the PaymentByPaypal method)
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
I'm working on a codeigniter project and i have the following url:
mysite/Project/frontend/tournaments/table
and i would like to turn it into:
mysite/Project/tournaments/table
What should i write in .htaccess??
This is what i already have:
RewriteEngine on
#Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Have you tried it?
RewriteRule ^mysite/Project/frontend/tournaments/table/([^/]*)$ mysite/Project/tournaments/table/$1
Simple Rewrite rule will solve your problem i think
So, this is how I would do it using CodeIgniter Routing feature:
Assuming Tournaments is your controller.
//Example: mysite/Project/tournaments/2332
// Will accepts numbers or letters
$route['Project/(:any)/(:any)'] = 'Project/frontend/$1/$2';
//Example: mysite/Project/tournaments/table
// Will accepts only letters.
$route['Project/([a-z_]+)/([a-z_]+)'] = 'Project/frontend/$1/$2';
You can add another route rule for admin URLs like the following:
//Example: Project/admin/users/add
// You can use either (:any) or ([a-z_]+)
$route['Project/admin/(:any)/(:any)'] = 'Project/admin/$1/$2';
I suggest you to read Routing documentation, it's a bit tediuos at the begining, but it will help you a lot!
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.
I have the following code:
$this->video->videoupdate($userid, $title, $id);
redirect("admin/videos", "refresh");
But the redirect is not working and I don't know why (I am using CodeIgniter)
Have you sent anything to the browser prior to calling redirect? From the user guide
Note: In order for this function to
work it must be used before anything
is outputted to the browser since it
utilizes server headers.
have you loaded the URL helper?
$this->load->helper('url');
Load this in your controller. I usually put mine either in the constructor or the autoload config.
try this :
redirect("admin/videos");
without "refresh"
Remove the echo if any above redirect
Check your htaccess, write like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
RewriteBase /sitename/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d``
RewriteRule ^(.*)$ /index.php [L]
I have just fixed it with this ->
$config['base_url'] = 'http://localhost:3000/';
So apparently you must define this before use your url helper.