1

I want all the incoming traffic to be tunneled through a PHP script, simply by uploading the script to the server and adding a few lines to my .htaccess. Is it - or something similar - possible?

E.g.:

  1. I visit a page.
  2. The PHP script gets the whole URL, and gives back the redirect target URL to the .htaccess.
  3. The .htaccess redirects only if the URL has been modified by the PHP script.
Tamás Bolvári
  • 2,976
  • 6
  • 34
  • 57

2 Answers2

2

Something very similar can be done using Apache's mod_rewrite more traditional rules. Or as you've noted in the conversation to @Zak's answer perhaps using an internal redirect.

Here is an example sequence:

  1. User visits page (e.g. http://myhost.com/my_area/my_file.jpg)
  2. .htaccess is configured to pass all requests for /myarea (or even /) to a php script. (e.g. handle-url.php) (Example code)
  3. The logic in handle-url.php can now do any of several things:
    1. Return an error
    2. Redirect the user to any url using the the header("Location: URL) method as mentioned by @Zak (e.g. http://myhost.com/file_area/my_file.jpg )
    3. Directly read the file from disk and return the data to the user. (Example code)

Take a look at the mod_rewrite's advanced example page. Depending on what exactly you need to do there may be other options.

For example, On-the-fly Content-Regeneration may be helpful. As may FallbackResource.

Community
  • 1
  • 1
Wafflecode
  • 45
  • 9
  • 1
    I need a solution in which Apache makes the last redirect, not PHP. I have tons of existing RewriteRules, that I don't want to implement in PHP. (And the new logic that I want to add is too complex for Apache, so in this case I need PHP.) – Tamás Bolvári May 17 '16 at 01:01
  • hmm. see: http://stackoverflow.com/a/1390754/6287702 It's possible such a virtual redirect (i.e. lacking a ptoto+host) can trigger a subrequest through mod_rewite. Can't test this at the moment. Worst case senario, you could just proxy out the back of your php script and pass through the reply. But that's getting ugly. – Wafflecode May 17 '16 at 06:27
1

.htaccess is fired BEFORE the PHP script. Look at .htaccess as an extension of you web server (Apache) settings. You will not be able to "send" something to .htaccess as it's a configuration file for a server, not an engine itself. Conversly you can use PHP's native header redirect

PHP

header('Location: http://www.example.com/');
Zak
  • 6,976
  • 2
  • 26
  • 48
  • Maybe I could make Apache do a subrequest, so that at first it makes an internal redirect to the script, and then makes an external redirect - if needed - based on the script's return value, that could be stored in a server- or environment variable. Is it impossible? – Tamás Bolvári May 16 '16 at 23:43
  • RewriteMap - which I don't know too much about - seems to be promising as well. Except the fact that it can't be used in .htaccess context. – Tamás Bolvári May 16 '16 at 23:46
  • This one is interesting as well, I'll read more about it: _%{LA-F:variable} can be used to perform an internal (filename-based) sub-request, to determine the final value of variable. Most of the time, this is the same as LA-U above._ (Source: [httpd.apache.org](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond)) – Tamás Bolvári May 16 '16 at 23:50