I'm redirecting all requests to index.php, which parses the URL and fires the appropriate controller based on it.
Is it a good idea to change the way query arguments appear in the URL?
like http://site.com/somepage/sub-subpage/page=20,offset=100. then parse those arguments and pass them to the controller, because it looks more readable.
Or should I stick with the $_GET thing? like ...http://site.com/somepage/sub-subpage/?page=20&offset=100
Not too good idea because you'll have to implement query parsing yourself. I see no advantages in this way. And if you use standard ?name=val&name=val notation you have:
Automatic parsing and storage to $_GET[]
Possibility to start using POST in no time.
Less possible vulnerabilites in parsing. At least they are known.
Stick with standards and therefore its better you stick with $_GET thing.
YAGNI - You ain't gonna need it. Don't think too much, just do it. Apart from a matter of taste (someone might say "I dislike questions marks in my URLs"), there is a lot of benefit to just use the common format that is just working and for which many parsers/function do exist. Additionally you find documents you can refer to if you're unclear about the format.
Its better if you got nice urls (so called SEO friend URLs) even if you dont care google or its an admin area.
The reason for using nice url is
Its more readable.
You can change the parameter by hand.
When you paste it to email, IM or other media the url makes sense.
Ugly urls makes it difficult to read the actual values. Some times you need it.
When you see the address bar it looks nice and clean and you know where you are.
In ugly urls you dont know where you are. Each url seems a middle of no-where.
Creating clean url with a little help of mod_rewrite is not tough.
Rewrite all URI to your index.php as /index.php/REQUESTED_URL
In the index.php just parse the url and invoke controller.
I think this is only interesting for SEO. Have a look at Googles opinion to this question: http://googlewebmastercentral.blogspot.com/2008/09/dynamic-urls-vs-static-urls.html
So it makes sense when this "pseudo" URL defines a page with really different content (i.e. /user/clara, /user/tom ...) but avoid to put a dynamic variable like session IDs into this static form.
Related
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.
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
So I have a list of posts and when clicking on it, it should direct me to that particular post. Basically the way I code this is adding the id of the post to the end of the link, like posts/1 which will take me to the post with the id of 1. This is a basic design, because I see on many sites they do not have such urls...do they post the data to the new page? Or what is a better design to do this?
This is exactly how StackOverflow (among others) does this and it is good practice indeed. This is the URL of your post:
http://stackoverflow.com/questions/9257811/how-to-open-a-post-in-php-when-clicking-it-a-good-approach
The "/questions/9257811" segment is the id of the post. The other part is optional and is only used for Search Engine Optimization. Try to load http://stackoverflow.com/questions/9257811/WRONG-DESCRIPTION and see for yourself.
Edit: I couldn't use a wrong description in the above link, StackOverflow was correcting it automatically. It's easy to get the title once you get the id, which is how they autocorrect it. This avoids links like "/posts/34234/offensive-words-here" for example.
What you describe is the best way to send and receive an id in the URL. Most webpages do it this way. You have a link (example: Post #1.).
When applying url-rewrite it simply "prettify" the url by making it look this way http://www.yoursite.com/post/1, but in fact, the url is actully still the same. You can access it with $_GET['id'] was usual.
I beg to differ. Though most sites uses it (and i admit, numericals are more convenient for refer), a friendly approach is, using the title of the post. But you need to do a lot of extra steps to make it error free (such as, no post can have identical titles, using a dash or underscore between the words for flexibility plus filtering the url should be more strict and becomes complicated in this case). If you want to avoid all these hussle, use ids as they are easier to implement.
What method can you recommended for creating search engine-friendly URLs? When coding in PHP that is. Ideally I would like something like:
http://www.example.com/article/523544
So it doesn't display the file it's opening (eg article.php)
It is quite necessary to generate a SEO friendly URL's so that most of the Search engines can easily index it.And the most interesting part with it that URL can easily correlate to the Page Content and the User can generate a Pretty URL as per the keywords he want to rank the page on different Search Engines(e.g. google.com,google.co.in,bing.com)
The best example to have Pretty Links is on Wordpress.It actually stores the Dynamic Page URL's in the Database itself.And when the Pretty Ur is being called,internally the htaccess is called and it redirects to the original dynamic page in the system.
Some basic tips from
Google
SEOmoz
may help you.
Some topics in SO:
mod_rewrite
nice url
Edit:
You need to place a .htaccess file in your document root that includes the following rules:
RewriteEngine on
RewriteRule ^article/([0-9]+)?$ article.php?id=$1 [L]
Make sure mod_rewrite enabled in Apache and you are allowed to use it.
If you read some questions in SO in this topic it will help you understand how mod_rewrite works.
To make your urls more search engine friendly you may want to use 'slugs' so you need to sanitize your article titles like in this url.
Ideally your URL needs to contain something about the topic of the URL. You gave the example of http://www.example.com/article/523544, where this is better than using standard query strings, it's still not ideal, as all that any search engine can see from it is that it's an article.
It's important to remember that the segment (a segment is the string between each slash) closest to the domain is the most important:
http://www.example.com/most-important/next-important/less-important/
I personally always try to use the following URL structure, and keep my page/article titles unique:
http://www.example.com/this-wonderful-article
Notice the use of dashes and not underscores, this is generally known as the preferred method. Using this method I usually generate and save the article's slug ('this-wonderful-article') in the database, and then search for that instead of an ID.
Appreciated that sometimes it's very difficult to just use slug, especially with a larger website. You may have multiple articles with the same title, or the website may have user-submitted content over which you have no control. If this is the case, you can use the ID without any worries, but just be sure to include the title of the article in the URL. Eg: http://www.example.com/this-wonderful-article/29587
If you're looking for a method of using these URLs then I'd suggest looking at some mod_rewrite tutorials. Personally I use a framework that does the majority of the legwork for me such as CodeIgniter (http://www.codeigniter.com), or you could use something like the Zend Framework or CakePHP. If you're only doing articles then it might be worth looking into a sturdy CMS like WordPress, although this depends largely on your requirements.
I have posted a similar question here. However, this was more about getting advice on what to do. Now that I know what to do, I am looking for a little help on how to do it!
Basically I have a website that is pretty much 100% dynamic. All the website links are generated using PHP and all the pages are made up of php includes/code. I am trying to improve the SEO of the site by improving the URLs (as stated in the other question) and I am struggling a little.
I am using mod_rewrite of rewriting the nice urls to the ugly urls on the server. So what I need is to now convert the ugly urls (which are generated from the php code in the pages) to the nicer urls.
Here are the URLs I need to parse (these are in the other question aswell):
/index.php?m=ModuleType
/index.php?m=ModuleType&categoryID=id
/index.php?m=ModuleType&categoryID=id&productID=id
/index.php?page=PageType
/index.php?page=PageType&detail=yes
Here is what I want the above URLs to be parsed to:
/ModuleType
/ModuleType/CategoryName
/ModuleType/CategoryName/ProductName
/PageType
/PageType/Detail
There is an example on the other question posted by Gumbo however I felt it was a bit messy and unclear on exactly what it was doing.
Could someone help me solve this problem?
Thanks in advance.
I think I see what you're after... You've done all the URL rewriting, but all the links between your pages are using the old URL syntax.
The only way I can see around this is to do some kind of regex search and replace on the links so they use the new syntax. This will be a bit more complicated if all the links are dynamically generated, but hopefully there won't be too much of this to do.
Without seeing how your links are generated at the moment, it's difficult to say how to change the code. I imagine it works something like this though:
<?php echo "<a href='/index.php?m=$ModuleType&categoryID=$id'>"; ?>
So you'd change this to:
<?php echo "<a href='$ModuleType/$id'>"; ?>
Sorry if I've made errors in the syntax, just off the top of my head...
Unless I misunderstood your question, you don't parse the "ugly" URLs, your PHP script is called with them, so you $_GET[] your parameters (m, categoryID, productID) and you combine them to make your nice URLs, which shouldn't be too hard (just a bit of logic to see if one parameter is there and concatenate the strings).
You will need a front controller, which will dispatch the URL to the correct page.
Apache will rewrite the URL using rules in .htaccess, so that anything written will be redirected to index.php?q=. For example, typing http://example.com/i/am/here will result in a call to index.php?q=/i/am/here
Index.php will parse the path from $_GET["q"] and decide what to do. For example, it may include a page, or go to the database, look the path up, get the appropriate content and print it out
If you want a working example of a .htaccess which will do exactly that (redirect to index.php with ?q=path) take a look at how drupal does it:
http://cvs.drupal.org/viewvc.py/drupal/drupal/.htaccess?revision=1.104
As Palantir wrote this is done using mod_rewrite and .htaccess. To get the correct rewrite conditions into your .htaccess you might want to take a look at a Mod Rewrite Generator (e.g. http://www.generateit.net/mod-rewrite/). Makes it a lot easier.