REQUEST_URI is not overridden by using APACHE RewriteRule?
Asked Answered
S

3

10

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]
Samaritan answered 3/5, 2011 at 3:47 Comment(1)
That is how mod_rewrite works.Demotic
K
1

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.

Kolyma answered 3/5, 2011 at 9:3 Comment(2)
Thanks @mgutt. 5) is what i ended up doing.Samaritan
Another interesting tidbit i learned : if you have rewrite rules in the .htaccess file, they get run twice. Alternative is to plugin the rewrite rules into the httpd.conf file, but then i keep getting '400 bad request' errors.Samaritan
T
0

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'];
Tetrode answered 20/5, 2011 at 18:25 Comment(0)
H
-1

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.

Hotfoot answered 3/5, 2011 at 5:8 Comment(5)
not true. check httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule "Back-references are $N (N=0..9) identifiers which will be replaced by the contents of the Nth group of the matched Pattern."Samaritan
I won't argue with documentation, but I've never seen $0 used. Also, you're doing exactly what I said to do in your previous rule. Why the inconsistency?Hotfoot
i have used $0 before and it works (its same as in preg_match). The inconsistency is my bad, but even if i make it consistent (by using $0 throughout), the results are still the same.Samaritan
Interesting. Any reason to use $0 vs $1 as a starting point? Is it just personal preference?Hotfoot
i have seen $0 being used elsewhere, so you could say its a preference.Samaritan

© 2022 - 2024 — McMap. All rights reserved.