Codeigniter regexp routing - php

I have a problem with routing like this:
$route['(/[a-z]{2}/)'] = 'locale/somepage';
And in .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^(/[a-z]{2}/)$ /index.php/locale/somepage
I need replace first section of url (class or controller) and call an another controller. For example, I need to url as /en/page will call controller locale, but url need not be changed.
This code is not working. And if I try use only routes.php or only .htaccess, it not working too.
How I can make it work?

I think you've written your htaccess regexp rule wrong. You don't need to write it as a PHP regexp rule, try to rewrite it without the slash in your htaccess:
RewriteRule ^([a-z]{2})/page$ /index.php/locale/somepage
This will send any http://mypage.com/en/page to index.php/locale/somepage.
With that rule, CI will receive the url index.php/locale/somepage. At that point, CI will go to routes.php and will check if there are any rule to call a specific controller. If not, it'll try to go to a controller called locale, to load a method called somepage.
So, you don't need to use routes.php to modify again the apache url that you're receving to call another controller.

Related

How to rewrite an URL to a specific controller class with htaccess file in codeigniter

I have 2 controller classes in my application named Localhost/electronics.
The URL "Localhost/electronics/cameras" goes to
"localhost/electronics/home/details/cameras".
This is happening because of the rule
$route['(:any)'] = "home/details/$1";
2nd controller class specifics() and it's method showspecifics() is accessed through URL
`"localhost/electronics/specifics/showspecifics/camera1.`
How can I do the following?
Only with the help .HTaccess file, I want to.. be able to access the second class specifics() using URL
`"localhost/electronics/camera1` .
I am aware that using
`$route['(controllername/:any)'] = "specifics/showspecifics/$1";`
is a possible way close to achieving the clean URL but it's not what I want.
Please advise as to how to use htaccess to accomplish this.
Any idea is greatly appreciated.
Usually, you would capture the final part of the URL and then rewrite the request, e.g.
RewriteCond %{REQUEST_URI} !^/electronics/specifics/showspecifics/
RewriteRule ^electronics/(.+)$ /electronics/specifics/showspecifics/$1 [L]
The RewriteCond is there to prevent a rewrite loop.
This doesn't work with CodeIgniter however, because CodeIgniter looks at REQUEST_URI to determine the controller and method to serve this request. But REQUEST_URI isn't changed by the RewriteRule and remains /electronics/camera1, and CodeIgniter doesn't find an appropriate controller.
To change REQUEST_URI, you had to redirect instead of rewrite, e.g.
RewriteRule ^electronics/(.+)$ /electronics/specifics/showspecifics/$1 [R,L]
but this also changes the client's URL bar, which isn't desired in this case.
So, there's no way to achieve this with .htaccess and CodeIgniter.
To do this in CodeIgniter, you would use appropriate routes in application/config/routes.php like
$route['(cameras|smartphones|computers)'] = 'home/details/$1';
$route['(:any)'] = 'specifics/showspecifics/$1';
This handles the few categories by controller and method home/details, and everything else by the controller and method specifics/showspecifics.

Redirect to file passing url as a parameter

I have a task that's been giving me considerable trouble. Any help much appreciated.
I am using an MVC framework in PHP (codeigniter) and all requests are sent through a root index.php file.
I am trying to pass another site's url as a parameter to the defualt controller. Like this:
http://mywebsite.org/https://google.com
Apparently this can be done using with a mod_rewrite rule in the root .htaccess file
so ideally i would be able to use the 'http://google.com' as a parameter in my controller.
it requires some regexp and rewriteRule knowledge. Been struggling for a while.
Any ideas?
This would be a rewrite rule for the .htaccess:
RewriteRule /(.+) /index.php?url=$1 [QSA]
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa
With codeigniter this should do the job:
http://www.codeigniter.com/user_guide/general/urls.html?highlight=uri%20segments
The segments in the URL, in following with the Model-View-Controller approach, usually represent: example.com/class/function/ID
The first segment represents the controller class that should be
invoked.
The second segment represents the class function, or
method, that should be called.
The third, and any additional
segments, represent the ID and any variables that will be passed to
the controller.
And here is explained how:
Passing GET parameters through htaccess + Codeigniter

Using .htaccess file to remove a particular segment of the URL

