【问题标题】:Modrewrite is not sending $_GET paramsMod_rewrite 没有发送 $_GET 参数
【发布时间】:2013-02-22 20:06:39
【问题描述】:

我正在尝试重写我的网址:

RewriteEngine On
RewriteBase /

RewriteRule ^help/(.+)/(.+) help.php?helpid=$2
RewriteRule ^city/(.+)/(.+) city.php?cityid=$2

即使网址包含预期的格式:http://example.com/help/the-irelevant-title/5/

好像

isset($_GET['helpid'])) 总是会在 help.php 中触发 false

通常这个系统对我有用。我在这里有什么遗漏吗?

-编辑-

我可以看到这应该有效(或至少你们中的一个人回答)我正在向问题添加更多代码:

完整的 .htaccess 内容

RewriteEngine On

ErrorDocument 500 /oohps.php
ErrorDocument 404 /oohps.php

RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

RewriteBase /

RewriteRule ^help/([^/]+)/([^/]+) help.php?helpid=$2 [QSA]
RewriteRule ^city/([^/]+)/([^/]+) city.php?cityid=$2 [QSA]

RewriteRule ^login$ login.php

help.php 的开始

session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
$_GET['noseo'] = true;
include('htmlhead.php');
/* Leemos la id del grupo*/
$selected;
if(isset($_GET['helpid'])){
    $selected = $_GET['helpid'];
}else {
    echo '<script>alert("Modrewrite fails. Id is missing, id: '.$_GET['helpid'].'");location.href = "/"</script>';   /* Allways returns this*/
}

【问题讨论】:

  • 确保 MultiViews 被禁用,否则 Apache 可能会在不使用 mod_rewrite 的情况下将 /help/ 映射到 help.php

标签: php .htaccess url-rewriting get


【解决方案1】:

正则表达式过于贪婪,第一个 .+ 也匹配斜线,导致正则表达式的其余部分失败。 [^/]+ 正则表达式将匹配除斜线之外的任何内容。

RewriteRule ^help/([^/]+)/([^/]+) help.php?helpid=$2
RewriteRule ^city/([^/]+)/([^/]+) city.php?cityid=$2

【讨论】:

  • 也可以使用非贪婪量词:^help/(.+?)/(.+).
【解决方案2】:

使用 [QSA] 应该可以转发 GET 参数。此外,将.+(贪婪)更改为[^/]+(意味着任何不是斜线重复1次或多次的字符),它应该可以解决您的问题。

RewriteRule ^help/([^/]+)/([^/]+) help.php?helpid=$2 [QSA]
RewriteRule ^city/([^/]+)/([^/]+) city.php?cityid=$2 [QSA]

也可以简写为:

RewriteRule ^(help|city)/([^/]+)/([^/]+) $1.php?$1id=$3 [QSA]

From the apache manual:

当替换 URI 包含查询字符串时,RewriteRule 的默认行为是丢弃现有的查询字符串,并将其替换为新生成的查询字符串。使用 [QSA] 标志会合并查询字符串。

考虑以下规则:

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

使用 [QSA] 标志,对 /pages/123?one=two 的请求将映射到 /page.php?page=123&one=two。如果没有 [QSA] 标志,相同的请求将被映射到 /page.php?page=123 - 也就是说,现有的查询字符串将被丢弃。

【讨论】:

  • QSA 不适用,他的初始网址似乎没有查询字符串。他正试图从 /help/foo/bar 转到 /help.php?helpid=bar
  • QSA-Flag 用于将现有查询附加到重写的 url,它将 /help/title/5/?param=1 变成 help.php?helpid=5&param=1。您不必处理查询。这非常有用,但在这里不是正确的答案。
  • @SamDufel 我现在知道了,并在你写答案之前更新了我的 OP,以指定这只是为了其他人在谷歌上搜索相同的问题。
  • 我喜欢这个'RewriteRule ^(help|city)/([^/]+)/([^/]+) $1.php?$1id=$3 [QSA]' +1
【解决方案3】:

你有两个变量是有原因的吗?这虽然有效:

RewriteEngine On
RewriteBase /    
RewriteRule ^help/([^/\.]+)/([^/\.]+)/?$ help.php?helpid=$2 [L]
RewriteRule ^city/([^/\.]+)/([^/\.]+)/?$ city.php?cityid=$2 [L]

【讨论】:

  • 是的,第一个变量是由于 SEO。我知道我只需要 id 但是是的,在这种情况下我需要它来提供与 URL 相关的关键字
  • 进行了您需要的编辑。不过,其他人已经给出了基本相同的答案。
  • 不确定,对我来说似乎没问题?处理这样的 seo 类型内容的更好方法是执行类似 RewriteRule ^help/([^/\.]+)/?$ help.php?helpid=$2 [L] 之类的操作,然后通过类似 / 的方式调用脚本help/53-this_is_the_title ,然后从 - 中分解以获取 id 或像 CakePHP 这样的框架本机支持这个
【解决方案4】:

你可以在根目录下的 .htaccess 文件中试试这个:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/([^/]+)/[^/]+/([^/]+)/?  [NC]
RewriteRule .*              %1.php?%1id=%2             [L]

重定向

http://domain.com/par1/anything/par2 带或不带斜杠

http://domain.com/par1.php?par1id=par2

字符串 par1anythingpar2 被假定为动态的,根据问题中的示例,par1 是 PHP 脚本的名称,也是 key 的第一部分重定向查询中的名称。

par1 = 帮助时的示例:

重定向:

http://domain.com/help/the-irelevant-title/5/

收件人:

http://domain.com/help.php?helpid=5

对于永久重定向,将[L] 替换为[R=301,L]


虽然我没有测试其他规则并且不知道它们是否按预期工作,但问题中的 .htaccess 文件应该像这样修改以在此答案中包含规则集:

Options +FollowSymlinks -MultiViews
RewriteEngine On
ErrorDocument 500 /oohps.php
ErrorDocument 404 /oohps.php

RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/([^/]+)/[^/]+/([^/]+)/?  [NC]
RewriteRule .*              %1.php?%1id=%2             [L]

# Next line is a suggested addition to prevent loops.
RewriteCond %{REQUEST_URI}  !login\.php [NC]
RewriteRule ^login$   login.php         [L]

【讨论】:

  • 其实只是通过添加 'Options +FollowSymlinks -MultiViews' 解决了。非常感谢!
  • 不客气。这是我认为缺少的东西,可能是问题的原因,但我也将这两条规则合并为一条。也许以这种方式拥有它们会更好,更通用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 2012-05-24
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
相关资源
最近更新 更多