Zakukashi
Zakukashi

Reputation: 586

Some header passing variable?

Im trying to pass some variables from the URL to the PHP script. Now I know that www.site.com/index.php?link=HELLO would require $_GET['link'] to get the variable "link". I was hoping there are other ways to do this without the variable.

For instance I want a link structure like this: www.site.com/HELLO. In this example I know that I have to create a Directory called Hello place an index file and it should work but I don't want to create a directory and Im hoping there's a way to "catch" that extra part after the domain. I'm thinking of creating a custom HTTP 404 Page that will somehow get the variable of the not found page, but I don't know how to get the HTTP 404 error parameters. Is there another simpler way to get a variable without the use of the extra ?link= part? I just want it to be a structure like this www.site.com/HELLO.

Upvotes: 0

Views: 112

Answers (4)

kba
kba

Reputation: 19466

What you want is URL rewriting. You don't mention what kind of web server you're using, so I'll assume it's Apache.

If you have mod_rewrite enabled on your web server, this can easily be accomplished by creating a .htaccess file in your document root with the following

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1

This will make all requests - that match the regular expression [a-zA-Z0-9]+ - get forwarded to index.php. For instance, if you try to access domain.com/hello, PHP would interpret this as trying to access index.php?request=hello.

You can read more about this in the Apache HTTP Server manual about mod_rewrite.

Upvotes: 2

Christopher Johnson
Christopher Johnson

Reputation: 2629

you can do that with a mod-rewrite structure.

Here's a pretty good tutorial on how to set it up with php/apache.

http://www.sitepoint.com/guide-url-rewriting/

Upvotes: 0

Madara's Ghost
Madara's Ghost

Reputation: 174967

In which case, you normally use .htaccess to alter the URL in some form. For instance:

RewriteEngine On                             #Enable Rewrite
RewriteCond %{REQUEST_FILENAME} !-f          #If requested is not an existing file
RewriteCond %{REQUEST_FILENAME} !-d          #If requested is not an existing directory
RewriteRule ^(.*)$ /index.php?link=$1 [L]    #Preform Rewrite

Which will make www.site.com/hello to www.site.com/index.php?link=hello. This change is invisible to the user (he will still see www.site.com/hello as an address). Be advised that it may cause trouble if you try using relative paths with CSS/JavaScript files.

Upvotes: 2

kitti
kitti

Reputation: 14814

You need to use URL rewriting to do this. If you're using Apache: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Upvotes: 0

Related Questions