htaccess-重写以捕获友好的URL或查询字符串 [英] htaccess - Rewrite to capture friendly URL or querystring
问题描述
我正在尝试提出一个或多个重写规则,这些规则将采用友好的URL或包含完整查询字符串的URL.
该计划是通过使用PHP的loadHTML读取URL来创建纯文本页面.
I'm trying to come up with one or more rewrite rules that will take either a friendly url or a url containing the full query string.
The plan is to create a text-only page by reading in the URL using PHP's loadHTML.
例如:
输入
1. http://www.example.com/disclaimer (http://www.example.com/text/disclaimer on text-only version)
2. http://www.example.com/info/aboutus (http://www.example.com/text/info/aboutus on text-only version)
3. http://www.example.com/news?id=123 (http://www.example.com/text/news?id=123 on text-only version)
输出
1. http://www.example.com/includes/textonly.php?page=disclaimer
2. http://www.example.com/includes/textonly.php?page=info/aboutus
3. http://www.example.com/includes/textonly.php?news?id=123
因此,在textonly.php上,我将使用$ _GET ['page']);例如1)和2),并使用$ _SERVER ['QUERY_STRING'];例如3).
So on the textonly.php I would use $_GET['page']); for example 1) and 2), and use $_SERVER['QUERY_STRING']; for example 3).
例如1)和2),我想到了:
For example 1) and 2), I came up with:
RewriteRule ^text/(.*) includes/textonly.php?page=$1
例如3),我想到了:
RewriteRule ^text/(.[?]) /includes/textonly.php [QSA]
他们独立工作,但不一起工作.有人可以帮忙吗?
They work independantly but not together. Can anyone help?
推荐答案
在汤姆和迈克尔的指导下,这是我想出的:
With guidance from Tom and Michael, this is what I've come up with:
在Htaccess中,将所有内容通过查询字符串发送到PHP:
in Htaccess send everything to PHP in the querystring:
RewriteRule ^text/(.*) /includes/textonly.php [QSA,L]
然后使用PHP:
$page = 'http://'.$_SERVER['HTTP_HOST'].'/'.str_replace('/text/','',$_SERVER['REQUEST_URI']);
似乎适用于友好的url(到目前为止已对2个级别进行了深入测试)和查询字符串. 希望它可以,所以我将用它作为解决方案:)
Seems to work for both friendly urls (2 levels deep tested so far) and querystrings. Hopefully its okay, so I'll go with this as a solution :)
这篇关于htaccess-重写以捕获友好的URL或查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!