Easy Seo Friendly Url's Without Apache And .htaccess

Mar 27th, 2009 by Mattinblack

Many people want to implement SEO friendly URL's but are hamstrung because of their inability to access .htaccess files. Within 20 minutes you can have SEO friendly URL's on your site so long as the site can use PHP scripts...

Many people want to implement SEO friendly URL's but are hamstrung because of their inability to access .htaccess files. This may be because of the lack of such a facility due to not being on an appropriate server, or more likely that the host does not trust his customers to configure their directories properly.

Within 20 minutes you can have SEO friendly URL's on your site so long as the site can use PHP scripts. There is a solution, which I present here, using PHP. I have a script you can download for free or you can just type it in from this listing. Here it is:
php1.gif

You only need to change two lines to get it to work for you but I will also explain how the script works below, then give you the download link.

You simply modify the script to your requirements and save it as (e.g. index1.php) in the root directory of your server.
Line 2 you need the url in the quotes to be the path to wherever the php files are that you wish the script to address. For example if your site was http://www.mongo.com and you had a php blog in a directory called blog2 you would make the url http://www.mongo.com/blog2/ dont forget the trailing slash or the quotes around it or the semi-colon at the end of the line.

Line 3This setting restricts the script so it will only operate on internal links from within your website. To use this option it should be the basic web address (no http://) of your site. To follow our example http://www.mongo.com with no trailing slash. If on the other hand you wish to have the script operate from anywhere then simply put a single full stop between the quotes.

Thats it! It will work perfectly. Here is how you construct the URL's to link to it. I will carry on using our mythical 'Mongo' blog site to demonstrate. Let us say your blog has a php file called 'showpage.php' and it takes two parameters 'section' and 'page-no'. Normally your SEO unfriendly URL that you have been using would be:

http://www.mongo.com/blog2/showpage.php?section=mongo-world&page-no=22

Search engines hate the ? and & bits called a query string. Your new url would be simply:

http://www.mongo.com/index1.php/showpage/section/mongo-world/page/22

Yes there is a dot php in the middle but the search engines dont mind that at all.

So the first slash is the name of the php file, the next is the name of the first parameter, then its value, and so on for as many parameters as you like. In fact you can even add parameters to put keywords in the url. For example:

http://www.mongo.com/index1.php/showpage/section/mongo-world/page/22/gorilla-butts-seen-in-fiji Would be no problem. It is this keyword addition aspect that makes these URL's great for SEO.

Another way of using this is simply to put all the html you normally have in your index file at the end of the script (after the '?>') and save it as index.php replacing your index file. You have to remember if you want to use the url friendly scripts that you must still put index.php in the SEO friendly url though!

Now I promised I would run through the workings of the program with you. Here we have a line by line account. Lines 4 and 27 are comments. Line 5 takes the output from server vars, which contains everything after the '.php' part of the link addressing the current page, and uses '/' as a seperator, placing it in an array we have called sef_input. So in our example above sef_input would contain in place one 'showpage', in place 2 'section', in place 3 'mongo-world' and so on. Place zero is nearly always blank and you dont need to worry about the content if it is not.

Line 6 gets the URL of the page containing the link which was clicked on to get here, so if the link was clicked on page http://www.mongo.com/sitelist.htm then that is what the sef_refer variable would contain after this line. Line 6 is prepending '://' to the sef_mysite variable you edited at the start. this is simply to stop people cheating the security system by making a URL like:

http://www.mongo.com/index1.php/showpage/section/mongo-world/page/22/www.hackersite.com

and thus being able to place links on the site 'www.hackersite.com' that would work.

Line 8 checks that the incoming link is from within your site before allowing the script to proceed, and lines 9 and 10 check that sef_input actually has some parameters and is not for example from a link like:

http://www.mongo.com/index1.php?action=ADD&item=book22

This allows you to put other useful code inside the same script file if you like, beneath the comment in line 27, or simply to let the script 'drop through' and display whatever html code you put in beneath line 30 if you link to the script with no slashed parameters.

Line 11 sets the program name from the first parameter, adds dot php? to the end of it to make the first part of a query string. Line 12 sets up a variable we use to decide if the parameter being read from sef_input is the name of a parameter or its value. Line 13 sets up a loop that will continue until it reaches the end of sef_input but starts at place 2 (place 1 was the name of the program).

Lines 15 and 16 only execute if toggle is zero. They apend the name of a variable to the program name, and then set toggle to 1 ready for the next input (which must be the variable value).

Lines 18 and 19 only execute if toggle is 1, they append and equals sign, the variable value, and an ampersand ready for the next variable name, and set toggle to zero ready for the next input which must be a variable name.

Line 22 prepends 'Location: ' to the program name (required for redirection), Line23 is the actual line that redirects to the program file. If you want to test the script comment this line out by putting a hash (#) in front of it, then the script will drop through to the echo statement on line 26 which will display the URL that the script would have redirected to.

WARNING: If your target php or cgi uses a path like file=products/dental/adhesives.html as a variable then this script will not work unmodified since the explode command will explode the embedded path as well. I have a simple solution so contact me to get it!

NOTE: if you are using the script in its 'drop through' mode to display html then comment out or remove line 26.

Thats it! If you want to save yourself some typing go to this Scripts Site to grab it for free.

Mattinblack

Written by Mattinblack

Rate this Article:

Rating: 3.4/5 (5 votes cast)

Add new comment

* You must be logged in order to leave comments, please Sign in or join us.

Comments

Mattinblack, over a year ago
Report comment

Without an example of how you are calling it, what you have set the variables to, and what you get as the echo result of line 26 I cannot even start to sort it out!

ihilliard, over a year ago
Report comment

I would really like to get this working as its a nice idea.
I’ve tried this on my server and localhost but I can’t get it working and I dont know why.
If I un-edit the last echo comment I get the REF> to my refer link. So that part is Ok.
The problem I am having is that it never does the redirect, regardless of what I do it always shows me the Oops page.
I have read the aticle over-and-over everything is correct as far as I can see. Any ideas? pointers?