My url link looked like:
http://localhost/CodeIgniter_2.1.2/index.php/pages/home
Then I wrote the .htaccess file using google as follows:
RewriteEngine on
RewriteCond $1 !^(index\.php|themes|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Now my link looks like:
http://localhost/CodeIgniter_2.1.2/pages/home
Now,I want to remove pages from this link. Can anyone help me figure this out?
So, my url could look like:
http://localhost/CodeIgniter_2.1.2/home
One way to do it is to create a custom route in the application/config/routes.php file, which routes the url 'http://localhost/CodeIgniter_2.1.2/home' to 'http://localhost/CodeIgniter_2.1.2/pages/home'
Custom routing is described here
The code will look something like:
$route['home'] = "pages/home";
EDIT
Your controller, method, and variable are named "pages", "view", and "home" respectively. Therefore, you should try the following route instead:
$route['home'] = "pages/view/home";
Also:
** Reserved Routes **
From the codeigniter documentation:
There are two reserved routes:
$route['default_controller'] = 'welcome';
This route indicates which controller class should be loaded if the URI contains no data, which will be the case when people load your root URL. In the above example, the "welcome" class would be loaded. You are encouraged to always have a default route otherwise a 404 page will appear by default.
Thus, you should not be setting the default controller to "pages/view/home." Rather, you should create an "index" method in the controller that defaults to the "home" view.
Also
Don't forget to change the $config['index_page'] from 'index.php' to '' in the config.php file located in application/config/config.php.
Try changing your rule so that the target is:
RewriteRule ^(.*)$ index.php/pages/$1 [L]

how to make codeigniter style URI segments

I was wondering how one could make codeigniter style url segments on a project.
Aside from an htaccess rule to capture the segments, how can this be done in PHP?
After snooping around the codeigniter source code, i could not find any reference to an htaccess file that captures the usage.
Any idea on how this can be done?
Assuming you are passing ALL requests to index.php via Apache mod_rewrite or Nginx:
// Get the current URL path (only) and convert encoded characters back to unicode
$path = rawurldecode(trim(parse_url(getenv('REQUEST_URI'), PHP_URL_PATH), '/')));
// array merge with defaults
$segments = explode('/', $path) + array('home', 'index');
//First two segments are controller/method
$controller = array_shift($segments);
$method = array_shift($segments);
// #todo serious security checking here!
// Finally, load and call
$controller = new $controller;
$controller->$method($segments);
Your controller would look like this
class Home
{
public function index($params)
{
print_r($params);
}
}
What you do is set up one single url param in htaccess, and then use a string splitting method to retrieve a model, controller, and view from that string which will then call a model class, a controller class, and then render the data into a view. the transformation would be something like the following:
mysite.com/index.php?url=/tasks/all => mysite.com/tasks/all
which calls the task model, which then calls the function called "all()" inside of the tasks controller.
As soon as this site goes back online, do look at the htaccess portion of the tutorial -- he did a good job of showing how its done http://www.henriquebarroso.com/how-to-create-a-simple-mvc-framework-in-php/
You still need something to "forward" all virtual requests to a physical file.
The idea is that any URI that doesn't match a physical file or folder on disk is rewritten (usually through mod_rewrite) to your index.php file (it's usually your index file so a direct call to the index works too), and it appends the URI to the path or as a query string parameter:
RewriteCond %{REQUEST_FILENAME} !-f # Not a real file
RewriteCond %{REQUEST_FILENAME} !-d # Not a real folder
RewriteRule ^(.*)$ index.php/$1 [L] # Rewrite to index.php, with a leading slash, followed by the URI
Alternatively, you can use a standard error document handler (still in .htaccess or apache config, but no need for mod_rewrite!):
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Now that control is passed to uniformly to an index.php file, you need a router mechanism to match the route to the correct controller. Ideally, you'd have a list of static and dynamic routes. Static routes are direct matches to the URI, and dynamic would be regular expression matches. Check your static routes first, as it's as simple as a single hash lookup, while you'll have to loop through all the dynamic routes.
For performance, it's nice to put your more common dynamic routes at the beginning of the list, and the obscure ones at the end.
Using the .htaccess file to employ mod_rewrite to rewrite the base of the url is essential to this. Otherwise, the browser will treat each segment as a supposed folder or file (depending on whether or not you have a trailing /)
once you have that, however, you can simply use the method described in this post:
how do i parse url php

Hiding default controller name in PHP CodeIgniter URLs

I am trying to remove the default controller name from URLs in CodeIgniter using htaccess; I have hidden index.php but also want rid of the default controller which is currently called con_index.
For example if site root was mysite.com,
mysite.com/con_index/function1 would change to mysite.com/function1 and so on.
All other controllers can remain in the url, so if I had another controller called locations with a function called location1, mysite.com/locations/location1 would stay the same.
I think this makes for a more conventional structure rather than a class and function name popping in there the second you leave the site root. Gnashed my teeth trying to achieve this, can anyone help?
Try this inside your .htaccess :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\/$ index.php/com_index/$1 [nc] [L]
This would replace any occurrences inside brackets with $1. So, when you're calling www.example.com/function_1/ it actually calls www.example.com/com_index/function_1
However, I'm not sure it works for CI because CI might have some restrictions for accessing the URL Route.
Have you tried using
$route['default_controller'] = 'con_index'
CodeIgniter Default Controller
enter code here$route['(:any)'] = 'controller_name/function_name/$1';
it will replace controller and then u can try to access url by not putting controller name in it.

Categories