How to remove Controller and function name from URL in CodeIgniter - php

I am having a serious issue with one application developed in CI.
Currently my URLs look like this
http://www.example.com/content/index/mission/
I want to remove /content/index/ from URL So, It should look something like this.
http://www.example.com/mission
I have routing and .htaccess method as well. But nothing seems to be working.
Here is my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|css/js/style/system/feature_tab/robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
I have also tried routing of CI by defining reouters in config/router.php But it was not working :(

For your specific example, you'll want to create a route (in application/config/routes.php) that maps
$route['mission/']
to
"content/index/mission"
In other words, $route['mission/'] = "content/index/mission";
See the CI documentation regarding URI routing for more info

You can go into application/config/routes.php and set your own URL routing rules. (i.e. use something totally different than Controller/Funcction). There should be an array called $route which lets you assign mappings of url => controller/function. Hope this helps.
Check out this guide, its right up you're alley:
http://codeigniter.com/user_guide/general/routing.html

Related

additional .htaccess in subfolder | mod_rewrite for query parameter

I have a little mod_rewrite problem and really need some help.
I need to have a second .htaccess file in a subfolder for a project on my domain.
The URL to the project is like:
https://example.com/project-name/
Now I have a query parameter for which I want to create a nice URL with mod_rewrite.
The URL including the parameter looks like this:
https://example.com/project-name/index.php?preset=nameofapreset
And I want it to be:
https://example.com/project-name/nameofapreset
The important thing: The mod_rewrite has to work for the current folder only. Without having any info about the current path, cause I need to use the file in other folders, too.
That's what I thought should work (cause I am using a similar rewrite rule in another project):
RewriteEngine On
RewriteRule ^([a-z])/?$ index.php?preset=$1
But it doesn't. And all my trials fail.
Any ideas how I can make it work?
Thanks!
You can use this rule in /project-name/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?preset=$1 [L,QSA]

unique url to each username in codeigniter

I am developing a website in which i want to create a unique url for each user account.
means
http://example.com/username => route to controler/index/username
http://example.com/username2 => route to controler/index/username2
i am using codeigniter and the pattern of URL is :
http://exaple.com/controller name/method name/argument
I am getting no idea that how can we do this kind of routing just like facebook do ??
With CI routing you can achieve this very easily.
In your routes.php config (application/config/route.php) file you add a line similar to this one:
$route['(:any)'] = "controller/index/$1";
The left bit is the one you type or have in your browsers address bar. The right bit is what it routes to. In your case the name of the controller and method, followed by the argument. For your case that will be the user. It's an alphanumeric variable so that's how you pass it.
The complete reference can be found here, but the example above should make everything clear:
http://ellislab.com/codeigniter/user-guide/general/routing.html
The argument http://exaple.com/controller_name/method_name/argument section is what needs to be unique in your case.
You can do this easy with codeigniter routes. Have a look at the manual: http://ellislab.com/codeigniter/user-guide/general/routing.html
You need modify the .htacces to replace index.php for example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /<your_root_foler>/%{HTTP_HOST}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /<your_root_foler>/%{HTTP_HOST}/index.php/$1 [L]
</IfModule>
This line (RewriteRule ^(.*)$ //%{HTTP_HOST}/index.php/$1 [L]) replace all index.php in your app where do you use codeigniter
Afte you can modify your routes.php like say "bottleboot"

How can I use .htaccess to modify the functionality of Phil Sturgeon's CodeIgniter REST library so object IDs can be passed in the URL?

I am using Phil Sturgeon's REST server for CodeIgniter and want to modify the functionality so I can support URLs like these:
http://api.example.com/users
http://api.example.com/users/1
To get a list of users and a single user respectively, instead of the ones it supports like these:
http://api.example.com/users
http://api.example.com/users?id=1
I read on his blog that this should be possible using mod_rewrite, but have been unable to get this working as expected.
The default CodeIgniter .htaccess looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
And I have tried to add my own rules to attempt to achieve this functionality. This is one that I expected to work correctly. It is placed after the activation of the RewriteEngine and before the CodeIgniter rules.
RewriteRule ^users/([0-9+]) /users?id=$1 [NC]
I figured this would then cascade down to the next rule which would route through CodeIgniter and then hit the correct users.php controller, then the index_get method (as remapped by the REST Server).
Instead, I get an 'unknown method' error - it appears as though CodeIgniter is trying to use the user integer as a function, for example in users/12 it is trying to find the 12() method in users.php.
Does anyone know what is going wrong here, or can recommend a solution to this problem?
CodeIgniter uses a front controller pattern and supports clean URLs. This means that it should be able to transparently pick up and route requests they way you want. You should be able to set your web server to route all requests to CodeIgniter's index.php and modify it's configuration file to suit.
Edit your system/application/config/config.php file and set the index_page variable: $config['index_page'] = '';. Then, edit your .htaccess file or your virtual host configuration file in Apache to use something like:
# Turn on the rewriting engine.
RewriteEngine On
# Default rewrite base to /
RewriteBase /
# Rewrite if the request URI begins with "system":
RewriteCond %{REQUEST_FILENAME} ^system
RewriteRule ^(.*)$ index.php/$1 [NC,L]
# Or if it points at a file or directory that does not exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Also rewrite it to the front controller.
RewriteRule ^(.*)$ index.php/$1 [NC,L]
Edit: Check out this answer from the author himself, who says that CodeIgniter should pick up either query string or URI segment styles by default.
Edit 2: Ah, I see what you mean. You don't want to have the query variable name as a URI segment. You can probably fix this by modifying your routes file, such that it sends all queries to a single method on that controller.

URL rewriting redirecting to a single file

Let's say I have the project folder as follows:
folder/models
folder/view
folder/controls
folder/public
folder/library
Now let's say that the site folder is folder/public/ and inside that folder there's just one file called index.php. This file handle all the site page request via the GET parameter index.php?page=user for example will call the user.php file of the application in another folder. The point is that I'd like that an URL such as:
www.site.com/index.php?page=user&id=1
became
www.site.com/user/id/1
How can I do that?
This was taken from CakePHP .htacess rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
It will render everything under your host
http://www.site.com/* --> http://www.site.com/index.php?url=*
from here your index.php could parse $_GET['url']
//e.g browser requests www.site.com/user/id/1
$url = $_GET['url']; // user/id/1
$params = explode("/",$url); // array(0=>"user",1=>"id",2=>"1")
RewriteRule ^user/id/([0-9]+)$ index.php?page=user&id=$1
But it sounds to me that you should use so called router, redirect all trafic to index.php...
http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/ (check out this link)
In your case there is no point in using /id/, but here you go:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/id/(.*) index.php?page=$1&id=$2
Or what's a way better approach:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
Then handle the request in your index.php file by checking $_SERVER['REQUEST_URI'] against the patter you dream up.
Without apache rewrite rule, a micro framework named Slim can makes routing and templating for your php project. You'll define your routes only in index.php file. Like ;
Slim::get('/', function () {
Slim::render('index.template');
});
You will be implementing what is called the Front Controller pattern. If you Google that you will find several php implementations. I thought this series on building your own php framework was good.
http://fuelyourcoding.com/php-frameworks-just-roll-your-own-part-1/
Are you using Apache as web server?
If yes you can use *mod_rewrite* to accomplish that.
I have not done this myself, so I can't give you detailed instructions, but searching with google, using a search string like "mod_rewrite examples" lands you a lot of seemingly good tutorials.

Site URLs using a PHP "pretty url" framework

I have been experimenting with the lightweight NiceDog PHP routing framework, which routes like this:
R('entries/(?<id>\d+)')
->controller('Entries_Controller')
->action('show')
->on('GET')
Now the .htaccess file is set up to do this redirect like so:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
My problem is when I want to make a URL to somewhere, it seems impossible to do so.
Say I've requested the page entries/5, and on that page I would like to link to another entry entries/6:
Next Entry
This resolves to the address http://localhost/folder/to/project/entries/5/entries/6
Not what I want.
The href /entries/6 would link to http://localhost/entries/6
Also not what I want.
To work around this, I created a function to handle this problem:
function url($route) {
return "http://localhost/folder/to/project/$route";
}
So I can now write
Next Entry
which now links to http://localhost/folder/to/project/entries/6, which is exactly what I want.
However, I have to do this for EVERY in-site link, and it seems like there could be a better solution that doesn't involve an externally created URL.
Is there a "better" way to fix this problem? Is this a "common" problem with PHP frameworks? (It seems it would be)
The easy alternative would be to use <base href="http://example.org/your/project/index" /> in your page templates <head>. But that's basically like having full URLs generated. And yes, it's also valid for XHTML and still in HTML5.
I don't know about NiceDog but other frameworks I have used have a built in function that can convert a route to the corresponding URL.
For example in Symfony this would look something like:
Link Text
The routing system will then reverse resolve this into the URL relative to any root you set in the config.
Could you use:
RewriteBase /folder/to/project/
in your htaccess file making
RewriteEngine On
RewriteBase /folder/to/project/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Categories