PHP 的高级 Htaccess 重写规则
我目前有以下代码将搜索引擎友好的 url 重写为 PHP 可以处理的内容,但是存在一些问题。
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)/(.*) index.php?$1/$2
这会将 domain.com/cat/mens/size/large/
重写为 domain.com/index.php?cat/mens/size/large/
。
如果我可以使用不同数量的子目录将其重写为类似的内容,例如这对 PHP 代码更友好,那就太理想了:
理想重写: domain.com/index.php?cat=mens&size= 另一个示例
:domain.com/page/value/subpage/subvalue/something/true/
应重写为:domain.com/index.php?page=value& subpage=subvalue&something=true
还有一种方法可以重写:domain.com/search/?q=blah+blah
到:domain.com/index.php ?搜索&q=blah+blah
I currently have the following code to rewrite search engine friendly url's to something PHP can handle however there are a few issues.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)/(.*) index.php?$1/$2
This rewrites domain.com/cat/mens/size/large/
to domain.com/index.php?cat/mens/size/large/
.
It would be ideal if I could rewrite it to something like this using varying amounts of sub directories such as this which is friendlier to the PHP code:
Ideal Rewrite: domain.com/index.php?cat=mens&size=large
Another Example: domain.com/page/value/subpage/subvalue/something/true/
Should Rewrite To: domain.com/index.php?page=value&subpage=subvalue&something=true
Also is there a way to rewrite: domain.com/search/?q=blah+blah
To: domain.com/index.php?search&q=blah+blah
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
RewriteRule /page/(.*)/subpage/(.*)/something/(.*) index.php?page=$1&subpage=$2&something=$3
当url 中的变量数量没有预定义,最好将其作为字符串传递给 php,并让它处理它(爆炸、内爆、foreach、构建数组)。
RewriteRule /search/?q=(.*) index.php?search&q=$1
至于您在下面要求的内容:
RewriteRule /cat/(.*)/size/( .*)/?q=(.*)&abc=(.*) index.php?search&cat=$1&size=$2&q=$3&abc=$4
基本上, RewriteRule 的第一部分是给定的 url,第二部分是转到脚本的 URL。第一个变量中的每个 (somethingInHere) 都可以在第二个变量中使用,例如 $1、$2、$3 和 $4。
Try this:
RewriteRule /page/(.*)/subpage/(.*)/something/(.*) index.php?page=$1&subpage=$2&something=$3
When the amount of variables in the url isn't predefined it's better to just pass it as a string to the php, and let it handle it (explode, implode, foreach, build an array).
RewriteRule /search/?q=(.*) index.php?search&q=$1
As for what you asked for below:
RewriteRule /cat/(.*)/size/(.*)/?q=(.*)&abc=(.*) index.php?search&cat=$1&size=$2&q=$3&abc=$4
Basically, the first part of RewriteRule is the url given, the second part is the one that goes to the script. Every (somethingInHere) in the first in a variable that you can use in the second one like $1, $2, $3 and $4.