【问题标题】:Wrong value sent in htaccess redirect在 htaccess 重定向中发送的值错误
【发布时间】:2012-05-31 08:56:55
【问题描述】:

各位,我遇到了 htaccess 的问题。 我正在使用 codeigniter 作为框架。 我目前使用的代码是:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9_-]+)\.example\.(.*+) [NC]
RewriteCond %{REQUEST_URI} /index([0-9]+) [NC]
RewriteRule .* /index.php/main_controller/method/%1 [QSA,L]

现在我想做的是将 %1 作为一个值发送到我的 'main_controller' 控制器的名为 'method' 的方法。问题:不是发送进来的值([a-zA-Z0-9_-]+),而是发送进来的值([0-9]+),如果我发送%2应该是这种情况....我想发送进来的值([a-zA-Z0-9_-]+), 比如说,如果 url 是

somevalue.example.com/index33

它发送的是33而不是somevalue

【问题讨论】:

  • 你的最后一个 RewriteCond 覆盖了前一个,这意味着 $1/%1 是 /index(here) 之后的值

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} /index([0-9]+) [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.(.*+) [NC]
RewriteRule .* /index.php/main_controller/method/%1 [QSA,L]

【讨论】:

  • 感谢您的回复尼克,现在我可以得到一些值,但 %2 无法得到 33。我需要它们两个。:(我的意思是,两个 ([^.]+) 来自 ^ ([^.]+)\.example... 和 index([0-9]+) 中的 ([0-9]+) 是必需的:(
【解决方案2】:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.(.*+) [NC]
RewriteRule /index([0-9]+) /index.php/main_controller/method/%1/$1 [QSA,L]

所以,愚蠢的我。要从 URI 中获取变量,您不必将其放入 REQUEST_URI,只需将其放入 Rewrite rule 即可,RewrideCond 中的变量是在 %1 中获得的,而 ([0-9]+) 中的变量是从 RewriteRule /index([0 -9]+) 获得 1 美元。希望对其他人也有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 2020-02-15
    • 2017-12-12
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2014-04-23
    相关资源
    最近更新 更多