我有一个使用PHP的网站。 这是一个允许某人使用PHP GET调用来search数据库的网站。 然后显示所有符合search的项目。
有许多searchfilter(价格,来源网站,类别)。 这是search后,用户input“蓝色车”和“达拉斯”
http://example.com/after_search.php?search_title=blue+cars&search_location=Dallas&price-0=index&price_max=&price_min=&category=every-cat&store=all&max_number=20
有没有办法让它看起来像这样:
http://example.com/s/blue+cars/l/Dallas
而不改变代码结束的任何GETfunction?
我也有在网站上运行的JQuery,如果这是可以用来解决这个问题。
注意:我正在使用nginx
编辑1
鉴于下面的build议这似乎是一个nginx的问题。
这是我目前的/ etc / nginx / sites-available / default文件:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.php index.html index.htm; server_name server_domain_name_or_IP; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
当我在最后一个“}”之前简单地添加它时:
# nginx configuration location / { if (!-e $request_filename){ rewrite ^(.*)$ /array_search.php?path=$1 break; } }
下面的答案是nginx转换的apache代码,这似乎没有改变。
我也放置了list(,$_GET['search_title'], $_GET['search_extra'], $_GET['search_location']) = explode('/',$_GET['path']); // Add more parameters as needed
list(,$_GET['search_title'], $_GET['search_extra'], $_GET['search_location']) = explode('/',$_GET['path']); // Add more parameters as needed
在index和after_search php文件顶部list(,$_GET['search_title'], $_GET['search_extra'], $_GET['search_location']) = explode('/',$_GET['path']); // Add more parameters as needed
代码。
这个答案假设你的web服务器正在运行apache并且mod_rewrite模块被启用(通常默认启用)
这是我想办法解决的一个方法。 然而,你将不得不在你的php文件的开头添加一些php代码(但是你不应该改变你现有的实现中的其他任何东西)
首先在与after_search.php
相同的位置创建或编辑.htaccess
文件。 将下面的代码放在.htaccess
文件中:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /after_search.php?path=$1 [NC,L,QSA]
这将使网址http://example.com/s/blue+cars/l/Dallas将被重写为http://example.com/after_search.php?path=s/blue+cars/l /达拉斯幕后(意味着人们仍然会看到http://example.com/s/blue+cars/l/ 达拉斯 )
然后在你的php文件中,做到这一点:
<?php list(,$_GET['search_title'], $_GET['search_extra'], $_GET['search_location']) = explode('/',$_GET['path']); // Add more parameters as needed
这将拆分我们从htaccess文件创建的路径参数,并将每个参数分配给$ _GET变量中的相应键。 你只需要在php文件的顶部进行一次这样的操作,而不必碰其他任何东西。
编辑:我跑了通过“htaccess的nginx转换器”的Apache配置,并获得了Nginx的以下代码
# nginx configuration location / { if (!-e $request_filename){ rewrite ^(.*)$ /after_search.php?path=$1 break; } }