表单操作GET+Mod Rewrite


Form action GET + Mod Rewrite

如果我重复了一个问题,我很抱歉,但我似乎无法通过查看其他答案来解决这个问题:(

我有一张带有GET操作的表格:

<form action="tag" role="form" method="get">

其中包含一个名为"q"的搜索输入

我想要的是,当我提交表单时,导航栏中显示的URL是:

http://localhost/web/tag/dress

相反,

http://localhost/web/tag?q=dress

在我的.htaccess中,我有

RewriteRule ^tag/([0-9a-z]+)/?$ index.php?tag=$1 [L]

当我直接访问时,它工作得很好

http://localhost/web/tag/dress

我只想让表单把我带到这个干净的URL,而不是使用?q=导航栏中的参数。

有什么建议吗?

my.htaccess的简化版本是:

ErrorDocument 404 http://localhost/web/#404
RewriteEngine on
RewriteRule ^index.html index.php [L]
###TAG
RewriteRule ^tag/([0-9a-z]+)/?$ index.php?seccion=home&tag=$1[L]
###USER PROFILE
RewriteRule ^([0-9a-z]+)/?$ index.php?seccion=profile&user=$1 [L]

我想知道我是否被迫用javascript来做这件事?

类似于:

$('.search-form').submit(function (event) { 
    var tag = $('#q').val();
    window.location='tag/'+tag;
    return false;
});

在现有规则之前添加此附加规则:

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} 's/+web/tag'?q=([^'s&]+) [NC]
RewriteRule ^ /web/tag/%1? [R=302,L]

更新:您的完整.hta访问:注意:缺少"/"。现在它应该起作用了。

ErrorDocument 404 web/#404
RewriteEngine on
RewriteBase /web/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /tag/'?q=([^'s&]+) [NC]
RewriteRule ^ tag/%1? [R=302,L]
RewriteRule ^index'.html$ index.php [L,NC]
###TAG
RewriteRule ^tag/([0-9a-z]+)/?$ index.php?seccion=home&tag=$1 [L,NC,QSA]
###USER PROFILE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z]+)/?$ index.php?seccion=profile&user=$1 [L,NC,QSA]