Developer Vidit
Developer Vidit

Reputation: 70

Changing your url to do something else

I don't know how to explain this, but I will try my best to explain it.

I am developing my website in php. So I have created an index.php file in Search folder

So if I have to search any word like 'good', it's address is

http://127.0.0.1/Website/search/?search=dh

Now if I try to access

http://127.0.0.1/Website/search/

I get an error on my page

Undefined index: search

because it is unable to find my $_GET["search"] variable which I am using to get the value of the input type.

Is there any way by which I can make this url

http://127.0.0.1/Website/search/something

be interpreted as

http://127.0.0.1/Website/search/?search=something

Upvotes: 0

Views: 61

Answers (4)

webNeat
webNeat

Reputation: 2828

For first problem: use isset() to check if $_GET['search'] is defined before using it.

For Second question: use a .htaccess file with the rule

RewriteRule ^/search/(.+)$ /search/?search=$1 [L,R] 

Upvotes: 2

lukaszwierz
lukaszwierz

Reputation: 21

You should use rewrite rules in apache: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html Than in Your script drop part of code reading "search" param and read $_SERVER['REQUEST_URI'] , parse it to get through the "seach" part in URL and read whats after it.

Upvotes: 0

Jan-Willem de Boer
Jan-Willem de Boer

Reputation: 797

You're getting the:

Undefined index: search 

error because you don't check if the variable exists.

To do this you can do a simple if statement like this one down below:

if(isset($_GET['search'])) {
    // search index exists
}

For changing your URL you need to use URL rewriting, here is a good article about it: http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

Upvotes: 1

Clément Malet
Clément Malet

Reputation: 5090

Undefined index: search

"Because it is unable to find my $_GET["search"]" : Then check if the variable exists :

if (isset($_GET['search'])) {
    // use $_GET['search'];
}

For your second question, you can have an URL like "mysite.com/Website/search/something" using URL rewriting with htaccess. Search these terms on SO or Google and you will find a lot of good tutorials.

Upvotes: 2

Related Questions