How to structure hyperlinks when using Clean URL or Pretty URL - php

I've tried to search for the best way to handle links on a dynamic website when using "Clean" or "Pretty" URLs, but have not been able to find anything.
I've found a LOT of information on how to use mod_rewrite and try_files, which I've implemented successfully, now my php front controller parses all the parameters on a URL, and it links to the page correctly. No problem here.
The issue I'm having is how to best build all the links on my pages. Currently my links are all in the format eg. "www.site.com?do=post&id=23" which works fine. However I'd like them to display as "www.site.com/post/23" which already also works fine, because of try_files, and how my front controller parses all the parameters from the URL.
However do I now go through all my code and change all the dynamically built links to build in the "www.site.com/post/23" format? It seems like a lot of work, and to be honest I'd like to leave it an "option" to either use Clean or Dirty URLs, similar to how Wordpress allows it as an option in their Admin panel.
Do sites like that keep links in the format "www.site.com?do=post&id=23" and use a rewrite function on all the links when the page is created? So the links show up as Clean when the user sees it?
I'm confused as the best way to handle this, and hope I explained what I'm looking for. I just want to know how best to handle the dynamic links and have it optional to display as clean or dirty url format, for lack of a better word.
Thanks for any help.

That's a very conceitual question...
You could see how Laravel framework router works.
You do not need to use it, but you can get ideas there.
The router class is responsible of know how to create a url to some resource, page, action, whatever you want.
So in your view you just call a method that return the url.

Related

I like to change my id number to my post title in php

I am using this function in php
http://domain.com/page.php?do=post&id=124
but i wish to change this URL to
http://domain.com/page.php/this is first post.html
How can I change this URL?
You need to have knowledge of .htaccess to accomplish this.
What you need to achieve here is URL Rewriting.
What is "URL Rewriting"?
Most dynamic sites include variables in their URLs that tell the site
what information to show the user. Typically, this gives URLs like the
following, telling the relevant script on a site to load product
number 7.
http://www.pets.com/show_a_product.php?product_id=7 The problems with
this kind of URL structure are that the URL is not at all memorable.
It's difficult to read out over the phone (you'd be surprised how many
people pass URLs this way). Search engines and users alike get no
useful information about the content of a page from that URL. You
can't tell from that URL that that page allows you to buy a Norwegian
Blue Parrot (lovely plumage). It's a fairly standard URL - the sort
you'd get by default from most CMSes. Compare that to this URL:
http://www.pets.com/products/7/ Clearly a much cleaner and shorter
URL. It's much easier to remember, and vastly easier to read out. That
said, it doesn't exactly tell anyone what it refers to. But we can do
more:
You can find more information here.
You have to perform url rewriting. Its a technique for you you need to configure your .htaccess file.
Check this for Details

php dynamically create new pages in custom cms

I'm creating my own CMS from scratch as a way to build my php and mysql skills. Everything is going well, but I'm at the point where I want to create individual post pages for each blog post I write. So the index.php page has a list of all my blogs with snippets of each post and there is a read more button that should take the user to the full page for each blog post. Each post has a url created from the blog title entered in the "create post" form. I'm trying to figure out how to create unique pages for each post without passing the title, subhead, post content and other info through the GET.
This also dovetails with another feature I'm trying to add. I want to be able to create individual pages using a "create page" form like I did for my posts. So if I want an "about us" page I go to my admin form, fill out the title, add the content, and when I hit submit it creates the page dynamically. I have thought all day about how I'd do these two things but can't quite figure out how I can do this.
FYI, I'm not asking for code, I just need a push in the right direction as I try to conceptualize how to achieve this. Thanks!
If you're not familiar with the Model-View-Controller pattern, reading up on it might be prudent. MVC is frequently the right starting place for high-level design of web applications.
Also, a CMS is a big enough project you should consider using a PHP framework like CodeIgniter, Symfony, Zend, etc. to make your life easier. It removes a lot of the drudge work and common tasks.
Dynamic Page Creation and Display
I think you want to split it into two things: the text content (basically what you put in the forms) and the HTML templating surrounding that content.
When you make a page or blog post, you would want to store the actual content (what you type into the creation form) in a database of some sort (not necessarily an RDBMS, but if you're trying to build MySQL skills it's a reasonable choice).
Then you would use a separate function to bind that content into an HTML template and present it to the user when they load a given page.
URL Routing
To get nicer-looking URLs you can use something like apache's mod_rewrite. You can use that to convert a URL like this:
posts/how-to-make-a-cms
to this:
posts.php?title=how-to-make-a-cms
Then you can have posts.php read from GET as normal. How you choose to do the conversion is pretty open-ended.
To avoid getting really complicated rewrites, people often just structure everything to go to a central routing script which figures out what class and method to call and what arguments to pass it. So it would rewrite the URL above to:
main.php?a=posts/how-to-make-a-cms
Then main.php would parse out the segments of that argument from GET and figure out where to send them. Like it might take posts/show/how-to-make-a-cms and do something like:
$o = new Posts();
$o->show("how-to-make-a-cms");
If you do it that way, I think you can avoid mod_rewrite entirely as long as you're willing to accept only slightly pretty URLs, like this:
mysite.com/main.php?/posts/show/how-to-make-a-cms
I haven't done this type of thing before (because the frameworks do it so beautifully already), so I might be missing some minor details.
You should watch some tutorials from phpacademy.org or thenewboston.org, they have best and most valuable tutorials ever made about PHP.
I think you may try to start from that course/playlist:
phpacademy.org: PHP Tutorials: Creating a Blog
If you don't understand everything, watch this:
thenewboston.org: Official Beginner PHP Tutorials Playlist!
If you have no problems with PHP itself you may try to use some simple framework with MVC support. That helps A LOT in variable handling between pages, makes work with database easier etc.
phpacademy.org: Introduction to CodeIgniter
phpacademy.org: Introduction to CodeIgniter - Basic Website
I had the same problem. You can easily do this by using the fopen function. Here is a link to a tutorial: http://www.tizag.com/phpT/filecreate.php
<?php
function wwwcopy($link,$file)
{
$fp = #fopen($link,"r");
while(!feof($fp))
{
$cont.= fread($fp,1024);
}
fclose($fp);
$fp2 = #fopen($file,"w");
fwrite($fp2,$cont);
fclose($fp2);
}
//Example on using this function
wwwcopy("http://www.domain.com/list.php?member=sample", "sample.html");
//Another example
wwwcopy("http://www.domain.com/list.php?member=sample2", "sample2.html");
?>

