Welcome to   
"The Blog"

Monday, June 02, 2008

Posted in PHP

Friendly URL Tutorial 01

I finally got my URL's to display the right way and I pray I never have to go through this process again.  For those of you that are new to PHP and creating friendly URL's I will say this:

Make sure you know how you want your URL's to display way before hand. 

Changing my entire site to the “new” links was such a pain. Buy hey, no pain no gain right? Just agree!

Any way, with all the crap that I went through trying to figure this out, I can honestly say that posting on forums was a waste of time.  I posted my questions, etc, etc and not a single damn person helped out.  It was only through countless searches and trial and error that I figure this out, and honestly, I don't even know if it's done right, BUT the main point is that it works dammit!  Also, a lot of these tutorials that I found did not work at all.  Many of them are outdated or just written like you know what you're doing. 

Well, if we knew what we were doing, WE WOULDN'T NEED A DAMN TUTORIAL!!!

Sorry, I'm a little on edge writing this so please bare with me.

I am going to share what I did, what I somewhat understand, and how I did it so pay close attention to what I am going to write. 

I created the file with 3 directories / tiers / paths, whatever you want to call them.  You'll understand the importance of this as we move on.

Disclaimer:  If this doesn't work for you, sorry but don't ask me what to do because I won't know.

Step 1

I figured out about friendly URL writing and redirecting just by typing it in google.  You need access to your .htaccess file and if you don't have one, you need to make one.  This should sit in the root of your site.

Note:  From what I understand this is only for Apache Servers so if you're on a windows server, go away.

You can edit this file in any text editor you have, I prefer Dreamweaver, just add .htaccess to your extensions list and it should open.

Step 2

Once you have the file opened, type in this:

Options +FollowSymLinks
RewriteEngine On

I'd love to explain that to you but all I really know is that this will turn on your mod rewrite and nothing will work if you don't have this, so just type or paste it in.

Note:  The .htaccess file will also let you point to a custom 404 page.  In order to do this just type "ErrorDocument 404 /whateveryourfileiscalled.zzz" without the quotations at the top of the doc.  Everything else will go underneath.

Step 3

When editing and rewriting the domains you will also need a basic understanding of "regular expressions".  What's that?  Let's move on and you'll get a clearer picture.

Underneath what you just pasted in the same document, type:

RewriteRule ^/?directory/(.*)/(.*)/$  / directory /$1/$2 [R=301,NC,L] #removes the slash, redirect
RewriteRule ^/? directory /(.*)/(.*)$  file.php?something =$1&something else=$2 [NC,L] #rewrites the old url
RewriteRule ^/? directory /([a-zA-Z0-9\-]+)$  /directory /$1/ [R=301,NC,L]  #adds the slash to the category, one level, redirect

Wow!  If you've never seen this before, that's what you should be thinking.  This is a mess of crap if you don't understand it, but I'm going to break down as much as I can.

The Breakdown

"RewriteRule" – You need this before every rule you write in the .htaccess file.  It tells the server that this is a rule.

"^" – The carot tells the server where to start the rule.  So this will tell the server to start the rule when it gets to the directory.

"/?" – Some people will say you need this, some will say you don't.  This was on the sitepoint website as a default for your rule to work on all the different versions of apache.  Feel free to mess around with this but I'd keep it in.

"directory" – This is a little confusing.  You can pretty much make this whatever you want but I would keep it the name of the PHP file that your changing.  So if your file is directory.php, name it directory.

"/(.*)/(.*)/" – The slashes are just other directories.  The (.*) is part of the “regular expressions”.  It's kind of like a wild card that says you can pretty much type anything.  I had some issues with this on certain occasions where the URL was getting messed up with a slash or some other character. If this doesn't work, use the other expressions I have listed below.

"$" – This means to end the rewrite rule.

After you end it, now you need to write the URL that needs to be rewritten.

"file.php?something=$1&somethingelse=$2 [NC,L]" – Just replace this line with whatever file you're changing.  If you have more than 2 variables then you need to add more to the actual rule.  The variables are always represented with the dollar sign.  The NC says to ignore the case, whether its capital or lower, and the L tells the server to stop looking for this pattern once it found it.  The R=301 means it is a permanent 301 redirect.

This rule is telling your browser to recognize this address:

domain.com/file.php?something=45&somethingelse=chicken

as this address:

domain.com/file/45/chicken

You can type both of these in now and get the same page.

