.htacces 捕获页面名称并将其作为 get 方法发送
我正在尝试捕获一个网址,例如
http://www.mysite.com/somepage.php?sometext=somevalue
并将其重定向到。
http://www.mysite.com/index.php?page=somepage.php&sometext=somevalue
我尝试在网上搜索这样的.htaccess,但找不到。
你能帮我吗?
I am trying to capture a url such as
http://www.mysite.com/somepage.php?sometext=somevalue
and redirect it to.
http://www.mysite.com/index.php?page=somepage.php&sometext=somevalue
I tried searching for such .htaccess online, but couldn't find it.
Can you please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定这是重复的,
但我在找到它/它们时遇到了一些问题[编辑:我找到了一个,尽管可能不是最好的例子]。无论如何,这是一个用相当标准的代码解决的相当标准的问题:
RewriteRule
将整个请求捕获为$1
,并将其作为页面传递到 index.php
获取参数。末尾的
[QSA]
标志表示采用任何现有的 GET 参数(示例中的sometext=somevalue
),并将它们添加为新请求中的附加 GET 参数。 ([L]
标志只是表示这应该是执行的最后一条规则。)请注意,这还将重定向对图像或 CSS 文件等内容的请求,因此最好添加以下行 直接在此规则之前:
这些行表示“如果请求是针对实际存在的文件或目录,则不处理该规则。”这样,对真实文件的请求将直接由 Apache 提供服务,而不是由 PHP 脚本处理(或更可能是错误处理)。
I'm quite sure this is a duplicate,
but I'm having a bit of an issue finding it/them[Edit: I found one, though possibly not the best example].Anyway, this is a fairly standard problem resolved with fairly standard code:
The
RewriteRule
captures the entire request as$1
, and passes it to index.php as thepage
GET parameter.The
[QSA]
flag on the end says to take any existing GET parameters (sometext=somevalue
in your example), and add them as additional GET parameters on the new request. (The[L]
flag just says that this should be the last rule executed.)Note that this will also redirect requests for things like images or CSS files, so it's good to add the following lines directly before this rule:
These lines say "if the request is for a file or directory that actually exists, don't process the rule." That way, requests for real files will be served directly by Apache, rather than being handled (or more likely, mishandled) by your PHP script.