基于 smarty 的网站中的 URL 重写?

发布于 2024-09-10 17:53:56 字数 700 浏览 1 评论 0原文

我想实现这一目标

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-09-17 17:53:56

您的问题对于您到底期望发生什么仍然有点模糊,但既然您已经发布了 .htaccess,我会尝试一下,看看是否得到您想要的结果后。

您的 RewriteRule 不起作用的最明显原因是您的测试模式与您作为示例提供的 URL 不匹配,因为您的 RewriteRule 需要尾随斜杠(并且仅匹配之前的一个字符)。此外,如果您将其放在 .htaccess 文件中当前的规则之后,它将永远不会匹配,因为请求已被重写为 index.php

我认为你想要这样的东西...

Options +FollowSymlinks

RewriteEngine on

RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^.*$ http://example.com/$0 [L,R=301]

# Make sure we end and force the redirect so %{REQUEST_FILENAME} gets changed
RewriteRule ^[a-zA-Z0-9-_+^/]+$ search_results/?action=search&username[equal]=$0 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* 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 your RewriteRule 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 to index.php.

I think that you want something like this...

Options +FollowSymlinks

RewriteEngine on

RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^.*$ http://example.com/$0 [L,R=301]

# Make sure we end and force the redirect so %{REQUEST_FILENAME} gets changed
RewriteRule ^[a-zA-Z0-9-_+^/]+$ search_results/?action=search&username[equal]=$0 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

However, if the search_results/ directory doesn't exist, then this is just going to be rewritten to index.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 the search_results/ directory, so very little (if anything) will end up at your site root's index.php.

Based on that, I feel like maybe there's some other criteria that you may have left out, but I could be wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文