【问题标题】:Generating clean URLs when using forms使用表单时生成干净的 URL
【发布时间】:2012-07-15 03:12:12
【问题描述】:

这是表格:

<form action='<?php echo $_SERVER['PHP_SELF'];?>'>
  <p><label>Movie Title:</label><input type='text' name='search'></p>
  <p><input id="submit" type='submit' value='Submit'></p>
</form>

当表单提交时,当前 URL 返回如下:

localhost/movie/index.php?search=ted

我希望 URL 像这样返回:

localhost/movie/search/ted

编辑:

我现在的 .htaccess 中有以下代码:

RewriteCond %{QUERY_STRING} ^search=(.*)$ [NC]
RewriteRule ^$ /search/%1 [NC,R,L] 
RewriteRule ^search/(.+)$ index.php?search=$1 [NC,L]

这在您输入 URL /movie/search/ted 时有效,但当您提交表单时,它仍然显示为 /movie/index.php?search=ted

【问题讨论】:

  • 此代码易受 xss 攻击。
  • 怎么样?很高兴在您的评论的基础上再接再厉!
  • http://localhost/movie/index.php/'&gt;&lt;script&gt;alert(/xss/)&lt;/script&gt;

标签: php .htaccess mod-rewrite


【解决方案1】:

重写规则不会更改 url,它们只是告诉您的服务器解释属于规则 ^search/([a-zA-Z0-9]+)/$ 的 url,例如 index.php?search=$1

如果你想更改将要使用的链接,你必须在你的html中更改它&lt;form action=''

【讨论】:

  • 我该怎么做呢,因为我已经测试了很多可能性,但仍然没有得到想要的结果。
  • 这个答案here 应该可以帮助你
  • 我似乎能得到的唯一 URL 是:localhost/movie/search/?search=ted 我尝试过的其他所有东西似乎都会导致重定向循环或完全失败。
猜你喜欢
  • 2012-04-27
  • 2017-04-15
  • 2015-05-28
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 2016-04-24
相关资源
最近更新 更多