2015-02-11 311 views

回答

0

有可能通过PHP:

if (isset($_GET['q'])) { 
    header ('Location: http://domain/search/' . $_GET['q']); 
    exit; 
} 

当然,你需要在你的.htaccess重写这个URL,以避免404 Not Found错误。

1

您需要手动处理表单提交。为此,您将绑定到onsubmit表单事件,阻止其默认行为并将其重定向到正确的URL。类似于你的情况:

document.querySelector('form').addEventListener('submit', function(e) { 
    e.preventDefault(); 
    location.href = '/search/' + encodeURIComponent(this.elements.q.value); 
}, false); 
1

你在找什么是RewriteRule。这是您的网页目录的.htaccess文件中指定的规则,它指定了服务器应该执行的特殊操作。使用这个,我们可以把你的查询字符串,映射到一个用户友好的URL,并使用标准的index.php文件,或任何你选择。

的.htaccess

# Match a condition: 
# http://example.com/search/?query=find+something 
RewriteCond %{QUERY_STRING} query=([^&]+) 

# Redirect that condition to 
# http://example.com/search/find+something 
RewriteRule ^search/$ search/%1 [L,R=301,NC] 

# Match the previous rule, and serve results from our search page 
RewriteRule ^search/(.*)$ search.php?q=$1 [L,NC] 

所有其他答复将做你问什么,并更改URL,但是他们没有将其映射新格式化的URL到一个页面,这将有助于随访结果。

+0

这是正确的,但OP需要'点击提交按钮后有url http:// domain/search/submit_text_here'。模式重写不会使浏览器向这样的URL发送GET请求,只要它们以这种形式出现,它就会正确处理它们。 – dfsq 2015-02-11 07:44:01

+0

使用'http:// example.com/search /'的表单操作和'get'方法会导致此重写正常工作。 - >'http://example.com/search/?query = find + something' < - 匹配第一个条件。 – 2015-02-11 07:45:51

+1

是的,我知道。我在说模式重写不会使浏览器首先将其提交给“http:// example.com/search/find + submiting”。如果有这样的请求,模式重写将正确处理它,但以这种形式发送请求是客户端工作。 – dfsq 2015-02-11 07:58:12