I'm using Codeigniter(PHP Framework) and I have URLs like http://www.mysite.com/age/21/gender/female/name/jamie/city/boston/userid/1234.
Is it possible to make the URL more SEO/user friendly? Like http://www.mysite.com/1234-Jamie-Boston and somehow still be able to pass the values found in the original URL strings like age =>21 and gender => female?
I think you could - use mod_rewrite to rewrite your new url from http://www.mysite.com/1234-Jamie-Boston to http://www.mysite.com/user/1234. In your user controller load from the db the one with id 1234 and set all his other properties
Or without the mod_rewrite - you could modify your application/config/routes.php file with an entry like:
$route['(\d+)-.*'] = "users/display_user/$1";
This way your SEO friendly urls will point to class Users, method display_users and you will get the id as parameter
The way that PHP works is that if you send 15 parameters to a function which only requires two, then PHP can more or less "swallow" the other arguments (they're still accessible, but they are less than optional). In your case, I would create a Users controller, perhaps with a display function which only takes one parameter, the user ID. That way, you can have /users/display/1234/whatever/you/would/like/zodiac_sign/stop_sign/favorite_letter/favorite%20punctuation/... Need I go on?
You can get around the need for a controller/method as part of your URL by using the $routes config file... In this case, I don't think user/display would really hurt SEO. I would not use mod_rewrite to do that simply because the architecture is already there in CodeIgniter.
Related
i'm very new to Yii, Normally yii serve like this url format
baseurl/app_dir/conroller/action/parametername/vaule
But i want to show up it like this
baseurl/app_dir/conroller/action/vaule
means without parameter name.
i read lot of solutions but couldn't find, does anybody knows how to do it, please help me.
It is somewhere in docs, here is example:
'/user/<id:\w+>/' => 'user/view/'
This means, that request /user/someUserName will be directed to action view in controller user with parameter id with value someUserName. This way you have urls with just value, but in application it is available as named $_GET parameter.
NOTE: \w+ is just regex, which are supported here, you could for example use \d+ to limit value to numeric etc.
I want to know whether it is possible to write pretty URLs in Code igniter or not?
I mean i have a code igniter project in which i am displaying data from uri segments like this:
http://myproj.local/category/subcategory/64/239
where 64 is the id of my category (e.g Garden Products )and 239 is my subcategory id (e.g Brushes).
Is there anyway I could write URLs like this in code igniter:
For categories:
http://myproj.local/Garden Products/
http://myproj.local/Household Range/
and for subcategories:
http://myproj.local/Garden Products/Brushes/
http://myproj.local/Household Range/Buckets/
Just like wordpress or other CMS.
IN SHORT,I WANT MY CODE IGNITER MVC framework TO DISPLAY URLs /Pages like these:
http://myproj.local/Garden Products/Brushes/
http://myproj.local/Household Range/Buckets/
Is this the kind of thing that can be achieved in the Code igniter or not??
IF YES, then how can I do this and how can i write this in code igniter ??What approach shall i use to achieve this ? I would appreciate if you could help me in this regard.Thanks
I have answered a simular question on this before.
Check this out and see if it helps.
Other options include database driven routing, take a look at this link.
Also there is an article/example on reverse routing here that may prove helpful.
SEO doesn't recommend spaces in URL
you can refer to seomoz
Instead of using IDs, you can use unique string names so http://myproj.local/category/subcategory/64/239 becomes just http://myproj.local/category/subcategory and then use routes to redirect anything that's not e.g. images, css, etc. whatever you use, to be redirected to a common controller/method which takes category and subcategory as parameters.
Also, CodeIgniter has a nice helper function url_title that will help you write unique string representations of your categories and subcategories... You just need to make sure they are unique.
The other way is like others said, make a whitelist in your routes file and make each redirection manually.
This why you have the routes file,however i know only of hardcoing categories, dunno about doing that automatically. you can do something like this :
$routes['Gardents/Products'] = "category/subcategory/64/239";
My current approach has been to use to _remap function provided by codeigniter to get the URI segment in order to check if the language is "en" or "np"
Here is a sample:
function _remap($url_title){
$this->_identify_language($this->uri->segment(1));
$data ['sub_categories'] = $this->category_model->get_category_list_by_url($url_title)->result_array();
$data ['news'] = $this->news_model->get_news_list_by_url($url_title)->result_array();
$data ['url_title'] = $url_title;
$this->_render_front_view('main',$data);
}
I am using this technique on every controller. Which is well not very efficient.
I wanted to ask if using sessions to store language codes would be better or is my current technique good enough?
Are there any other ways i can do this multi-lingual thing?
Of course my database is currently shaped for 2 lanaguages and i have seperated the fields. e.g:- title_en, title_np. these are echoed according to the language field used.
Lots of parts to this.
Your URL's do not really need to be /en/ and /fr/ unless you want it to be used for Google Analytics. Spidering doesn't make a lot of difference. Accept-Language headers can be just as reliable.
Globally parse this URL segment. You can use this method or the Accept-Language, but either way you need a hook, a MY_Controller or extend the Lang class.
Think about if you want the different languages to be totally seperate. For example, if I have an English page not translated to French, and the French page does not exist, should it show the English page or 404? You can either store the lang = fr in the database and take the value from a constant set in the hook/MY_Controller/etc.
WHERE lang = CURRENT_LANGUAGE
Structure your DB. title_en title_fr is one method, but it soon because unmanagable with lots of languages. Have a "pages" and "page_content" table, so that all generic information is in one table then all language specific (title, content, meta, etc) is in the page_conten table, which has a lang field.
There are a million ways to do all of this, but there is lots more to think about than just the URL. My favourite
I have been using this internationalization library for codeigniter and I find it suits my needs pretty well.
It extends the Lang class, and then in the constructor it parses the URI to figure out which language to use. So it is just loaded before you use any language files. You don't need to add any code to your controllers. It simply changes the setting in the language object. So you can retrieve the current language the same as you normally would:
$this->lang->lang();
If you have 500 news and 2 languages, changing url prefix in root will give you 1000 links, lets say "/en/hello-world" and "/np/hello-world" will have identical content and possibly the same title, which can be bad from SEO aspect. I would use session or cookies to store preferences, to preserve link juice.
Here is the real case, in the NewsNow.co.uk, there are many link of uptodate news from thousands of website. And the example for one of the news url:
http://newsnow.co.uk/A/471722742?-19721
all the news url are formated like that, but when we click it, we will be brought to the real url, for ex:
http://www.abcactionnewsx.com/dpp/news/state/bla-bla
anyone know how to achieve this efficiently ?
Store a table of 'internal' paths (the 'newsnow' urls) and the 'destination' urls in a database of some sort; sqlite3 would be a fine choice for smaller applcations.
You could hash the 'internal' paths if lookup time for specific strings was too slow in the database you chose.
When a request comes in, look it up in the database and send back a 302 response with the 'target' URL as the new location for the resource.
This is done using a rewrite engine that is built into common webservers like Apache or Nginx. These engines allow you to write rules that transform a url like the first one into something that would be better understood by your php pages. For example, you could create rules that would turn your first link above into:
http://newsnow.co.uk/index.php?catagory=A&id=471722742&referrer=-19721
This is transparent to the user and they will only ever see the link they first typed in. You can then use the variables being passed in to perform whatever actions you desire. In this case you might want to use the variables to perform some kind of database lookup to retrieve the actual destination that you are interested in. Then it's just a question of performing a php redirect to the link in question.
Check out the following link for a very quick intro to Apache's rewriting capabilities (called mod_rewrite): http://www.besthostratings.com/articles/mod-rewrite-intro.html
So, here's an example on Forrst, a CodeIgniter website:
http://forrst.com/posts/PHP_Nano_Framework_WIP_Just_throwing_some_ideas_o-mU8
Look at that nice URL. You've got the root site, then posts, then the post title and a short extract. This is pretty cool for user experience.
However, my CodeIgniter site's URLs just plain suck. E.G.
http://mysite.com/code/view/120
So it accesses the controller code, then the function view, then the 20 on the end is the Post ID (and it does the database queries based on that).
I realised I could do some routing. So in my routes.php file, I put the following in:
$route['posts/(:num)'] = "code/view/$1"; - so this will make http://mysite.com/posts/120 be the same as http://mysite.com/code/view/120. A bit nicer, I think you'll agree.
My question is - how can I use a similar technique to Forrst, whereby an extract of the post is actually appended to the URL? I can't really see how this would be possible. How can the PHP script determine what it should look up in the database, especially if there are several things with the same title?
Thanks!
Jack
To get a URL like in your example you need to add a routing rule, like you've already done $route['posts/(:num)'] = "code/view/$1";. Forrst's url seems to be "mapped" (or something like that), I think the last part of the uri is the identifier (o-mU8 seems like a hash, but I prefer an int id) which is stored in the db, so if he queries, he splits the uri by the ndashes (_), and gets the last part of it, like this within your controller action:
$elements = explode('_',$this-uri-segment(2));
$identifier = $elements[count($elements)-1];
$results = $this->myModel->myQuery($identifier);
Basically the string between the controller/ and the identifier is totally useless, but not if your goal is a better SEO.
I hope this helps
See the official dicussions. The term that is often related to this is "slug". Haven't tested the approach from the CI forums myself, but the suggestions and examples look pretty good.
The URL helper in codeigniter has a function call url_title(). I haven't used it myself but I think it's what you are looking for.