Jump to content

PHP Get From Mod Rewrite


ojsimon

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/127121-php-get-from-mod-rewrite/
Share on other sites

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.

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

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".

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

 

 

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.

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']}");
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.