简单的 htaccess 重写/重定向到搜索页面

发布于 2024-12-08 13:10:30 字数 1075 浏览 0 评论 0原文

我有一个搜索表单,它将 GET 请求发送到名为 search.php 的页面。我在 htaccess 文件中设置了重写规则,将某些内容(例如 /search)重写到各自的页面。我只想将 search.php?q=query 重写为 /search/query。

这是我所拥有的。

RewriteRule search.php?q=(.*) /search/$1
RewriteRule search/(.*) search.php?q=$1 [nc]

我做错了什么?!

这是完整的文件

ErrorDocument 404 /index.php?p=404

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L] 

RewriteCond %{HTTP_REFERER} !^$  
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wghandcrafted.com/.*$ [NC]  
RewriteRule \.(gif|jpg|jpeg|psd|js|swf|flv|png)$ /feed/ [R=302]  

RewriteRule ^(products|blog|feed|search|checkout|checkout)$ $1.php [nc]
RewriteRule products/cat/(.*)$ products.php?type=cat&cat=$1 [nc]
RewriteRule products/(.*)$ products.php?type=single&product=$1 [nc]
RewriteRule blog/(.*) blog.php&post=$1 [nc]
RewriteRule feed/(.*) feed.phptype=$1 [nc]

RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]

I have a search form that sends a GET request to a page called search.php. I have rewrite rules set up in my htaccess file that rewrite certain things, like /search, to their respective pages. I simply want to take the search.php?q=query and rewrite it to /search/query.

Here is what I have.

RewriteRule search.php?q=(.*) /search/$1
RewriteRule search/(.*) search.php?q=$1 [nc]

What am I doing wrong?!

Here is the complete file

ErrorDocument 404 /index.php?p=404

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L] 

RewriteCond %{HTTP_REFERER} !^$  
RewriteCond %{HTTP_REFERER} !^http://(www\.)?wghandcrafted.com/.*$ [NC]  
RewriteRule \.(gif|jpg|jpeg|psd|js|swf|flv|png)$ /feed/ [R=302]  

RewriteRule ^(products|blog|feed|search|checkout|checkout)$ $1.php [nc]
RewriteRule products/cat/(.*)$ products.php?type=cat&cat=$1 [nc]
RewriteRule products/(.*)$ products.php?type=single&product=$1 [nc]
RewriteRule blog/(.*) blog.php&post=$1 [nc]
RewriteRule feed/(.*) feed.phptype=$1 [nc]

RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

吃颗糖壮壮胆 2024-12-15 13:10:30

使第一行执行重定向,第二行执行重写

RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]

移至规则集的末尾。

否则,RewriteCond %{REQUEST_FILENAME} !-f 规则会先于其他规则制定,这意味着只有对不存在文件的请求才会由该行下面的任何规则处理。由于存在 search.php 文件,因此可以防止达到该规则。

Make the first line perform a Redirect and the second perform a Rewrite

RewriteRule search\.php?q=(.*)$ /search/$1 [R=301,L]
RewriteRule search/(.*)$ search.php?q=$1 [NC]

and move

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^images/.*\.jpg$ /images/default.jpg [L]

to the end of the set of rules.

Otherwise, the RewriteCond %{REQUEST_FILENAME} !-f rule is enacted before anything else, meaning that only requests for non-existant files will be handled by any rules below that line. As there is a search.php file, this prevents that rule from ever being reached.

浪推晚风 2024-12-15 13:10:30

我遇到了同样的问题,这是我发现对我有用的解决方案,在我的网站上,查询被发送到index.php,我发现我是否有“^index.php$ /search/%1? [R =301]”作为第一个重写规则,它只会出错,因为第二个重写规则使其进入循环,所以我用“^$”替换“^index.php$”,允许它仍然请求相同的文件。这可能不是最好的解决方案,但却是一种可行的解决方案。这是我的工作代码:

Options +FollowSymLinks
RewriteEngine on    

RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^$ /search/%1? [R=301]

RewriteBase /search
RewriteRule ^search/(.*)$ /index.php?q=$1 [NC]

I was having the same problem and here is a solution i found that worked for me, on my site the queries are being sent to index.php, I discovered if I had "^index.php$ /search/%1? [R=301]" as the first rewrite rule it will just error out because of the second rewrite rule making it just go in a loop so i replaced "^index.php$" with "^$" allowing it to still request the same file. It might not be the best solution, but a work around that works. Here is my working code:

Options +FollowSymLinks
RewriteEngine on    

RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^$ /search/%1? [R=301]

RewriteBase /search
RewriteRule ^search/(.*)$ /index.php?q=$1 [NC]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文