"([a-zA-Z0-9\-]+)" – This is also "regular expression" but it's a bit more specific. If the (.*) isn't working, use this instead.   It just says that letters, lowercase or capital, and numbers can be displayed.  The \- is needed if you have dashes in your rewrite.  If you have underscores, than you can use \_, or pretty much \ and a symbol that is in your URL. The plus sign at the end says that you can have more than one character.

The rest of the rules can be explained with the comments that are next to them.

For the rest of this post, I'm just going to write the code so you can copy it if you'd like.  Just tweak it so it fits what you need. 

The reason why I'm just going to paste the code here is not because I'm lazy (maybe a little) but mainly because I can't really explain the rest of it.  It pretty much follows suit with what's above but has a few additions that I copied from other places in order to get the crap to work.

Here's the code in its complete form:

Options +FollowSymLinks
RewriteEngine On
#The following is the redirect
RewriteRule ^/?directory/(.*)/(.*)/$  /directory/$1/$2 [R=301,NC,L] #removes the slash, redirect
RewriteRule ^/?directory/(.*)/(.*)$  file.php?Something=$1&somethingelse=$2 [NC,L] #rewrites the old url
RewriteRule ^/?directory/([a-zA-Z0-9\-]+)$  /directory/$1/ [R=301,NC,L]  #adds the slash to the category, one level, redirect

RewriteRule ^/?directory/([0-9]+)/$  file.php?something=$1 [NC,L]
RewriteRule ^/?directory/([0-9]+)$ /file/$1/ [R=301,NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /file\.php\?something=([^&]+)\ HTTP/
RewriteRule ^file\.php$ /file/%1/? [R,L]  #These 2 lines remove the .php extension.

RewriteRule ^/?([a-zA-Z0-9\-]+)$  /$1/ [R=301,NC,L]  #adds the slash to the page,redirect
RewriteRule ^/?(.*)/$ $1.php [NC,L]

#Apache lets dynamic files accept slashes at the end
#redirect dynamic file endings to their "directory" equivalent
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)\.php?$ /$1/ [R=301,L]

Ok, here are a few things you need to know:

  • Order Matters! I can't stress this enough, if you have more directories, put the line of code at the top and then go in order from most to least.  THIS MATTERS.  If something in the code is not working, most likely you have it in the wrong order.  Mess around with that and see what works.
  • After RewriteCond any related variables to that condition must contain a % instead of a dollar sign.  You'll notice that in an example above.
  • If the (.*) isn't working, try the a-z expression.
  • Keep in mind that when you do a rewrite, IT IS NOT A REDIRECT.  Meaning, after a rewrite you will still be able to view the page at the old URL unless you redirect it to the new one.  file.php?id=5 will still work as well as /file/5 and they will be the same page.  This will cause duplicate content on search engines unless you do the 301. Some of my redirects I did in the actual PHP code.  As I continue my tutorials for the blog / news app, I'll go over them.
  • If you confused on any of this, good luck, HA j/k.  Seriously though, I figured all this out in like a week, so do your research.  There are plenty of sites that will give you a gist of what you need to know, you'll just have to piece it together.

This is a lot of code and I probably have some things that I don't need in here, but it does work.  If you are familiar with this stuff and know simpler ways of doing it that actually function, let me know.  Share your knowledge!

Here are some useful links, I'm surprised I'm actually doing this, but you will need some help:

Update: I ran into some issues with the backend using this code because of the actual directories.  This entire code works but if you try to access i.e. yourdomain.com/images/  It may not show up because it will think that the directory images is actually images.php.  I have the work around that I will post tomorrow.  I hate this .htaccess sh*t.

 


 

What do you think of "Friendly URL Tutorial 01"?

There is currently 1 comment.


Enter Your Nickname

Enter A Comment

What is two plus two?

 

Comments:

July
29
Tuesday

No John Reese, I will be deleting your comment. STOP SPAMMING AND TRYING TO GET FREE LINKS WITH MY SITE!!! None of your links are even displayed hahaha. Attention people, NO SPAMMERS, email me if you want to exchange links!!!

Posted by WebMaster at 10:40 am

Blog Categories

Rants (3)

Personal (3)

Internet Marketing (6)

PHP (3)


Latest Blogs

» I'm Moving!
» A Designers Nightmare
» My lack of updates
» Awesome Job Op (HA)
» Friendly URL Tutorial 01

My ONLINE Portfolio

SureFire Web Services Baby Tutors ZoAir ATS JS
American Container Concepts