基于 smarty 的网站中的 URL 重写?
我想实现这一目标
example.com/search_results/?action=search&username[equal]=PorscheSA
,我已经在许多网站上使用 .htaccess来
example.com/PorscheSA
实现这一目标,但由于该网站是基于 smarty 的,因此它似乎不起作用。任何帮助将不胜感激。
好的,这是 .htaccess 文件:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
为了实现以下目标,我尝试了以下操作:
RewriteRule ^([a-zA-Z0-9-_+^/])/$ /search_results/?action=search&username[equal]=$1
但什么也没发生。
I wanted to achieve this
example.com/search_results/?action=search&username[equal]=PorscheSA
to
example.com/PorscheSA
I have used .htaccess for many websites to achieve this, but since this website is smarty based, it doesn't seem to work. Any help will be much appreciated.
Ok, this is the .htaccess file:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
In order to achieve the following I tried this:
RewriteRule ^([a-zA-Z0-9-_+^/])/$ /search_results/?action=search&username[equal]=$1
and nothing happened.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题对于您到底期望发生什么仍然有点模糊,但既然您已经发布了
.htaccess
,我会尝试一下,看看是否得到您想要的结果后。您的
RewriteRule
不起作用的最明显原因是您的测试模式与您作为示例提供的 URL 不匹配,因为您的RewriteRule
需要尾随斜杠(并且仅匹配之前的一个字符)。此外,如果您将其放在.htaccess
文件中当前的规则之后,它将永远不会匹配,因为请求已被重写为index.php
。我认为你想要这样的东西...
但是,如果
search_results/
目录不存在,那么无论如何这都会被重写为index.php
,所以我不确定在这种情况下重定向的目的是什么。而且,如果它确实存在,由于您的测试模式几乎与我期望在站点路径中看到的任何内容相匹配,那么所有内容都将被重写到search_results/
目录中,所以很少(如果有的话) )最终会出现在您网站根目录的index.php
处。基于此,我觉得您可能遗漏了一些其他标准,但我可能是错的。
Your question is still a little vague as to what exactly you expect to have happen, but since you've posted your
.htaccess
, I'll take a stab at it and see if I get what you were after.The most obvious reason that your
RewriteRule
isn't working is that your test pattern doesn't match the URL you've provided as an example, as yourRewriteRule
requires a trailing slash (and only matches one character before that). Additionally, if you put it after the rules that you currently have in your.htaccess
file, it will never match because the request will have already been rewritten toindex.php
.I think that you want something like this...
However, if the
search_results/
directory doesn't exist, then this is just going to be rewritten toindex.php
anyway, so I'm not sure what the purpose of the redirection would be in that case. And, if it does exist, since your test pattern matches pretty much anything I'd expect to see in a site path, then everything will be rewritten to thesearch_results/
directory, so very little (if anything) will end up at your site root'sindex.php
.Based on that, I feel like maybe there's some other criteria that you may have left out, but I could be wrong.