【发布时间】:2017-04-18 09:52:23
【问题描述】:
好的,我尝试了多种解决方案,但仍然无法实现。所以我需要做的是
mydomain.com/search?q=product&l=london
到
mydomain.com/search/product/london
基本上我正在使用重写规则通过遵循此代码来隐藏 .php 扩展名
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^([^\.]+)$ $1.php [NC,L]
所以现在,search.php 转换为搜索和用户搜索时。它重定向到斜杠而不是查询字符串
这是我目前使用过的
RewriteCond %{THE_REQUEST} /search/?q=([^&]+)&l=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [NC,L,R]
#skip the rule if the request is for dir
RewriteCond %{REQUEST_FILENAME} !-d
#skip the rule if the request is for file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite any other request to "/packagemenu.php?"
RewriteRule ^([^/]+)/([^/]+)/?$ /search?q=$1&l=$2 [NC,L]
与
RewriteBase /search/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+) [NC]
RewriteRule ^ search/%1/%2/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC]
RewriteRule ^ search/%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual one
RewriteRule ^search/([^/]+)/?$ search?q=$1 [NC,L,QSA]
RewriteRule ^search/([^/]+)/([^/]+)/?$ search?q=$1&l=$2 [NC,L,QSA]
但它们都不起作用。我对这方面了解不多,所以请帮帮我。
【问题讨论】:
-
我认为这是一个愚蠢的错误,因为您有 GET 变量 location 但在正则表达式中搜索
l。 -
它是 l。对不起,我需要编辑获取变量是 q 和 l
-
search.php是否与 .htaccess 处于同一级别并且都在您的网站根目录中? -
是的@anubhava 这就是为什么 search.php 成为唯一的搜索
标签: php apache .htaccess mod-rewrite