如何使用htaccess分割网址 [英] How can i split URL with htaccess

查看:80
本文介绍了如何使用htaccess分割网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

google.com/en/game/game1.html

应该是

google.com/index.php ?p1 = en& p2 = game& p3 = game1.html



我如何分割URL并将index.php发送给 /部分?

For example:
google.com/en/game/game1.html
should be
google.com/index.php?p1=en&p2=game&p3=game1.html

how can i split URL and send index.php the part of "/" ?

推荐答案

仅当查询参数的长度固定时,才能实现此目的。否则,还有另一种方法,但需要解析应用程序中的路径。

You can only achieve this if the query parameters are of a fixed length. Otherwise there is an other way but requires parsing of the path in the application.

以下规则匹配所有三个URL部分,然后将它们重写为 index.php 的命名查询参数。

The following rule matches all three URL parts then rewrites them as named query arguments to index.php.

RewriteRule ^([^/]+)/([^/]+)/(.+)$ index.php?p1=$1&p2=$2&p3=$3

这将重写:

/en/game/game1.html

收件人:

/index.php?p1=en&p2=game&p3=game1.html



长度未知的实现



Unknown length implementation

# Don't rewrite if file exist. This is to prevent rewriting resources like images, scripts etc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?path=$0

这将重写:

/en/game/game1.html

收件人:

/index.php?path=en/game/game1.html

然后您可以解析应用程序中的路径。

Then you can parse the path in the application.

编辑:)为此,仅当URL的第一级包含两个字符时,重写规则才匹配:

) To make it so the rewrite rule only matches if the first level of the URL consists of two characters do:

RewriteRule ^([a-zA-Z]{2})/([^/]+)/(.+)$ index.php?p1=$1&p2=$2&p3=$3

您也可以使用未知长度这样实现:

You can also do it for the unknown length implementation so:

RewriteRule ^[a-zA-Z]{2}/ index.php?path=$0

这篇关于如何使用htaccess分割网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