PHP 的高级 Htaccess 重写规则

发布于 2024-08-23 04:02:59 字数 756 浏览 4 评论 0原文

我目前有以下代码将搜索引擎友好的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凹づ凸ル 2024-08-30 04:02:59

试试这个:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文