PHP Image display using POST URL

The popular image hosting site Imgur is great and I'm new to PHP. I'm just wondering how they manage to get an image to display in the page when entering a URL such as:
http://imgur.com/gallery/yjg8v
How does the script know to display a certain image from yjg8v? Are they using $_SERVER['HTTP_HOST']?
Newbie here, not trying to be spoon fed, willing to read and study, just cant find much help on the internet about it.
Sites like imgur.com often "clean URLs" instead of traditional URLs with parameters.
Before clean URLs, you would see something like:
imgur.com/image.php?id=yjg8v
In your script, you can access the parameter via
$_GET['id'];
To produce clean URLs, you can use mod_rewrite in Apache (or a similar technology in your chosen web server) to map '/gallery/yjg8v' to '/image.php?id=yjg8v'.
You can find additional mod_rewrite information here.
yjg8v is a parameter being passed to a script called gallery. It doesn't look like a normal query string because there are various ways of coding a website and the normal query string cab look like a directory structure to the casual observer but actually be running through a front controller or mod_rewrite to get the parameters from the URL like a query string.
This SitePoint article is a bit old but demonstrates how this would work with mod_rewrite perfectly.
You can also use Apache's ForceType to accomplish this purely through PHP
To make it easier for you, this is called url rewrite, so the real url is like:
http://imgur.com/gallery/view.php?image=yjg8v
then use
$_GET['image']

Create Pages Automatically

Is there any method in Php by which I can create a page automatically based on a predefined template. Like if we create a new post in blogger it automatically creates page for that post with the name of that post, like this one:
http://learntoflash.blogspot.com/2009/12/exit-button-in-flash.html
Here Exit Button In Flash is the name of my post I have written and an automatic page is created for it.
Or like here on this website if we ask a question it automatically creates a page for that question. I want to know can I achieve this in Php or anything close to this ?
...here on this website if we ask a question it automatically creates a page for that question.
It sounds like you may believe an actual file is created when you post a question. My bet would be that this page is generated via the question id in the URL.
The only files created would be cached output, which may or may not resemble actual HTML pages.
You should use URL rewriting. This Apache module lets you define rules to rewrite web addresses in your desired way.
The process to make your web application ready for this, is not a short story so you should read more about it.
This article is a good starting point:
http://articles.sitepoint.com/article/guide-url-rewriting
This is acheived by using mod_rewrite. A good place to look for inspiration is the .htaccess used in Wordpress.
to something like that you have to grasp the very fundamental in php or any programming language at all, i mean the core of php is to create dynamical generated pages based on user/browser input.
You might need to take a quick tutorial about php might I suggest http://www.tizag.com/phpT/
good step for step tutorial
Edit:
if you're wondering how the websites seems to have created a html page for every question, the answer would be they're not they are probably using mod_rewrite as mentioned before to rewrite to url to print a little more user friendly url, the actual url could be something like this https://stackoverflow.com/index.php?post=4499289 in reality

How to use multiple files in a php webpage

I am creating a webpage with PHP/MySQL, and each page is in its own .php file.
How should I organize the addressbar, when changing the viewing page?
I have thought of two different ways to solve this:
1) www.mywebpage.com/?page=REFERENCES+pageid=GUID_TO_USE_FOR_DB_LOOKUP
2) www.mywebpage.com/references.php?GUID_TO_USE_FOR_DB_LOOKUP
Which should I choose? Are there other ways to do this?
Edit: When the user clicks a link to another page, I want to redirect the user to the corresponding .php file. The question is how to show this in the addressbar in the web browser.
With the GUID_TO_USE_FOR_DB_LOOKUP, I want to pass parameter to the .php file
Another way to do it:
www.mywebpage.com/references/GUID.html
You can use mod_rewrite to use URLs like this and have them mapped to "references.php?GUID=xyz" internally.
However, all solutions are fine and will work the same way (display the page), the advantage of the above is just that the URL is more "readable" as well as better for search engine optimization as the URL-"path" is parsed by search engines (other than using parameters).
Search for "mod_rewrite SEO" to find out more about this topic, e.g.:http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/
A couple of questions to ask:
Why do you care about the URL? Should it be human readable? Do you care about SEO? Do you like to use a "framework" with a single point of entry (which could work like your first example)? Do you want to "hide" your actual filenames from your users? etc. etc.

Categories