htaccess SetEnv REDIRECT_ prefix - apache

I'm setting an environment variable in an htaccess file and it's being prepended with "REDIRECT_". As far as I've read, this is caused by URL rewriting.. the thing is, I'm not doing any rewriting (that I'm aware of).
My webspace contains two files:
.htaccess
SetEnv Foo "bar"
index.php
<?php
print_r($_ENV);
Now, I'm guessing this may have something to do with the fact that this is on a shared hosting package with 1&1.. is there anything else I should be checking or has anyone experienced this before? Or am I just missing something??
Cheers

The issue is triggered by using PHP as a CGI Wrapper. If PHP is running as mod_php apache module it's not prefixing your variables.
Reason for that is internal redirect handling thus apache recreates the variables with the REDIRECT_ prefix :-/
Solution (updated)
Use PHP-FPM or mod_php
If you wanna use CGI Wrapping for PHP put this into your .htaccess:
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

First of all your syntax for SetEnv is wrong. It should be like this:
SetEnv Foo "bar"
Then to access this field from PHP you need to do:
echo $_SERVER["Foo"];
Which should show up as "bar".

Related

Is it possible to create a dynamic SetEnv in an .htaccess file?

Currently I am trying to set an environment variable to point to a directory but I need it to be dynamic.
Currently I have the following
SetEnv WP_DIR /home/user101/website/public_html/www/app
Is it possible to make that WP_DIR variable get the /home/user101/website/public_html/www part dynamically?
Something like:
SetEnv WP_DIR ${CURRENT_PATH}/app
Please keep in mind that I am really new to this.
SetEnv isn't equip to handle dynamic data as its value. But mod_rewrite can handle this for you. There isn't an ENV variable, to the best of my knowledge, which contains the current absolute path of the directory. But you can write something fancy to grab it:
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*?)/?\1$
RewriteRule ^.*$ - [E=WP_DIR:%{DOCUMENT_ROOT}%2/app]
However setting ENV variables from .htaccess isn't so reliable since they can get prefixed by REDIRECT_ if the URL gets rewritten. You'll have to keep an eye out for REDIRECT_ prefixed variables from PHP.
If you have to do this kind of stuff in the first place, I think you're approaching your code wrong, PHP is well-equipped to process this kind of logic.
If you're trying to rewrite your requests to index.php and get around .htaccess's issue that it adds REDIRECT_ in front of your variable names when you want to set ENV variables from .htaccess for PHP, I'd strongly suggest not using ENV variables in the first place. One approach is RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]. But I dislike this ugly approach since it uses the querystring. The best method I've found is to simply rewrite your requests to index.php then from PHP you'd read strtok($_SERVER["REQUEST_URI"],'?') will contain the request the user requested without the query string. You'd still reference $_GET[...] for such parameters.
You can use PassEnv directive to pass on env variable from system to Apache:
PassEnv CURRENT_PATH
SetEnv WP_DIR ${CURRENT_PATH}/app

How do I configure apache for a custom directory?

Trying to configure apache2 to load example.com/forum/ from a different document root, relative to the site root. Forums are installed somewhere else on the server.
Is there a directory alias command? I've found the alias configuration entry for apache, but had no luck.
Basically, I want example.com to have the same directory its always had, but example.com/forum/ to be hosted somewhere else, on the same server.
I tagged this question with mod_rewrite because I thought maybe it would be the key, here.
Cheers!
Alias is the right way, unless you have some subtlety that you didn't reveal in your question.
# http.conf
Alias /forum /usr/lib/bbs/ # or whatever
The job of Alias is to take the abstract URL coming into your system and map it to a concrete filesystem path. Once it has done that, the request is no longer an URL but a path. If there is no Alias or similar directive handling that URL, then it will get mapped to a conrete path via DocumentRoot.
If this isn't working, you have to debug it further. Are you getting errors when you access /forum? Look in the error log.
It all depends of what you want. You can "hardlink" with real path and it works (so you were right to think it could work with mod_rewrite).
Quick sample (that works on my production domains) to make an internal change (I add a subdirectory):
RewriteRule (.*) %{DOCUMENT_ROOT}/mysubfolder%{REQUEST_FILENAME} [QSA,L]
So you can easily do something like:
RewriteRule ^/forum/(.*) %{DOCUMENT_ROOT}/mysubfolder%{REQUEST_FILENAME} [QSA,L]
And my suggestion would be that if you plan to have more rewrite rules, keep everything homogeneous, i.e.: keep on using only rewrite rules, so use my suggestion above. This way you'll not get a bad mix of Alias, RewriteRules and so on. For nice and clean stuff: keep everything homogeneous.

