View Full Version : Resolved Changing site.com/test.php --> site.com/test

03-27-2009, 08:30 PM
So I'd like to change urls like www.site.com/test.php to be www.site.com/test/

I'm sure this is .htaccess related, so could someone please explain? I tried googling, but I don't know what to really call this.

Any help is appreciated. :)

03-27-2009, 09:16 PM
That's right, you can use htaccess to make it work: http://newmediarts.blogspot.com/2007/01/hide-file-extensions-in-urls-with.html :)

03-27-2009, 09:25 PM
Works great! Thanks!

But how would I go about having extended urls such as:



03-27-2009, 09:33 PM
I simply use folders to achieve this. I think its called search engine friendly links or removing file extension in url :)
The thing about .htaccess, it deletes all your file extensions including images and other things and you don't want that. Search engines likes .jpg / .png /.gif and other helpful extensions as far as I know, and they don't like things like .html / .php / .aspx. So I simply use folders. Every page has its own folder. (good organization training btw :) )

For example:
I want to rename my /contact.php form to /contact
What I need is /contact folder and than I put contact.php in it. There are one more thing to it, /contact.php has to be remained to /index.php, this way ftp will read it as main folder file.

03-28-2009, 06:33 AM
I'd rather not have to make multiple folders because I'm running a .tpl system.

I'd like to be able to get a url like this:


03-28-2009, 09:23 PM
The suggested solution is a very poor way of accomplishing the functionality you'd like.

You'll want to look into using regular expressions in your rewrite rules.

RewriteEngine On

RewriteRule ^(\w+)/+page([0-9+])/*$ /inc/$1.php?page=$2 [L]
RewriteRule ^(\w+)/*$ /inc/$1.php?page=1 [L]

ErrorDocument 404 /inc/error.php

# Examples:
# http://www.site.com/whatever/page3 -> http://www.site.com/inc/whatever.php?page=3
# http://www.site.com/whatever -> http://www.site.com/inc/whatever.php?page=1

03-28-2009, 10:57 PM
100% agreed with kbluhm, mod_rewrite is a very effective means of making clean URLs while not sacrificing functionality.

Googled (http://www.google.com/search?q=mod_rewrite&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a)

I custom built a CMS I use on the sites I design that is based around this concept. It allows me to run everything through index.php (much more efficient for things like global includes, session management, page management, etc.) and output it under seemingly different sections.

For example in my case I've got:

In the address bar, while mod_rewrite is actually turning that into


Script handles it, user is happy. All good times :)

03-28-2009, 11:54 PM
Let Me Google That For You :D (http://www.lmgtfy.com/?q=mod_rewrite)

the methods above are the best and they are the correct ways to use mod_rewrite :)

03-29-2009, 02:41 AM
Those look as if they turn




How do I do it the other way around?

03-29-2009, 03:27 AM
Those look as if they turn




How do I do it the other way around?

Well it does both. See what happens is this:

The Server (aka Apache) doesn't understand http://mydomain.com/page/about-us/27. To that it will understand it as folders and sub-directories.

So, in order for Apache to understand it, the mod_rewrite transforms that into the long string listed above according to your re-write rules.

So essentially what you're doing is all the links the user sees are the nice, clean ones. The only time the long index.php?p=about...etc. links appear are internally to apache - never seen by the user.

To achieve this, you use mod_rewrite. For example lets say I've got this architecture:


I don't want the user to see that mess, so I use mod_rewrite.

I want users to type it in this format: http://mydomain.com/page/<page-name>/<article-number>. So lets tell apache that's what we want:

RewriteEngine On
RewriteBase /

RewriteRule ^page/(.*)/(.*)$ index.php?p=$1&articleNumber=$2
(.*) - This is REGEX (http://www.zytrax.com/tech/web/regex.htm) essentially meaning that it accepts any character into that spot within the link
$ - In the inital statement this signals the end of the REGEX expression
$1... $2... $n - This signals the regex expression you are relating to. So the first instance of (.*) is $1, the second instance is $2, and so on.

That's a quick run-over of it for you. If you are interested in learning it, I highlighy recommend you check out this tutorial (http://www.easymodrewrite.com/). I used that one to start teaching myself mod_rewrite and it is very helpful.

03-29-2009, 08:14 AM
Thanks so much for the help. :]