Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm in the process of migrating many sites from an old to a new server configuration. Each site is based on a similar (but sadly not identical) codebase, using mod_rewrite URLs.

  • Ubuntu 8.04 LTS => Ubuntu 12.04 LTS
  • Apache 2.22.8 => Apache 2.2.22
  • PHP 5.2 (FastCGI) => PHP 5.3 (PHP5-FPM)

Mostly working like a charm, but on the new config the $_SERVER['REDIRECT_URL'] is no longer set, and the code is failing due to a dependence on this global variable.

From what I understand, this variable gets set by Apache when a redirect occurs. Obviously this isn't happening now, but I'm struggling to find the cause.

  • Is it the Apache upgrade, or (my guess) the switch from PHP FastCGI to PHP5-FPM?
  • How do I get this variable back?

I'd really rather not have to edit the code on each site, so I'll set a global PHP auto_prepend if necessary, but ideally I'd like to fix the server configuration and have this set in the first place.

Potentially related: I now also have a couple of new $_SERVER variables, namely REDIRECT_SCRIPT_URL and REDIRECT_REDIRECT_SCRIPT_URL. These seem to have the correct data I want for the REDIRECT_URL, but also seem to indicate there's two internal redirects occurring that weren't before - Google searches for REDIRECT_REDIRECT_SCRIPT_URL only returns random var_dump outputs. Is SCRIPT_URL the new REDIRECT_URL?

Edit 1

Checking again REDIRECT_URL is (now) set, but always to 'index.php' (the mod_rewrite target) instead of the expected typed URL. I have resorted to a using PHP auto_prepend_file to manually set the needed variable.

I'm not sure how I missed it the first time round, but I've made several changes in the meantime so I suppose there's an outside chance it wasn't there. Apologies if this mislead anyone.

Edit 2

To address the mentions of ErrorDocument below, the mod_rewrite rule in use is:

RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

The $_GET['url'] variable is set, so the rule must be working.

To be clear, at this stage I've gone with the auto_prepend_file workaround I mentioned initially.

share|improve this question

migrated from stackoverflow.com Jul 19 '12 at 6:41

This question came from our site for professional and enthusiast programmers.

    
-1 for failing to show due diligence. A quick Google search is not a substitute for reading the documentation. Before upgrading systems or code you should ensure you understand what has changed, at least as far as it affects your systems or projects. –  John Gardeniers Jul 19 '12 at 23:47
    
Apologies for not being clear; I read the documentation as best as I could see. Then I resorted to Google when I came up against something I couldn't explain. The configuration is on a separate machine, and during pre-move testing I have discovered the above problem. I have, however, found a mistake I made, so I'm going to update the question. –  Barry Jul 24 '12 at 9:41
    
it's really in both your and our best interests if you tell us these things up front. I've reversed my vote. –  John Gardeniers Jul 25 '12 at 1:46
    
Can you give us more information on the previous and new server config? The setting of REDIRECT_URL in PHP comes from the SCRIPT_URL environment variable in apache I think, but there are many things that may modify the behaviour of that import (eg, suPHP, wrapping engines, AddTypeHandler statements, etc). –  Jon Kloske Aug 30 '12 at 5:22
add comment

3 Answers

REDIRECT_URL is set only by the Apache web server, and only in certain circumstances. It is probably not something your code should be relying on. As far as I can tell, the best solution would be to fix the buggy PHP code; you can't really force this to be always set in Apache.

share|improve this answer
    
Thanks for the response, though I couldn't see anything on that link related to my problem unfortunately. I don't think an external ErrorDocument is involved in this. I wish I could change the existing code, but that's not an option at this point. –  Barry Jul 30 '12 at 16:07
    
The point is, REDIRECT_URL is only set during ErrorDocument processing. That means your PHP scripts were loaded as ErrorDocuments in the original configuration, and whoever wrote them relied on this behavior. Now that it's not happening, the scripts break. –  Michael Hampton Jul 30 '12 at 16:12
    
There are no ErrorDocument definitions in use, and I can't see how the mod_rewrite RewriteRule (added above) is involving one. I'm not diagreeing with you about REDIRECT_URL not being guaranteed, I just can't find any relevant documentation as to why. –  Barry Aug 1 '12 at 16:24
    
I linked to it above in my answer. If you need something further, you'll have to read the Apache source code. Anyway, remember to accept the answer that resolved your problem by clicking the outline of the check mark next to it, and welcome to Server Fault. –  Michael Hampton Aug 1 '12 at 16:31
add comment

According to PHP's documentation:

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.

So it would seem that not all $_SERVER superglobal variables are as platform-agnostic as one would expect. $_SERVER['REQUEST_URI'] seems to be one such value that is consistently defined by all major webservers so you may want to try using that instead.

share|improve this answer
    
Thanks for this, but I was already aware that sometimes this is set, and sometimes not. What I was hoping for was a way to actively influence which variables get set for my particular configuration, as I cannot find any documentation on specifics based on my configuration (ie, how I would know in advance of an environment build). –  Barry Jul 24 '12 at 9:51
add comment

You can set a php auto-prepend-file to include a small snippet of PHP code that sets this variable.

share|improve this answer
    
In my question I already identified this as the course of action I would take if it wasn't possible. It was also what I ended up doing, as per the edit I made before you answered. –  Barry Jul 30 '12 at 16:01
    
Apologies, I don't recall seeing the edit before I posted; it may have been I started replying before you made the edit, but I can't be sure since the subsequent edit has removed the original edit datestamp :/ I also should have made it clearer that what I was suggesting is probably the correct way to achieve what you're after, rather than a "fallback". You can use [E:blah=blah] flags to the rewrite rules to set arbitrary environment variables to then fetch inside the pre-include file which is cleaner than using GET variables to pass stuff from the redirect engine to your code. –  Jon Kloske Aug 6 '12 at 2:20
    
And by "probably correct", I mean that's how I've done this exact thing in several places in the past. –  Jon Kloske Aug 6 '12 at 2:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.