重定向到不同的URL与.htacess或PHP [英] redirect to different URL with .htacess or PHP
问题描述
我在index.php文件的形式,接受用户输入,它包括,并将用户输入进行处理另一个PHP文件。
I have a form at index.php that takes in user input, it the includes and sends the user input to another php file for processing.
下面是index.php文件的code:
Here is the code of index.php:
<?php
if(isset($_GET['q'])){
include_once "form.php";
exit(0);
}
?>
<!Doctype HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Search</title>
</head>
<body>
<form method="get">
<input type="text" name="q" />
</form>
</body>
</html>
当提交表单时,通过按下回车键时,文本输入的重点是,它关系到
When one submits the form, by hitting enter when the text-input is focused, it goes to
http://mysite.com/?q=textUserEntered
(如果只是域名被访问过)
要么
http://mysite.com/index.php?q=textUserEntered
(如果的index.php之前访问过)
http://mysite.com/?q=textUserEntered
(if just the domain was visited before)
or
http://mysite.com/index.php?q=textUserEntered
(if index.php is visited before)
我怎样才能得到它去 http://mysite.com/form?q=textUserEntered
或的http:// mysite的。 COM /的index.php /形成的?Q = textUserEntered
,同时还传递表单数据form.php的
How can I get it to go to http://mysite.com/form?q=textUserEntered
or http://mysite.com/index.php/form?q=textUserEntered
while still passing the form data to form.php
我会preFER用PHP的解决方案,但如果要做到这一点的唯一方法是用.htacess的话,请分享一下。
I would prefer a solution with PHP, but if the only way to do this would be with .htacess, then please share.
我在开始的index.php和form.php的尝试这样做,它导航到该URL,但不传递到form.php的数据并导航到404错误页面。
I tried this in the beginning index.php and form.php, it navigates to the URL but doesn't pass the data to form.php and navigates to the 404 error page.
if(!empty($_GET['q']))
{
header("Location: form?q=".rawurlencode($_GET['q']));
exit;
}
我不能使用该操作属性,因为加入form.php的action属性的值将使URL http://mysite.com/form.php?q=userEnteredText
不是 http://mysite.com/form?q=userEnteredText
推荐答案
只要告诉形式,其中发送数据,则不需要重定向:
Just tell the form where to send the data, you don't need to redirect:
<form method="get" action="form.php">
这篇关于重定向到不同的URL与.htacess或PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!