Optional URL params that are automatically appended if not included - php

My site has articles and I would like the URL to be like so:
example.com/(article id)/(article title)
There are two reason I would like the URLs like so, SEO, and readability.
This setup is close to how stackoverflow is set up. And just like on stackoverflow, I don't want the title to be required but if it's not including in the I want the back-end to append the title to the URL.
To understand what I mean, take the current page's URL, remove the title form it and go to that new url. If you notice when the page is refreshed the title was automatically appended to the URL.
When I think about how to achieve this, it requires two queries. The first query will get the title based on the id, then form the new url, and redirect to it. The second URL will query the database with the id again this time displaying the article.
Is this the only way? Can I append the extra param without actually redirecting?
Side note: I am using cakePHP

I think that yes, this is the only way.
Call #1 to example.com/(article id): the server would query the database for the id, see that the url isn't the canonical one[1] and do a redirect.
Call #2 to example.com/(article id)/(article title) would behave as normal, meaning some more queries to the database...
^ I suggest you store an article's generated url path so you could use it in other places too.
This way, if no title was provided, the path would just be "/(article id)" and you wouldn't need to do a redirect here.

Related

PHP Rewrite URL or Mask or Redirect?

I'm trying to get domain.com/employee?display_county=sd&dept=sales&id=23 to display in the URL as domain.com/firstnamelastname while retaining the URL variables.
And also if someone goes directly to domain.com/firstnamelastname it would still have the original URL variables.
How would one accomplish this?
Edit: OK, so I'm starting to rethink this...
Let's say I have a page that displays a list of users.
Each user has a clickable link that directs to domain.com/employee?display_county=XX&dept=XXXXX&id=XX
How could I get the URL to display domain.com/firstnamelastname when a user is clicked and still bring up the correct user?
I was thinking maybe using a POST method to pass the id to the script that displays the user and then rewriting the URL to display the firstnamelastname related to the id, but then browsing directly to domain.com/firstnamelastname would leave the id variable empty...

Pretty urls codeigniter

I have the following situation, I have some posts on my website which are access by id, lets say for example
http://websiteurl.com/posts/see/1
the posts controller, has a see method which looks for the id on the database, lets say it find it and the posts title is "Most amazing photos" , how could i configure the Route.php class of codeigniter so that this posts its accessible from an url similar to:
http://websiteurl.com/posts/see/most-amazing-photos-1.html
this posts are created dynamically and they are a lot, so setting one by one manually is not possible.
Thanks!
What you can do is use regex. Assuming that you will be creating different url tag for each photo you can have something like this in you route.php
$route['posts/see/(:any)'] = "posts/see/$1";
However it is necessary that you have unique url-tag for each photo otherwise it will be ambiguous to fetch photo from the url-tag.
Have it a go.
The url parameter you would like to achieve is called a slug
like wordpress does, you can query the POST via its id or its slug.
One way to implement this is every time a post is created, you save the slug to the db.the slug can be the title of the post or anything readable for SEO. So that everytime a user visits the site, your controller method may prcess the parameter if it is a slug or an id and continue the query using the given parameters.
then you can set up the routes like:
$route['posts/see/(:any)'] = "posts/see/$1";

Alternatives for sending a parameter

I know two ways for sending an ID to another page:
as URI segment. exemple: http://mypage.com/index.php/ID
as an input field in a form using POST or GET methods.
Is there another way than these tow?
The purpose is to have a list of records. When the user clicks on one of them, s/he gets the full details of the selected record in a different page.
"Is there another way than these two?"
You can store it in $_SESSION too. If that what you are curious about.
But it's not the best way with your current problem.
Simply use GET (www.yourpage.com/records/THEID)
Well, you might consider this to fall under your first option of a URL segment, but "URL segment" usually means some part of the URL before any GET parameters...just to make sure you're aware of this, you can also put in GET parameters in a URL yourself without creating a form.
These days, URI segments like index.php/123 are popular, but traditionally the way to link to a detail page like that was index.php?id=123; then you can access the ID using $_GET['id'].

Codeigniter Route ID

I've built a webpage that, in its products page has this prototype for its url
http://www.example.com/products/show/some-really-nice-product/512
The last bit of the url is the product id, which serves the sole purpose of searching the db for the product.
But, is it possible, using routing, to be able to hide that part and still be possible to search the product by its id?
Like
http://www.example.com/products/show/some-really-nice-product/ ??
Thank you
If you don't want to put a number in your URL, you can't later search by a number/id :)
So you could add a VARCHAR field to the table, say "urlized", and use "some-really-nice-product" as its content for the given ID (you'll need to 'urlize' all your product names and make them unique). Don't forget to index your new field...
Then, you could access the product page using this URL which is very SEO friendly (ends with ".html"):
http://www.example.com/products/show/some-really-nice-product.html
...by adding a route to CodeIgniter, somehow like this:
$route['^products/show/(.*).html'] = 'products/show/$1';
In your controller/model, search the database for the string that is passed to "show" method, instead of the ID
if you hide id you can't retrieve that from the url anymore
if you want to hide that anyway just do:
$route['products/show/(:any)'] = "products/show/$1/$2";

PHP MVC: How to dispaly content via text not id values through the URL

I am used to creating small php mvc structures etc and relying on passing the id of the item i need to display:
www.asite.co.uk/controller/method/IDnumber
But I see a lot of sites use say the title of a page within the URL.
www.asite.co.uk/controller/this-is-a-title
www.asite.co.uk/controller/i-am-another-section/this-is-a-title
MY question is its easy to get the contents via the ID method but who would you work it to use the text method to retrieve the data / content you require.
Initially I would think of removing the '-' separators then searching the 'title' field say for the result, but not sure if there would be a better/easier way.
Many Thanks
I would like to tell you that the text in url is just name of the page. As I also work in MVC so i know that it is only a page name, the requested data is in post method or stored in sessions which are taken forward to the page and after they are made in use, they are destroyed.
I use a column in my table content called "slug" and find the content searching by this column. This column needs to be unique, of course.
If you just want to display header differently, do it via htaccess:
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

Categories