简单的htaccess重写/重定向到搜索页面 [英] Simple htaccess rewrite/redirect to search page

查看:183
本文介绍了简单的htaccess重写/重定向到搜索页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送一个GET请求到一个名为的search.php网页搜索表单。我有重写建立在改写某些事情,比如/搜索我的htaccess文件的规则,以各自的网页。我只是想采取的search.php?Q =查询并将其重写/搜索/查询。

下面是我。

 重写规则的search.php?Q =(。*)/搜索/ $ 1
重写规则搜索/(.*)的search.php?Q = $ 1 [NC]
 

我是什么做错了吗?!

下面是完整的文件

 的ErrorDocument 404 /index.php?p=404

选项​​+的FollowSymLinks
RewriteEngine叙述上

的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^图像/.* \ JPG $ /images/default.jpg [L]

的RewriteCond%{HTTP_REFERER}!^ $
的RewriteCond%{HTTP_REFERER} ^ HTTP://(WWW \)wghandcrafted.com /.*$ [NC]
重写规则\(GIF | JPG | JPEG | PSD | JS |瑞士法郎| FLV | PNG)。$ /料/ [R = 302]

重写规则^(产品|博客|饲料|搜索|结帐|结帐)$ $ 1.PHP [NC]
重写规则的产品/猫/(.*)$ products.php类型=猫和放大器;猫= $ 1 [NC]
重写规则的产品/(.*)$ products.php类型=单&放大器;产品= $ 1 [NC]
重写规则博客/(.*)blog.php的&放大器;后= $ 1 [NC]
重写规则饲料/(.*)feed.phptype = $ 1 [NC]

重写规则搜索\的.php?Q =(。*)$ /搜索/ $ 1 [R = 301,L]
重写规则搜索/(.*)$的search.php?Q = $ 1 [NC]
 

解决方案

请在第一线执行重定向,第二进行重写

 重写规则搜索\的.php?Q =(。*)$ /搜索/ $ 1 [R = 301,L]
重写规则搜索/(.*)$的search.php?Q = $ 1 [NC]
 

和移动

 的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^图像/.* \ JPG $ /images/default.jpg [L]
 

要的组规则的结尾。

否则,的RewriteCond%{} REQUEST_FILENAME!-f 规则别的之前制定的,这意味着只请求对不存在的文件将通过下面的规则来处理该行。由于有一个的search.php 文件,该prevents,从统治日益被达成。

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]

解决方案

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.

这篇关于简单的htaccess重写/重定向到搜索页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