【问题标题】:redirect GET form to user friendly URL将 GET 表单重定向到用户友好的 URL
【发布时间】:2015-09-10 10:59:49
【问题描述】:

所以我有一个表单,它使用 GET 作为一种方法将我的数据重定向到此页面 (seccions/search.php),它将我重定向到:

http://host/seccions/search.php?search_term_string=([^/]*)&search_term_on=([^/]*)

一旦提交到其他模式,我想被重定向:

http://host/empresas/categoria/$1/poblacio/$2/

我正在尝试通过 htaccess 来实现这一点(因为我没有看到其他简单的方法)我尝试将丑陋的 url 重定向到漂亮的 url,稍后会在 htaccess 上使用转换为正确的 url 模式这段代码:

RewriteRule ^seccions/search.php?search_term_string=([^/]*)&search_term_on=([^/]*)$ empresas/categoria/$1/poblacio/$2/ [L]
RewriteRule ^empresas/categoria/([^/]*)/poblacio/([^/]*)$ seccions/search.php?que=$1&on=$2 [L]

(我知道我正在更改参数名称,并且 .php 搜索页面已经为不同的 $_GET 名称准备好了)

但是一旦表单被提交,它就会转到带有 GET 参数的丑陋 url。

有办法解决吗?

谢谢!

【问题讨论】:

    标签: html .htaccess


    【解决方案1】:

    您的第二条规则是正确的,但第一条将不起作用,因为您无法匹配 RewriteRule 中的查询字符串。您需要在RewriteCond 中使用THE_REQUEST 变量以避免循环:

    RewriteEngine On
    RewriteBase /puntvallesgit/
    
    RewriteCond %{THE_REQUEST} /search\.php\?search_term_string=([^\s&]+)&search_term_on=([^\s&]+) [NC]
    RewriteRule ^ empresas/categoria/%1/poblacio/%2/? [R=302,L,NE]
    
    RewriteRule ^empresas/categoria/([^/]+)/poblacio/([^/]+)$ seccions/search.php?que=$1&on=$2 [L,QSA]
    

    【讨论】:

    • 如果它应该在不接触代码的情况下工作,由于某种原因它不是:S
    • 如果我在表单中搜索,它会将我重定向到“localhost/puntvallesgit/seccions/…”,预期的应该是“localhost/puntvalles/categoria/Articles+nous/poblacio/Agramunt”,我确实有其他条件相同的 url 模式,但使用 1 或 2 个参数而不是之前的 2 个(用于面包屑)我将发布正确的回复,以便您可以查看与这些链接相关的所有 HTACCESS 代码。
    • 好的,现在它“工作”了,但它杀死了我的本地主机子文件夹,将我重定向到“localhost/empresas/categoria/Articles+erotics/poblacio/Agramunt”而不是“localhost/puntvallesgit/empresas/categoria/Articles+erotics/…
    • 您是否忘记在问题中提及/puntvallesgit/
    • 我可能忽略了它,因为它“不应该”影响本地主机,所有其他重写不需要指定与工作“根”文件夹相关的任何内容,就像它在线时一样不会使用该子文件夹。我现在要测试
    猜你喜欢
    • 2014-02-24
    • 1970-01-01
    • 2014-11-13
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多