REQUEST_URI 未被使用 APACHE RewriteRule 覆盖吗?

发布于 2024-11-04 20:30:10 字数 3264 浏览 1 评论 0原文

问题:

我使用 Kohana/PHP 为其他公司开发托管网站。 我让客户在其 DNS 服务器中输入 CNAME 条目以指向我的域。例如。 http://invites.somecompany.com 指向 http: //www.mydomain.com

因此,我的 apache 服务器上的 %{HTTP_HOST} 条目是“invites.somecompany.com”,

我想重写 http ://invites.somecompany.com/invitehttp://www.mydomain.com/invites/invite

尽管 Apache 似乎正在这样做,但 $_SERVER['REQUEST_URI '] 仍然是“/”。 问题是 Kohana 使用 $_SERVER['REQUEST_URI'] 将请求路由到适当的控制器代码。在这种情况下,它将其路由到 base 索引控制器,而不是“invites”控制器。

事实:

我正在使用的 Apache mod_rewrite 指令(在 .htaccess 文件中):-

RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteCond %{REQUEST_URI} !.*invites.*
RewriteRule ^(.*)$ invites/$1

# For Kohana
RewriteRule .* index.php?kohana_uri=$0 [PT,QSA,L]

在 index.php 中,我做:

var_dump($_SERVER);

并且我得到:

'REQUEST_URI' => string '/',
'QUERY_STRING' => string 'kohana_uri=invites/index.php&kohana_uri=invites/invite'
'REDIRECT_QUERY_STRING' => string 'kohana_uri=invites/invite'

那么 mod_rewrite 不会修改 REQUEST_URI 吗?

需要:

'REQUEST_URI' => 'invites/invite',
'QUERY_STRING' => string 'kohana_uri=invites/invite',

我如何获得它?

===================== 编辑

重写日志条目:-

strip per-dir prefix: /Users/project/invite -> invite
 applying pattern '^(?:application|modules|system)\b.*' to uri 'invite'
 strip per-dir prefix: /Users/project/invite -> invite
 applying pattern '\.git' to uri 'invite'
 strip per-dir prefix: /Users/project/invite -> invite
 applying pattern '^(.*)$' to uri 'invite'
 rewrite invite -> invites/invite
 add per-dir prefix: invites/invite -> /Users/project/invites/invite
 strip per-dir prefix: /Users/project/invites/invite -> invites/invite
 applying pattern '.*' to uri 'invites/invite'
 rewrite invites/invite -> index.php?kohana_uri=invites/invite
 add per-dir prefix: index.php -> /Users/project/index.php
 strip document_root prefix: /Users/project/index.php -> /index.php
 internal redirect with /index.php [INTERNAL REDIRECT]
 strip per-dir prefix: /Users/project/index.php -> index.php
 applying pattern '^(?:application|modules|system)\b.*' to uri 'index.php'
 strip per-dir prefix: /Users/project/index.php -> index.php
 applying pattern '\.git' to uri 'index.php'
 strip per-dir prefix: /Users/project/index.php -> index.php
 applying pattern '^(.*)$' to uri 'index.php'
 rewrite index.php -> invites/index.php
 add per-dir prefix: invites/index.php -> /Users/project/invites/index.php
 strip per-dir prefix: /Users/project/invites/index.php -> invites/index.php
 applying pattern '.*' to uri 'invites/index.php'
 rewrite invites/index.php -> index.php?kohana_uri=invites/index.php
 add per-dir prefix: index.php -> /Users/project/index.php
 initial URL equal rewritten URL: /Users/project/index.php [IGNORING REWRITE]

Problem :

Am using Kohana/PHP to develop a hosted website for other companies.
I get the customer to put in a CNAME entry in their DNS server to point to my domain. Eg. http://invites.somecompany.com is pointing to http://www.mydomain.com.

Thus, the %{HTTP_HOST} entry on my apache server is 'invites.somecompany.com'

I want to rewrite http://invites.somecompany.com/invite to http://www.mydomain.com/invites/invite

Even though Apache seems to be doing that, the $_SERVER['REQUEST_URI'] is still "/".
The problem is Kohana uses $_SERVER['REQUEST_URI'] to route the request to the appropriate controller code. In this case, it routes it to the base index controller, instead of the 'invites' controller.

Facts :

The Apache mod_rewrite directives i am using (in the .htaccess file) :-

RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteCond %{REQUEST_URI} !.*invites.*
RewriteRule ^(.*)$ invites/$1

# For Kohana
RewriteRule .* index.php?kohana_uri=$0 [PT,QSA,L]

in the index.php, i do :

var_dump($_SERVER);

and i get :

'REQUEST_URI' => string '/',
'QUERY_STRING' => string 'kohana_uri=invites/index.php&kohana_uri=invites/invite'
'REDIRECT_QUERY_STRING' => string 'kohana_uri=invites/invite'

So the mod_rewrite does not modify the REQUEST_URI ?

Need :

'REQUEST_URI' => 'invites/invite',
'QUERY_STRING' => string 'kohana_uri=invites/invite',

How do I get that?

======================
Edit

Rewrite Log Entries :-