Changing a file's URL without physically moving it

I have a site, running Linux + Apache.
I have a file in my root directory, let's say file.php.
I want the URL to the file to be "domain.com/newdir/file.php", but I don't want to actually create the newdir and move the file there because it would be a huge hassle to update many many links all over my site.
Is there a way to accomplish this, meaning making the file accessible by the new URL without moving it?
Thank you.
On this site: workwith.me, you can find information about .htaccess and mod_rewrite. For your example you have to make a file called .htaccess and put it in the root directory. The file should contain these directives:
RewriteEngine on
RewriteRule ^newdir/file.php$ /file.php [L]
You can do this for every file you want to rename.
Four possible solutions I can think of:
If your OS supports it, create a symlink:
mkdir /home/foo/htdocs/newdir
ln -s /home/foo/htdocs/file.php home/foo/htdocs/newdir/file.php
... and make sure Apache is configured to follow them:
Options FollowSymLinks
Create an Alias or AliasMatch (probably overkill)
Good old mod_rewrite:
RewriteEngine One
RewriteRule ^newdir/file\.php$ file.php [L]
Ugly: use a custom 404 error page with a PHP script that checks $_SERVER['REQUEST_URI'].
I guess the standard solutions are #1 and #3.

How to configure apache (ubuntu) such that www.mysite.com will direct to www.mysite.com/drupal6/?

I am a newbie to ubuntu and apache. Can someone tell me how I could direct to
www.mysite.com/drupal6
when user address www.mysite.com?
Thanks a lot.
Cheers.
If you are running Apache and Ubuntu, there is actually a really easy way to force this redirect using a simple php script.
Create an index.php file in the root of your server and paste the following code into it
<?php header("location: drupal6/") ?>
This will cause the site to auto-redirect to the drupal6 folder whenever it is visited.
This should work. Create a file in the root folder of your server called .htaccess - the dot at the beginning is very important as this helps the server identify the file as a hidden / system config file.
Open the file and paste the following lines of code in :
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ www.mysite.com/drupal6/$1 [R,L]
This should force all traffic to the server to redirect to your custom folder.
A brief explanation of the .htaccess code
If you want rewrites to work, you have to enable the Rewrite Engine and tell the server to follow symlinks.
The second section establishes the rule - specifically applying it to all traffic on the standard web port of 80.
The final line tells the server to grab everything after the URL and append it to the new address (mysite.com/drupal6).
There's a lot more you can do with .htaccess files but you really need to Google for good examples to test out.
Look at Apache's mod_rewrite documentation. You will need a RewriteRule in your apache configuration at the minimum, you may also need RewriteCond's to define when the RewriteRule is used.
Your rewrite pattern will be rewriting the REQUEST_URI with something from: ^/$ to: /drupal6. The ^ and $ are essential to prevent Apache getting into an infinite loop while rewriting the base URI by only matching "/" and not "/anything-else".
I assume you're on a recent version of Ubuntu and Apache? If so, see the Apache 2.2 documentation on mod_rewrite.

.htaccess mod_rewrite rule not working in Ubuntu

My apologies if this is an easy one. I have Googled it up the hizzy to no avail.
I am running Ubuntu 9.04, Jaunty Jackelope and Apache2. After much trouble, I finally enabled mod_rewrite, and my .htaccess file is attempting to do it's thing, but is failing. This is my setup.
In /etc/apache2/conf.d/ I have a file called apeace-public-html.conf. It reads as follows:
# makes /home/apeace/public_html/ available through /localhost/apeace
Alias /apeace /home/apeace/public_html/
And in /home/apeace/public_html/ I have the following .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^test\.html$ index.html
Also in /home/apeace/public_html/ I have a file named index.html, but I do NOT have a file named test.html. It seems to me that this rewrite should show index.html when I try to access http://localhost/apeace/test.html. However, this is the error I get:
Not Found
The requested URL /home/apeace/public_html/index.html was not found on this server.
So the question is, what in the world am I doing wrong?
Much thanks.
-apeace
Just a guess here, but can you try to make the RewriteRule like ^test.html$ /apeace/index.html
From the error message, it seems it is translating `http://localhost/apeace/test.html to http://localhost/home/apeace/public_html/index.html
Your rewrite rule is working correctly since it's telling you it can't find "index.html". If you went to test.html and it said it can't find "test.html" then your rewrite rule would be at fault.
So what this means is that something else is wrong in your setup, whether it's a bad file or directory name somewhere, or whatever else. Make sure there's nothing basic you're overlooking.
But in answer to your question (especially the title), your htaccess is fine.