I've been thinking about how does people make a special link like
www.domain.com/this-is-a-link
then i checked how to make a slug URL and it was good, But i was wondering, Do i make a special column for SLUG URL inside the database and call the entire row using the SLUG instead of the id? Is that how all people do it? Or is there a better way?
[id title content slug]
[1 TheTitle LoremIpus the-title]
$query = "SELECT * FROM posts WHERE slug = $slug";
What you are looking for is URL rewriting. Please search on that topic. You will find articles like These, which has information about the URL rewriting!
Related
I'm trying to figure out to get a complete clean url for my blog. Right now I have a decent url that looks like this:
http://example.com/post/44/post-name
Here "44" is the Post ID which I use to get the correct post from the database.
But I have seen wordpress blog having url's like this:
http://example.com/post-name
How can this be achieved? as I need the post ID / $_GET['id'] to be able to get the post.
You don't necessarily need the id to retrieve the Post. You only need an indexed attribute with unique values.
To achieve this, your Post model needs to have a "slug" attribute. That slug attribute can be based on any other attribute or combination of attributes you choose, but it's usually based on a "name" or "title" attribute.
So to make all this work, when you create a new Post, you need to create a slug from the name/title and store that in the DB, along with the other Post model data. In your controller action you will then retrieve the /slug from the URL and query the db Post data for a Post with the same slug.
You'll also need to figure out to handle duplicate slugs as they need to be unique. Perhaps something like this would work: /slug-2
Make a database table mapping slugs to IDs, then lookup the ID from the slug in the URL.
When a blog is created,
INSERT INTO slugs_postids (slug, id) VALUES (?, ?);
Then when you do a lookup,
SELECT id FROM slugs_postids WHERE slug = ?;
Question:
how to make codeigniter dynamic url search engine friendly?
Example: 1
My Current URL: After selecting menu called "Articles"
http://localhost/lw_user/home_control/getMenu/52
Expected URL:
http://localhost/articles
Example:2
My Current URL: After selecting sub-menu called "thehindu" under menu "articles"
http://localhost/lw_user/home_control/getPage/6
Expected URL:
http://localhost/articles/thehindu
NOTE: This is dynamic URL and contents are fetching from database
What you are talking about is called as slug.
So, how to use slug?
Will explain with an example:
URL - http://www.example.com/products/apple-iphone-5S-16GB-brand-new/
1) Assuming you are having a product page and ofcourse product page needs some data from URL to understand which product to display.
2) Before we were querying our database using the id we are getting from the URL. But now we'll do the same thing (querying our database) just replacing id with slug and thats it!
3) Hence adding an additional column in your database named slug. Below will be your updated product database structure (just an example).
Columns Values
id (int(11), PK) 1
title (varchar(1000)) Apple iPhone 5S 16GB
slug (varchar(1000)) apple-iphone-5S-16GB-brand-new
price (varchar(15)) 48000
thumbnail (varchar(255)) apple-iphone-5S-16GB-brand-new.jpg
description (text) blah blah
...
...
I've also answered on slug before. Check if it helps.
How to remove params from url codeigniter
Codeigniter - SEO Friendly URL Structure (Slug Implementation)
This has to be done using the routes class.
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
I would suggest you look at the user guide for more info.
https://ellislab.com/codeigniter/user-guide/general/routing.html
I am writing some little hacks for wordpress by giving myself some used cases. I think that is the best way to learn this
I have created a scenario where I am checking if the page name is 'x' then fetch contents of posts categorized as 'y' in the page x. Works fine as long as I hard code the page names like this
if(is_page('x')){
query_posts('category_name=y');
}
Now I am thinking what if I generalize the x and retrieve all the categories and push them in an array. Iterate through the array and look for match like
if(pageName == postCategoryName){
query_posts('category_name=the correct category');
}
I believe I would basically have to create the post categories with the same name as the page name
Conceptually I am good with this but when it is coming to the syntax I am getting a bit lost. How should I approach this?
This could be achieved by getting the slug of the current page and then finding the same slug in a category.
e.g. if you were on the Geography page
$slug = basename(get_permalink());
$slug would contain geography so you can query that by:
query_posts("category_name={$slug}");
And that should return the posts in the category.
I am using the CodeIgniter framework and am having trouble figuring out how to get my urls to display the category name but still have my queries reference the category id.
I have a controller set up called jobs. The structure of the url is "example.com/jobs/listings/category_id" example: "example.com/jobs/listings/3" where 3 would be the category id for "software".
I'd rather the url say "example.com/jobs/listings/category_name" example: "example.com/jobs/listings/software".
The problem I'm running into is that the "jobs" table holds stores the category_id that the job belongs to and not the category_name. I have a separate table that holds just the category id's and their names. I'm not sure how to A.) Write my query and B.) Make an SEO friendly url structure.
My current query looks something like "SELECT * FROM jobs WHERE cat_id = 3;
Also, I'm not sure if I should be using mod_rewrite in an .htaccess file or if I should be using the routes.php file in CodeIgniter.
Any help would be greatly appreciated!
-Thanks
Just join to your other table.
SELECT * FROM jobs JOIN categories ON jobs.cat_id = categories.id WHERE categories.name = ?;
Make sure to put a unique key on the category name, if you don't have one already.
mod_rewrite is a poor solution to app routing and wouldn't help much here anyway. Stick to whatever your framework provides.
Have a look here http://codeigniter.com/user_guide/helpers/url_helper.html at URL_title()
Convert you category name using the url_title() function making sure you have no duplicates.
When a url is called Look up your category from your model using a simple where statement
Eg
$this->db->from('categories');
$this->db->where('name', $url);
Etc...
Use this to load your page titles etc then use the categoryid returned to do the look up on your jobs table.
i still , don't understand , how wordpress can understand what is this url refer to :
www.mysite.com/about-me/
they are using no identifier
if they using slug functions so how they can retain story information or in other word , how they change back the slugged title to select from database
It processes the "pretty" URL and queries the database with that data. Of course slugs are checked to be unique on creation. Details are in the function url_to_postid() in the file wp-includes/rewrite.php.
If you want to directly obtain the id from the slug you can query the database:
SELECT ID
FROM wp_posts
WHERE post_name = '$slug'
you might need to check wp_posts which is the default name, but it depends on the installation.
This is just a guess:
My guess is that they store the titles in a database, and make sure every title is unique. That way, they can do a look-up by title and know which item is coupled to that.