strip per-dir prefix: /Users/project/invite -> invite
 applying pattern '^(?:application|modules|system)\b.*' to uri 'invite'
 strip per-dir prefix: /Users/project/invite -> invite
 applying pattern '\.git' to uri 'invite'
 strip per-dir prefix: /Users/project/invite -> invite
 applying pattern '^(.*)
 to uri 'invite'
 rewrite invite -> invites/invite
 add per-dir prefix: invites/invite -> /Users/project/invites/invite
 strip per-dir prefix: /Users/project/invites/invite -> invites/invite
 applying pattern '.*' to uri 'invites/invite'
 rewrite invites/invite -> index.php?kohana_uri=invites/invite
 add per-dir prefix: index.php -> /Users/project/index.php
 strip document_root prefix: /Users/project/index.php -> /index.php
 internal redirect with /index.php [INTERNAL REDIRECT]
 strip per-dir prefix: /Users/project/index.php -> index.php
 applying pattern '^(?:application|modules|system)\b.*' to uri 'index.php'
 strip per-dir prefix: /Users/project/index.php -> index.php
 applying pattern '\.git' to uri 'index.php'
 strip per-dir prefix: /Users/project/index.php -> index.php
 applying pattern '^(.*)
 to uri 'index.php'
 rewrite index.php -> invites/index.php
 add per-dir prefix: invites/index.php -> /Users/project/invites/index.php
 strip per-dir prefix: /Users/project/invites/index.php -> invites/index.php
 applying pattern '.*' to uri 'invites/index.php'
 rewrite invites/index.php -> index.php?kohana_uri=invites/index.php
 add per-dir prefix: index.php -> /Users/project/index.php
 initial URL equal rewritten URL: /Users/project/index.php [IGNORING REWRITE]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沦落红尘 2024-11-11 20:30:10

1.) 如果手动调用此链接是否有效?
http://www.mydomain.com/invites/invite

2.) RewriteCond % {HTTP_HOST} !^www.mydomain.com$ 需要像 RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ 一样进行转义

3.) RewriteRule 。 * index.php?kohana_uri=$0 [PT,QSA,L] 是一个无限循环,因为 index.php 也适合正则表达式 .*

4.) 您需要 R 标志将用户重定向到 inform/RewriteRule ^(.*)$invites/$1 [R=301]%{REQUEST_URI} 与浏览器地址栏中的 uri 相同。

5.) 如果您不想重定向访问者,您可以“破解”kohana 系统并在第一行设置 $_SERVER['REQUEST_URI'] = $_GET['myURI'];如果这是使其运行的唯一选项,则为index.php。 myURI 可以根据需要通过 mod_rewrite 填充。

1.) Does this link work if you call it manually?
http://www.mydomain.com/invites/invite

2.) RewriteCond %{HTTP_HOST} !^www.mydomain.com$needs to be escaped like RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$

3.) RewriteRule .* index.php?kohana_uri=$0 [PT,QSA,L] is a infinite loop as index.php fits to the regex .*, too.

4.) You need the R flag to redirect the user to invite/ RewriteRule ^(.*)$ invites/$1 [R=301]. The %{REQUEST_URI} is the same uri as in the browser address bar.

5.) If you don't want to redirect the visitor you could "hack" the kohana system and set $_SERVER['REQUEST_URI'] = $_GET['myURI']; in the first line of the index.php if this would be the only option to get it running. The myURI can be filled through mod_rewrite as you wish.

我不在是我 2024-11-11 20:30:10

由于 mod_rewrite 的工作方式,您无法立即将其放入 $_SERVER['REQUEST_URI'] 中。如果您愿意修改 $_SERVER (这有时被认为是一个坏主意),那么像这样的事情怎么样:

$_SERVER['REQUEST_URI'] = $_GET['kohana_uri'];
$_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_QUERY_STRING'];

Because of the way mod_rewrite works, you can't have it in $_SERVER['REQUEST_URI'] immediately. If you're happy to modify $_SERVER (which is sometimes considered a bad idea), how about something like this:

$_SERVER['REQUEST_URI'] = $_GET['kohana_uri'];
$_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_QUERY_STRING'];
巴黎盛开的樱花 2024-11-11 20:30:10

删除了我之前的答案,因为它完全错误。感谢您的文档链接,学到了新东西。

删除 QSA 标志:

# For Kohana
RewriteRule .* index.php?kohana_uri=$0 [PT,L]

根据文档

'qsappend|QSA'(查询字符串追加)
该标志强制重写引擎
附加查询字符串部分
替换字符串到现有的
字符串,而不是替换它。使用
当您想要添加更多数据时
通过重写规则的查询字符串。

第一次就应该注意到这一点。

Deleted my previous answer as it was flat out wrong. Thanks for the documentation link, learned something new.

Remove the QSA flag:

# For Kohana
RewriteRule .* index.php?kohana_uri=$0 [PT,L]

Per the Docs:

'qsappend|QSA' (query string append)
This flag forces the rewrite engine to
append a query string part of the
substitution string to the existing
string, instead of replacing it. Use
this when you want to add more data to
the query string via a rewrite rule.

Should have noticed that the first time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文