PHP:获取mod

编程入门 行业动态 更新时间:2024-10-26 08:33:46
本文介绍了PHP:获取mod-rewriteURL变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图传递参数,并使用 GET 函数在PHP上已使用mod-rewrite重写的URL。

我有这在我的.htaccess文件:

选项+的FollowSymLinksRewriteEngine叙述在的RewriteBase /的RewriteCond%{HTTP_HOST} ^!WWW \\。(。*)重写规则^ $ HTTP://www.% {HTTP_HOST} / $ 1 [R = 301,L]的RewriteCond%{} REQUEST_FILENAME!-f的RewriteCond%{} REQUEST_FILENAME!-d重写规则^([^ ​​- ] *)?/ $行动= $ 1 [L]重写规则^搜索/ $?=动作搜索[L]

如果我有像 www.example/search/?q=test A URL我无法得到任何的参数,我犯罪嫌疑人是由于我已经重写URL!?

我一直在使用类似下面的 QSA 标记尝试,但仍然没有能够得到它的工作。

重写规则^搜索/?$?=动作搜索[L,QSA]

解决方案

您需要更新您的RewriteRules使用的 QSA (查询字符串附加)标志(不包括?字符在规则):

重写规则^搜索/ $?=动作搜索[L,QSA]

的 QSA 标记告诉Apache将追加到URL preserve什么:

  

在更换URI包含查询字符串,默认行为  重写规则是放弃现有的查询字符串,并更换  用新生成的一个。使用[QSA]标志导致查询  串要组合

    

考虑以下规则:

    

重写规则/pages/(.+),/page.php?page=$1[QSA]

    

随着 [QSA]   国旗,为请求/网页/ 123?一种=两个将被映射到   /page.php?page=123&one=two 。如果没有 [QSA] 标志,同样的请求  将映射到 /page.php?page=123 - 也就是说,现有的查询  字符串将被丢弃。

此外, ^搜索/ $ 规则永远达不到的,因为它是由这一个覆盖:重写规则^([^ ​​- ] *)/ $?ACTION = $ 1 [L] (其中有→标志)。您需要更新您的整个的.htaccess 文件如下:

选项+的FollowSymLinksRewriteEngine叙述在的RewriteBase /的RewriteCond%{HTTP_HOST} ^!WWW \\。(。*)重写规则^ $ HTTP://www.% {HTTP_HOST} / $ 1 [R = 301,L]重写规则^搜索/ $?=动作搜索[L,QSA]的RewriteCond%{} REQUEST_FILENAME!-f的RewriteCond%{} REQUEST_FILENAME!-d重写规则^([^ ​​- ] *)?/ $行动= $ 1 [L,QSA]

I'm trying to pass parameters and use the GET function in PHP on a URL that has already been rewritten using mod rewrite.

I have this in my HTACCESS file:

Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ www.%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^-]*)/$ ?action=$1 [L] RewriteRule ^search/$ ?action=search [L]

If I have a URL like www.example/search/?q=test I'm unable to get any of the parameters which I suspect is due to me already rewriting the URL!?

I've tried using the QSA flag like below but still haven't been able to get it working.

RewriteRule ^search/?$ ?action=search [L,QSA]

解决方案

You need to update your RewriteRules to use the QSA (Query String Append) flag (without the ? character in the rule):

RewriteRule ^search/$ ?action=search [L,QSA]

The QSA flag tells Apache to preserve anything appended to the URL:

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

RewriteRule "/pages/(.+)" "/page.php?page=$1" [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.

In addition, the ^search/$ rule is never reached, because it is overridden by this one: RewriteRule ^([^-]*)/$ ?action=$1 [L] (which has the L flag). You need to update your entire .htaccess file as follows:

Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ www.%{HTTP_HOST}/$1 [R=301,L] RewriteRule ^search/$ ?action=search [L,QSA] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^-]*)/$ ?action=$1 [L,QSA]

更多推荐

PHP:获取mod

本文发布于:2023-11-02 08:35:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   mod

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!