简体   繁体   English

PHP的高级Htaccess重写规则

[英]Advanced Htaccess Rewrite Rule For PHP

I currently have the following code to rewrite search engine friendly url's to something PHP can handle however there are a few issues. 我目前有以下代码重写搜索引擎友好的URL到PHP可以处理的东西,但有一些问题。

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/ . 这会将domain.com/cat/mens/size/large/重写为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: 如果我可以使用不同数量的子目录(例如对PHP代码更友好的子目录)将其重写为类似的东西,那将是理想的:

Ideal Rewrite: domain.com/index.php?cat=mens&size=large 理想的重写: domain.com/index.php?cat=mens&size=large

Another Example: domain.com/page/value/subpage/subvalue/something/true/ 另一个例子: domain.com/page/value/subpage/subvalue/something/true/

Should Rewrite To: domain.com/index.php?page=value&subpage=subvalue&something=true 应重写为: domain.com/index.php?page=value&subpage=subvalue&something=true


Also is there a way to rewrite: domain.com/search/?q=blah+blah 还有一种方法可以重写: domain.com/search/?q=blah+blah

To: domain.com/index.php?search&q=blah+blah 致: domain.com/index.php?search&q=blah+blah

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). 当url中的变量数量未预定义时,最好将其作为字符串传递给php,并让它处理它(explode,implode,foreach,build a 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. 基本上,RewriteRule的第一部分是给定的url,第二部分是转到脚本的部分。 Every (somethingInHere) in the first in a variable that you can use in the second one like $1, $2, $3 and $4. 变量中第一个中的每个(somethingInHere),你可以在第二个变量中使用,如$ 1,$ 2,$ 3和$ 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM