0

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.

2

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried to put this in the .htaccess file but when I do the request it gives me this error:The requested URL /site.com/index.html was not found on this server.
The current code only rewrite requests for files including a-zA-Z0-9. index.html contains a dot, and is therefore not rewritten. I've just updated my answer to rewrite every request, unless the requested resource exists as a file or directory.
2

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.

Comments

0

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

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.