ojsimon Posted October 5, 2008 Share Posted October 5, 2008 Hi I am trying to make a simple get form that asks the user to enter their name and then it returns their name on the next page, i have managed to do this but now i would like to have nice clean SEO Urls insted of the ugly get urls. How do i make do this? I am currently doing: Index.php <html> <form action="welcome.php" method="get"> Name: <input type="text" name="name" /> <input type="submit" /> </form> </html> and welcome.php <html> Welcome <?php echo $_GET["name"]; ?>.<br /> </html> So my question is how to make this have clean urls such as domain.com/'search-phrase'? Thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 5, 2008 Share Posted October 5, 2008 With Apache's mod_rewrite module. Search Google for tutorials. Quote Link to comment Share on other sites More sharing options...
ojsimon Posted October 5, 2008 Author Share Posted October 5, 2008 yes i know that but how would you use it in this situation? Thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 5, 2008 Share Posted October 5, 2008 Ah. When submitting a form using the GET method, you will always be sent to "/page.php?name=value" as far as I know. And I'm not sure we can redirect that to e.g. "/value" (tried an example - didn't work). Quote Link to comment Share on other sites More sharing options...
ojsimon Posted October 5, 2008 Author Share Posted October 5, 2008 What method for a form can i use to get these clean urls? Thanks Quote Link to comment Share on other sites More sharing options...
R0bb0b Posted October 5, 2008 Share Posted October 5, 2008 POST or reformat with js Quote Link to comment Share on other sites More sharing options...
ojsimon Posted October 5, 2008 Author Share Posted October 5, 2008 yeh but how would you then get the search term after a forward slash like domain.com/'search-term' ? Thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 5, 2008 Share Posted October 5, 2008 But what you could do, is that welcome.php redirects to /value: <?php header("Location: {$_GET['name']}"); ?> And then let e.g. welcome1.php handle the outputting: <html> Welcome <?php echo $_GET["name"]; ?>.<br /> </html> Using this .htaccess file: RewriteEngine on RewriteRule ^(.+)$ welcome1.php?name=$1 Not sure if it'll work though, but give it a try. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 5, 2008 Share Posted October 5, 2008 The .htaccess file example above will redirect any typed request to "welcome1.php?name={request}", but without showing it. So "domain.com/test" would output the contents of "welcome1.php?name=test". Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 5, 2008 Share Posted October 5, 2008 Another thing, if you're going to use this on a live server, sanitize ANY user input before printing it on a page. Never echo any raw GET or POST data, and never use it in a header() like I did Quote Link to comment Share on other sites More sharing options...
ojsimon Posted October 5, 2008 Author Share Posted October 5, 2008 Sorry that kind of works just a few things? 1) what do u mean by sanitize? 2) at the moment it is just returning a blank page for some reason? 3) when you enter the term it does not redirect to domain.com/search term it still uses /welcome.php?name=search term but the /search term still works, how do i get the form to redirect to the domain.com/ search term? thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 5, 2008 Share Posted October 5, 2008 1) Clean up/filter the input. To make sure the code can't be maliciously misused. E.g. before outputting user input to the browser, run the data through htmlentities(), to make sure any HTML code won't be parsed, but just displayed. And you wouldn't want to redirect (using header()) to an unsanitized, user specified URL (it could be malicious). 2) and 3) If you use the setup I described, it should work. Note the "1" I used in "welcome1.php". Quote Link to comment Share on other sites More sharing options...
ojsimon Posted October 5, 2008 Author Share Posted October 5, 2008 This is what i am running and generating the problems i described earlieer: index.html <html> <form action="welcome.php" method="get"> Name: <input type="text" name="name" /> <input type="submit" /> </form> </html> welcome.php <html> <?php header("Location: {$_GET['name']}"); ?> Welcome <?php echo $_GET["name"]; ?> </html> welcome1.php <html> Welcome <?php echo $_GET["name"]; ?> </html> and i added the .htaccess file you said? What have i done wrong? Thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 5, 2008 Share Posted October 5, 2008 welcome.php should look like this: <?php header("Location: {$_GET['name']}"); ?> You can't have output before a header() call (only with output buffers turned on). If you aren't getting any errors, it must be because you've got output buffers turned on by default. Quote Link to comment Share on other sites More sharing options...
ojsimon Posted October 5, 2008 Author Share Posted October 5, 2008 Its still not working, both the problems stil remain? Thanks Quote Link to comment Share on other sites More sharing options...
thebadbad Posted October 5, 2008 Share Posted October 5, 2008 When you hit submit on the form, isn't it redirecting to "domain.com/value"? Maybe you should add the domain in the code: welcome.php <?php header("Location: http://domain.com/{$_GET['name']}"); ?> or just a forward slash: <?php header("Location: /{$_GET['name']}"); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.