Jump to content

How would YOU make a cleaner URL?


Johns3n

Recommended Posts

Hey all you guys at PHPfreaks.com

 

How would YOU make this URL "cleaner" because I'm currently writing a website (from scratch) and I would really like to improve my SEO, by having the website output much more cleaner URL's?

 

At the moment the website I'm creating is a kind of blog based system, and whenever I link dynamically, I make it write a link with this structure:

 

http://www.sitename.com/post.php?categoryid=22&itemid=33

 

Now lets imagine here for a moment, that link above linked to a article that has the title "Lady Gaga is wonkers" and it has the unique id of "33" (as you can see above) and it is in the category called "Music" and that category has it's own unique id that is "22" (as you can also see in the above link)

 

Now I wan't to clean that link up so that it removes the file name (post.php) and write the category name AND the article name instead of the $_GET queries!

 

So that I might end up with a link that looks like this:

http://www.sitename.com/Music/Lady-Gaga-is-wonkers

Furthermore I wan't the cleaned up link to be "bookmarkable" so the end users, won't have to save the ugly and unclean link.

 

I have been looking into a few solutions, before posting here, but being the COMPLETE NOVICE at PHP, Serverside that I am. I thought that I would see what you pros maybe had of ideas? =) </bootlicking>

 

Whatever your advice/solutions are, remember it has to work with the first mentioned link structure!

 

In advance! Thanks alot :)

Link to comment
https://forums.phpfreaks.com/topic/218428-how-would-you-make-a-cleaner-url/
Share on other sites

put the following in your htacess file

 

.htacess

RewriteEngine On
RewriteRule ^Music/Lady-Gaga-is-wonkers$ /post.php?categoryid=22&itemid=33 [L]

 

 

The original URL:

http://www.sitename.com/post.php?categoryid=22&itemid=33

The rewritten URL:

http://www.sitename.com/Music/Lady-Gaga-is-wonkers

  Quote

put the following in your htacess file

 

.htacess

RewriteEngine On
RewriteRule ^Music/Lady-Gaga-is-wonkers$ /post.php?categoryid=22&itemid=33 [L]

 

 

The original URL:

http://www.sitename.com/post.php?categoryid=22&itemid=33

The rewritten URL:

http://www.sitename.com/Music/Lady-Gaga-is-wonkers

 

Okay, just realized I wasn't clear on a certain thing :( sorry about that, your suggestion would only replace that ONE article right? I need to make it automatically convert my links, to a clean URL with category and article title, and I can't really sit and type each new article into the .htaccess file, so I need it to do something along those lines automatic.

  Quote

Assuming you're using the Apache webserver, this should be done using MOD REWRITE in either the httpd.conf file or the .htaccess file.

 

I'm moving this from the PHP area to the Apache Rewrite area.

 

Ken

 

Preferably I would love if this could be done with PHP.

Pretty URLs are a case of placing links on your site that look pretty using plain HTML anchor links (generated with PHP if you prefer), using a regular expression to recognize patterns and move the information from the Request URL to the query string. Then php must be able to identify that information to extract relative information from your data store (usually database). Mod_rewrite is required to successfully rewrite a URL from for example domain.com/product/cuddly-toy to domain.com/product.php?product=cuddly-toy. At that point it's a normal php script that queries the database for cuddly-toy.

 

most people start out using domain.com/product.php?id=32, if this is what you have it's important to understand mod_rewrite cannot convert cuddly-toy to 32. You will either have to keep 32 in the URL for example domain.com/product/cuddly-toy-32 or query the database for cuddly-toy instead of 32.

  Quote

Pretty URLs are a case of placing links on your site that look pretty using plain HTML anchor links (generated with PHP if you prefer), using a regular expression to recognize patterns and move the information from the Request URL to the query string. Then php must be able to identify that information to extract relative information from your data store (usually database). Mod_rewrite is required to successfully rewrite a URL from for example domain.com/product/cuddly-toy to domain.com/product.php?product=cuddly-toy. At that point it's a normal php script that queries the database for cuddly-toy.

 

most people start out using domain.com/product.php?id=32, if this is what you have it's important to understand mod_rewrite cannot convert cuddly-toy to 32. You will either have to keep 32 in the URL for example domain.com/product/cuddly-toy-32 or query the database for cuddly-toy instead of 32.

 

Thanks for the explanation mate.. So just to verify I got this right! (again novice at serverside) I need a "unique identifier" in the URL?

 

Like lets imagine I was creating a dynamic link:

echo "<a href='post.php?category="$row['cat_title']"-"$row['cat_id']"&"$row['item_name']"="$row['item_id']"' title='Go to "$row['item_name']"'";

 

And then $_GET the URL into a $variable and like explode(); it at "-" so that I get the unique identifier and then run the query?

 

And in the mean time have mod_rewrite remove the filename, "category=" and "itemid="?

 

Is it something along those lines?

In order to help you with this we will need to know,

 

a.) assuming you already have a working site, what the links currently look like.

b.) what you would like a link to look like.

c.) what data is required to fetch information for the page from the database.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.