Rewrite search page query URL with htaccess

淺唱寂寞╮ 提交于 2019-12-24 04:27:30

问题


I want when search form submitted, the URL be like this :

localhost/xampp/external/proj3/search/searchquery

Instead of this:

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


回答1:


You can use this code in your DOCUMENT_ROOT/.htaccess file:

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]



回答2:


Create a .htaccess file in your document root. And put this:

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

Note: Be careful with the regular expression. Filter it cautiously.



来源:https://stackoverflow.com/questions/28250100/rewrite-search-page-query-url-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!