htaccess中的重写规则将查询字符串转换为斜杠php

编程入门 行业动态 更新时间:2024-10-26 06:31:04
本文介绍了htaccess中的重写规则将查询字符串转换为斜杠php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好的,我尝试了多种解决方案,但仍然无法实现.所以我需要做的是

OK I've tried multiple solutions but still can't achieve it. so what I need to do is

mydomain/search?q=product&l=london

mydomain/search/product/london

基本上,我正在按照以下代码使用重写规则来隐藏.php扩展名

Basically I'm using rewrite rules to hide .php extension by following this code

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !- RewriteRule ^([^\.]+)$ $1.php [NC,L]

因此,现在,search.php会转换为搜索以及用户搜索时.它重定向到尾部斜杠而不是查询字符串

So now, search.php converts into search and when user searches. it redirects to trailing slash instead of query string

这是我到目前为止使用的

Here what I've used so far

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]

AND

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]

但是它们都不起作用.我没有那么多的知识,所以请帮帮我.

But both of them doesn't work. I don't have so much knowledge about so please help me out.

推荐答案

将您的网站根.htaccess设置为:

Have your site root .htaccess as this:

Options -MultiViews RewriteEngine On RewriteBase /jobportal/ # To externally redirect /search.php?q=product&l=london to /search/query/london/ RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)&l=([^\s&]+)\s [NC] RewriteRule ^ search/%1/%2/? [R=301,L,NE] # To externally redirect /search.php?q=product to /search/query/ RewriteCond %{THE_REQUEST} /search(?:\.php)?\?q=([^\s&]+)\s [NC] RewriteRule ^ search/%1/? [R=301,L,NE] # To externally redirect /dir/file.php to /dir/file RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC] RewriteRule ^ %1 [R=301,NE,L] # ignore all rules below for real files/directories RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L] # internal forward from /search/product/ to search.php?q=product RewriteRule ^search/([^/]+)/?$ search.php?q=$1 [NC,L,QSA] # internal forward from /search/product/london/ to search.php?q=product&l=london RewriteRule ^search/([^/]+)/([^/]+)/?$ search.php?q=$1&l=$2 [NC,L,QSA] # internal forward from /dir/file to /dir/file.php RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+?)/?$ $1.php [L]

更多推荐

htaccess中的重写规则将查询字符串转换为斜杠php

本文发布于:2023-11-01 12:11:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   重写   字符串   转换为   规则

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!