I am close from my completing my first database/PHP driven site. The last thing I am looking to do is to introduce 'pretty URLs' so look smart .com/products/red-widgets instead of .com/products?=red widgets.
However I am confused as to what impact this may have. Do I still create the links as normal and they just appear different or do I need to write the links differently.
Plus apparently Google dislikes query URLs.
Not sure what you mean...you have to use the "fancy" url in the link.
Plus apparently Google dislikes query URLs.
They don't care whether you use query strings or fancy urls makes no difference to google's search engine.
If you are going to go through the pain of learning how to handle "pretty urls" at the server level (using mod_rewrite) it makes sense to propagate the general adoption of these urls by generating internal "pretty url" links on your own site from the start.
Leaving aside the arguments regarding SEO worthiness, pretty urls are also good because:
you can read them out on the phone to people
they look better in print
they convey more meaning
they are not ugly
One of the biggest tricks is working out how to enforce uniqueness though.
Google is not mad keen on query URLs or static pages (and that includes pages created by a template/CMS using essentially static content), but it does want you to use query URLs for genuinely dynamic pages where the content is generated on-the-fly from a backend database in response to the query parameters.
Google has no trouble at all in accessing, indexing and returning query URLs, but using query URLs for a static site, or vice versa, can have an adverse impact on how it understands your site. That is particularly true when you have multiple variables.
eg if you have
Google knows that those three variables are independent and that the order is irrelevant (assuming it gets a 200 A-OK). So when it sees a link to
it knows that that is the same page that it has already got.
But if you have
or even just
it isn't obvious at all that those are query parameters (and in the first case variable names) that can be re-ordered or possibly removed. That means you're more likely to run into duplicate content issues and/or find that Google doesn't return the most relevant page from your site.
A good way to combine the two features, if you do have genuine dynamic pages, is to have pretty URLs that visibly redirect to the real query URL. That way search engines know to use the query format and so can understand the structure better, but you're giving real people an easier route into the site.
Sounds much more complicated than I thought I would it be.
I thought I just changed the HT Access file so when a I created products.php?=redwidget it automically rewrote it as /products/redwidget.
What is the best way to do prettry URLS. Do I have to change all the links on my site.
I am just at the stage of completing my site so I need to get it right!
I found this.. http://www.nouveller.com/quick-tips/quick-tip-6-how-to-write-clever-pretty-urls-with-htaccess/
Is this a good way to do it?
Editing the .htaccess file doesn't change any of the links on your pages; it only lets the computer understand them. Essentially, it "translates" the pretty links as the request comes in (when someone clicks the link) behind the scenes.
The point is to give the end user a nice readable url. What you do on your server end is to have the server understand which file /products/redwidget is supposed to fetch. This can be done as shown in that post you linked to.
@Stevie D -- nice reply, thanks for taking the time to make that, very informative.
@justlukeyou -- getting pretty urls from your database might lead you to turning a heading or a product title into something which is sometimes termed a "slug".
id , title, slug
23 | Red Widgets | red-widgets
Use advanced search constrained to this just this forum for the term slug to turn up some old discussions on this.
Now im confused, if a database full of products what do I need to do it to introduce pretty URLs
a) a .htaccess rule which rewrites urls from /products/red-widget to products.php?item=red-widget
b) a script called products.php which searches your database for "... from products where slug='red-widget'"
if you do not go down the route of maintaining a unique slug that script uses the equivalent of:
"from products where title = 'Red Widget'"
which you can do using the likes of: "from products where title = '". str_replace("-", " ", ucwords($item)) ."'";
For the sake of brevity the sql escaping is omitted, make sure you add it.
The other way to achieve somewhat pretty urls is to keep the id number in the url as discussed a few days ago here.
As I am querying key words I cant use ID numbers. Option A sounds the easiest and most effective option.
Is it possible to remove the "?=" and just replace it with a slash.
Once I have sorted the links my site will be complete, frustrating to be stuck on this tbh lol
One of us is confused here.
Using Apache's mod_rewrite takes a pretty url /products/red-widget and rewrites it that so behind the scenes this url is in fact fetched products.php?item=red-widget
So, in reference my previous reply you do a) and then you go on and do b) in one fashion or another.
I have added this to my htaccess file
However when I use /products/roomproductscategorised.php/bedroom instead of /products/roomproductscategorised.php?room=bedroom it doesn't work. However /products/roomproductscategorised.php?room=bedroom does work.
RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]
Im confused as to what rewrite code I use to make this work.
Does anyone have any advise what I can do. I have managed to remove the .php and of links but I still cant create a link without a query in it.
I cant progress with my site until this is sorted
OK, thread moved to the Apache forum, where you will get more help with this issue from the likes of @dklynn
; and @ScallioXTX
First, take a look at the menu items and links at http://wilderness-wally.com. Those links are the TITLE fields in a database which are unique, just like your digital ID numbers, but provide a "pretty" look as a URI. They are all served by a single file so the advice above is quite correct: YOU create the NEW FORMAT links in the manner you wish then YOU create the mod_rewrite code to convert that format to something Apache can serve. Wally's Home Page is titled Welcome so the link is http://wilderness-wally.com/Welcome. mod_rewrite converts that title to a value in a query string and passes that to the handler file (PHP) which fetches the content from the database.
[indent]WARNING: You'll need to use non-reserved characters in the title - PM for the list I use at Wally's site.
NOTE: I convert spaces to _'s before using the title as a link then convert back to spaces before querying the database.[/indent]
If you're take a look at the mod_rewrite tutorial linked in my signature, that should help you considerably. The bit about not using id numbers is rather buried in the immense amount of information but you'll be an expert after you read through it 3-4 times. Don't worry, though, there are plenty of coding examples!
This topic is now closed. New replies are no longer allowed.