Forum Moderators: coopster

Message Too Old, No Replies

Using PATH_INFO to make nice URLs

Making it appear as the root page

         

jusdrum

5:27 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



I use $_SERVER['PATH_INFO'] to make pretty URLs all the time, along with setting MultiViews in Apache to allow you to call PHP pages without the .php extension. For instance:

[somesite.com...]

becomes

[somesite.com...]

With some automagic that happens in articles.php.

What I want to do is make it so the URL can be this:

[somesite.com...]

using PATH_INFO in the default page in the root of the site. Is there a way I can do this, without using a hack like using a PHP script for the 404 page?

Nutter

6:09 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



You might try this over in the Apache forum and see about .htaccess rewrites.

Salsa

7:04 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



I do this kind of thing all the time by parsing $_SERVER['REQUEST_URI'] to extract whatever parts I want and make the appropriate queries. I also tend to leave the fake extension off the end of the string, so that some-article-name.html is simply some-article-name. I think that's even more readable, makes the "file" appear to be a directory, and is more permanent in the face of changing technology.

Getting too terse requres a lot of planning, however. For example, if you get rid of your fake /articles/ directory, how will you differentiate the contents of that when you want to include a /news/ section? Also, you're likely to run into problems on the day when some-article-name happens to be the same as some physical directory, like if you have an article about includes, and you also have a directory called includes. The ability to make pretty and friendly urls is great, but if you plan for the site to become very complex I think it's important to assign absolute meaning to some parts of them (like /article/) to allow the rest of them to be more flexible.

coopster

1:19 pm on Jan 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I just wanted to add that when you are using Apache Content Negotiation, you actually may want to get yourself into the habit of using $_SERVER['REQUEST_URI']. Why? Because when you put links like ...

<a href="http://example.com/path/to/mypage">My Page</a>

... on your site, PHP_SELF is going to contain the script name, including extension while the acutally URI requested will not:


PHP_SELF. . . : /path/to/mypage.php
REQUEST_URI . : /path/to/mypage
This knowledge becomes extremely useful at times.