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"
Related
I've seen some sites use links such as: www.example.com/r/0000 - the zeroes are the ID of the post, user, etc.
What I'm using now would be www.example.com/view.php?id=0000, which looks much uglier.
What's the term for the first method shown, and how would I go about implementing it to be handled by a PHP script on my site?
These websites use MVC pattern (perhaps use frameworks like Laravel, Slim, etc).
Routes in Laravel:
Route::get('proyects/id/{id}', array('as' => 'proyects_id', function($id) {
/* This code is executed. You can return a view or make other things */
}));
When I visit www.mywebsite.com/proyects/id/3 The route runs; you can also execute a function in the controller as follow:
Route::get('proyects/id/{id}', array('uses' => 'ProyectsController#makeAnything'));
This way you can pass information through the URL without that notation.
I recommend studying Laravel, it is really useful.
Laravel website: http://laravel.com/
This in called Human Friendly URLs. You can do this by configuration your .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
# if requested url does not exist redirect it to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
</IfModule>
All requests will be redirected on index.php. Then you need use pattern routingŠ± which contains in all popular frameworks - Symfony, Laravel, Zend and other. But in its implement no big deal.
I'm new to mod-rewrite and I have tried to mod-rewrite this url with no success.
URL structure like this :
http://mysite.com/script.php?id=15751890&xp=862297&wm=1721&ls=2725&he=63530&ks=23050&eath=53588&tk=10&ck=john&rk=37
I want to mod-rewrite it to :
http://mysite.com/script.php?id=15751890
Or :
http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37
I followed this http://wettone.com/code/clean-urls and this http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/
But just can't do it write
What to type in htaccess file
Well, if you really want to write such a rewrite you should be able to use this in your htaccess file. There are probably better ways to handle the URL's however, I'm giving you what you asked for.
NOTE: I'm going off of the URL you provided.
RewriteEngine on
RewriteRule (.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*) script.php?id=$1&xp=$2&wm=$3&ls=$4&he=$5&ks=$6&eath=$7&tk=$8&ck=$9&rk=$10&%{QUERY_STRING}$ [L]
So you can use this type of URL with that rewrite.
http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37
Refer to my answer to this question:
php, handle unique URLs
This will give you this:
http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37
Instead of trying to figure out every rewrite combination possible, you should learn the MVC routing method.
This way, you route everything to a PHP router, and then direct the request to controllers, etc, as needed.
HTACCESS to rewrite everything:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
In your PHP script, you can use: $_SERVER['REQUEST_URI'] to get the requested URI.
Example: http://example.com/foo/bar/95 would rewrite to index.php, and $_SERVER['REQUEST_URI'] would then be /foo/bar/95 - and then you route accordingly.
Typically, your URL structure would be like:
http://example.com/controller/action/param_1/param_2/param_3/etc
Whereas "action" is just another name for a controllers method. How you code it is up to you.
I know that in Codeigniter, the default controller is always called if a controller is not specified in the url. Furthermore, if a function is not specified in the url, the index function of the controller will be called by default. However when I enter the following urls in the browser address bar I get different results for each:
http://localhost/appfolder/
And
http://localhost/appfolder/index.php/defaultController
For the first url, it seems that the user is not logged in even if indeed the user is logged in. The second url works fine and does the correct thing whether the user is logged in or not, that is, it excecutes the index function of the default controller. It seems to me that the first url doesn't excecute the index function of the default controller. I'm racking my brain trying to figure out why entering the two urls produces different results because according to what I know, they're supposed to have the same results; i.e they're both supposed to call the index function of the default controller.
I'm sort of a newbie in using codeigniter (have used it for about 2 and half months now) so any help would really be appreciated.
This is an edit:
I know most of the comments here are about using a htaccess file but isn't it supposed to work the same way whether index.php is in the url or not? Does anyone have any other suggestions on what I can do to pinpoint the problem?
If you want to access the site through the url http://localhost/appfolder instead of http://localhost/appfolder/index.php/defaultController you will need to set up a .htaccess file in your sites webroot folder (the folder containing index.php). This is pretty much what I use on my site (customized for your setup):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /appfolder/index.php/$1 [L]
Alternative
RewriteEngine On
RewriteBase /appfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Similar question with plenty of discussion and other methods:
How to remove "index.php" in codeigniter's path
Set the default controller in the routes config file:
$route['default_controller'] = 'Blog';
From the docs:
CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable.
Hey use this one in your htaccess file i think it will work............
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
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.
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