使用htaccess重写搜索页面查询URL

时间:2015-01-31 09:27:13

标签: regex apache .htaccess mod-rewrite redirect

我想在提交搜索表单时,网址如下:

localhost/xampp/external/proj3/search/searchquery

而不是:

localhost/xampp/external/proj3/tools/search/search.php?query=searchquery

2 个答案:

答案 0 :(得分:3)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On
RewriteBase /xampp/external/proj3

# external redirect from action URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?query=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]

# internal forward from pretty URL to actual URL
RewriteRule ^search/([^/.]+)/?$ search.php?query=$1 [L,QSA,NC]

答案 1 :(得分:0)

在文档根目录中创建.htaccess文件。并把它:

RewriteEngine   On
RewriteRule     ^search/([a-zA-Z0-9]+)/?$   \
                search.php?query=$1 \
                [NC,L]

注意:注意正则表达式。小心过滤。