I set env variables in a distributor htaccess file this way:
RewriteCond %{HTTP_HOST} ([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ %1/$1 [QSA,L,E=DOMAIN:%1,E=URI:$1]
After that apache start to process another htaccess file in the %1 subfolder.
How can I read the ENV:DOMAIN and ENV:URI variables in that htaccess?
I tried with
%{ENV:DOMAIN}
%{ENV:URI}
but those variables are empty...
Try using %{ENV:REDIRECT_DOMAIN} instead. Apache adds the REDIRECT_ prefix before every environment variable, after rewriting.
Related
I have a simple .htaccessfile
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^v4r.info/(.*)/(.*) v4r.info/NGOplus/index.php?NGO=$1&page=$2 [L,QSA]
I tested the file in htaccess.madewithlove.com, it gives a correct result and copy&pasting the result works flawlessly. (http://localhost/v4r.info/NGOplus/index.php?NGO=action-for-woman&page=board.list.php&ff=710;;;;;&startdate=2017-11-11)
But htaccess fails on localhost with an error:
File does not exist:
/var/www/html/public_html/v4r.info/action-for-woman/board.list.php
The test URL is
localhost/v4r.info/NGOplus/index.php?NGO=action-for-woman&page=board.list.php&ff=710;;;;;&startdate=2017-11-11
htaccess is active. (rubbish line gives "internal server error")
in another directory htaccess is working fine.
apache.conf seems ok (AllowOverride All)
Added:
The htaccess file is not in the base directory but in the 1. subdirectory (v4r.info).
What works is htaccess in v4r.info/NGOplus with a symlink 'action-for-woman' to NGOplus
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
Here, apache does a «local» rewrite, i.e. just the last part of the URL (the directory name 'action-for-woman' I have to extract from $_SERVER ...)
my .htaccess file is in v4r.info directory what is not the root directory.
In that case, your rule will never match. The RewriteRule pattern matches a URL-path relative to the directory that contains the .htaccess file.
But anyhow, rewriting is not recursive afaik.
Yes, it is "recursive" in a directory context (ie. .htaccess). In that the rewrite engine "loops" repeatedly until the URL passes through unchanged, or you have explicitly set END (Apache 2.4).
Try the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !index\.php$
RewriteRule ^([^/]+)/([^/]+)$ /v4r.info/NGOplus/index.php?NGO=$1&page=$2 [L,QSA]
The check against the REDIRECT_STATUS environment variable is to ensure that only direct requests are rewritten and not already rewritten requests.
However, this pattern is still far too generic as it matches any two path segments. I put the 2nd condition that checks index.php just so you can request /v4r.info/NGOplus/index.php directly (as you were doing in your tests). However, this could be avoided by making the regex more specific.
I am setting a apache environment variable through php function, apache_setenv.
apache_setenv("EXAMPLE_VAR", "Example Value");
But I am having problem accessing this variable inside .htaccess I am not able to use this env variable inside .htaccess in anyway. but I can access it through php getenv().
And problem is not only by setting variable through php I am unable to access env variable set by .htaccess itself.
I've tried
Suppose env variable to be www.domain.com.
RewriteEngine On
RewriteCond ^(.*)$ test.php?id=%{ENV:VARIABLE} [L]
&
RewriteEngine On
RewriteCond %{ENV:VARIABLE} ^www.(.+)
RewriteRule ^ test.php?id=%1 [L]
And similar other test to just verify that env variable are set and functioning.
Mod_env and mod_rewrite is on.
Variable set using SetEnv directives are not seen by mod_rewrite due to sequence of module loading by Apache. Use SetEnvIf instead like that:
SetEnvIf Host ^ VAR=/foo/bin
Then use it in mod_rewrite rules:
RewriteEngine On
RewriteCond %{ENV:VAR} =/foo/bin
RewriteRule ^home/?$ /something [L,NC]
I have a set of apache (2.4.25) sites that use mod_vhost_alias so the one vhost services multiple subdomains.
I want to be able to set a environment variable, say
<VirtualHost _default_:443>`
ServerNameAlias *.whatever-it-is.com
SetEnv HOME_URL https://www.whatever-it-is.com/
# etc
then in my various sites .htaccess files pick up that variable. So a site might allow access to a subfolder or a specific file and redirect everything back to the home site.
php_flag engine off
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?
RewriteCond %{REQUEST_URI} !^/myapp[/]?
RewriteCond %{REQUEST_URI} !^favicon.ico
RewriteRule (.*) %{HOME_URL} [R=301,L]
# ^--- not working, how would you do it?
then in the case where I need to change the home_url across all sites, I can just apply it in the vhost and each of the dozens of subdomain sites will pick it up.
* also tried
RewriteRule (.*) %{ENV:HOME_URL} [R=301,L]
http://httpd.apache.org/docs/2.0/env.html
The %{ENV:variable} form of TestString in the RewriteCond allows mod_rewrite's rewrite engine to make decisions conditional on environment variables...
Use SetEnvIf to set your environment variable so that it is available for mod_rewrite later in .htaccess.
Inside VirtualHost config have it like this:
SetEnvIf Host ^ HOME_URL=https://www.whatever-it-is.com/
Then inside .htaccess have a simple rule:
RewriteEngine On
RewriteRule .* %{ENV:HOME_URL} [R=301,L]
Make sure to clear your browser cache before testing this change.
I'm using VertrigoServ 2.27 on my laptop (localhost:8080).
Rewrite module is enabled and i tested it with alice.html and bob.html example
(http://stackoverflow.com/questions/6944521/url-rewriting-on-localhost)
and it works with .htacces inside www-subfolder. And I also put rubbish text inside .htaccess and
I got error from Apcheserver so rewrite mod is running and I cab use rules.
here is the rule: (inside /www/folder1/.htaccess)
Options +FollowSymLinks
RewriteEngine On
RewriteRule /(.*) /index.php?disp=$1 [QSA,L]
So when I put this url into browser my index page loaded ok.
http://localhost:8080/folder1/index.php
And the problem is here: When I request login via index.page(login page) and send url to localhost
server by cliking send-button
the url changed localhost:8080/login, it should be localhost:8080/folder1/login
how I can keep subfolder name in url?
and I want convert urls like this: www.best-food-of-the-usa.com/index.php?operation=top&state=arizona&
city=mesa&limit=10
to like this: www.best-food-of-the-usa.com/arizona/mesa/top10.html
Any help is appreciated. Thanks
\Jose
RewriteRule is behaving as the docs say.
From RewriteRule Directive Apache Docs
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
So, when in Directory and htaccess context the prefix /www/folder1/ will be removed. Also remember when matching with RewriteRule in Directory and htaccess context, the pattern will never begin with /.
So, your RewriteRule Should be:
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*) /index.php?disp=$1 [QSA,L]
How to get file directory trough .htaccess by using RewriteRule ^(.*)$ ?id=$1 [L,QSA]?
If .htaccess is located in http://localhost/some/dir/.htaccess and I'm opening http://localhost/some/dir/here/I/use/RewriteRule/, how I detect value /some/dir/ without using RewriteBase and without manual adding %{DOCUMENT_ROOT}/some/dir/, like value localhost I get trough %{HTTP_HOST}?
If you do not use RewriteBase you need to tell mod-rewrite the real Directory Root /var/ww/mysite/some/dir in the rewrite rule. RewriteBase would take the location url and map it to the directory.
So you'll maybe end up with
RewriteRule /var/ww/mysite/some/dir/(.*)$ ?id=$1 [L,QSA]
And trying to map some internal variables it may be
RewriteRule %{DOCUMENT_ROOT}/some/dir/(.*)$ ?id=$1 [L,QSA]
But I'm unsure, I rarely use mod_rewrite in .htaccess -- I prefer Directory tags, and the file path management can be different in .htaccess (auto removal and adding of directory prefixes). If you do not find a solution try to ask Servfault, plenty of admins other there.
Actualy Apache still does not have pathinfo($,PATHINFO_DIRNAME), function like has PHP.
So on now there are solution on using %{REQUEST_URI}, like this example:
RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L]
may reset with:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,L]