How does .htaccess behave when located in directory above doc root - apache

I have the following rules in my .htacess file. They work perfectly fine as is, but I have several doc roots which I would like to apply the same rules to, all in the same parent directory. Is this possible with .htaccess?
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/some/path($|/)
RewriteRule (.*) /some/path/$1 [L]
FallbackResource /some/path/index.html
I know .htaccess is somewhat relative to the directory they're in, so I'm thinking I need to modify these somehow.
The end result from just moving it directly to the parent directory is everything returns the contents of /some/path/index.html

For anyone this helps, I accomplished this by adding it to the "post virtual hosts" section of my main apache config.
<Directory "/home/*/parentdirectory/*">
RewriteEngine on
RewriteRule !/some/path/ /some/path%{REQUEST_URI} [L]
FallbackResource /some/path/index.html
</Directory>

Related

apache apply rewrite rules to only one subdirectory

I have the following directory structure:
/var/www/html
--myapp
--phpmyadmin
In the /var/www/html folder there is a .htaccess file with following content:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.sk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mydomain.sk$
RewriteCond %{REQUEST_URI} !myapp/public
RewriteRule (.*) /myapp/public/$1 [L]
If I access the http://mydomain.sk the rewrite rules work as expected and I get the myapp/public/index.php file as root. However, if I want to access http://mydomain.sk/phpmyadmin the request is redirected to http://mydomain/myapp/public/phpmyadmin. Here I get the error 404 - page not found.
I cannot find a way to only serve http://mydomain.sk/myapp/public as a root folder and to serve http://mydomain.sk/phpmyadmin "as is".
It took some time to figure this one out. The rules posted above actually work as expected. It turns out I had another set of rewrite rules in apache2.conf file (that I totally forgot about) that took precedence over my .htaccess file.

Redirect all requests for a subfolder to the subfolders index.php with Apaches .htaccess and/or httpd.conf

All I want to do is the following:
User attempts to connect to :
http://mydomain/invites/XYZ123456
or
http://mydomain/invites/FOOFOOBLAHBLAH
or
etc ...
It must work for any URL that is entered after /invites needs to display the same /invites/index.php
I want the webserver to always show /invites/index.php
I can't seem to get this to work...
I have tried to setup the .htaccess and place directly inside the folder (/my/WebServer/www/invites... which is http://localhost/invites/)
Note: I have made sure that I have loaded the 'rewrite_module' inside the apache httpd.conf
#.htaccess (located in /my/WebServer/www/invites)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule . index.php [R=302,L]
Any help greatly appreciated.
Thank you.
Make sure you have AllowOverride set to All inside the httpd.conf to enable the usage of the .htaccess file within the (for this example /www/invites directory).
Also make sure you have loaded the rewrite_module (mod_rewrite.so)
Hope this can help anyone who may run across this post:
I ended up using the following configuration parameters inside my .htaccess
# /www/invites/.htaccess
# Internal redirection to index.php for nonexistent URLs.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /invites/index.php [L,QSA]

Is it possible to force apache server to read root htaccess before going into subdirectory?

I have a subdirectory in root (let's call it /sub) and .htaccess in root. If I call the following URL: mywebsite.com/sub, I get directly into subdirectory omitting .htaccess in root, so I wanted to ask whether it is possible to force server to read root .htaccess first before going to subdirectory (and make corresponding redirection if needed)?
Thanks in advance
EDIT: I have discovered that my redirection explicitly excluded directories which was unwanted (and seemed to be the root of my problem):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://redirectwebsite.com/$1 [R=301,L]
I removed the second line, but unfortunately I still get into my folder.
So to make it clear:
when I go to http://mywebsite.com/not-a-folder-in-root, I am redirected to the new site correctly
when I fo to http://mywebsite.com/a-folder-in-root, I don't get redirected and go instead to my subfolder's index.php
The latter is the behavior I want to eliminate.
It depends on what you need to do. First .htaccess files are read in the order in which they are found.
How directives are read
The configuration directives found in a .htaccess file are applied to
the directory in which the .htaccess file is found, and to all
subdirectories thereof. However, it is important to also remember that
there may have been .htaccess files in directories higher up.
Directives are applied in the order that they are found. Therefore, a
.htaccess file in a particular directory may override directives found
in .htaccess files found higher up in the directory tree. And those,
in turn, may have overridden directives found yet higher up, or in the
main server configuration file itself.
So the htaccess file will always override the file in the root directory. However you can manipulate the Apache config file and specify directives in say a Location directive. You can specify certain rules and then it will take affect over .htaccess rule. See info below.
As discussed in the documentation on Configuration Sections, .htaccess
files can override the sections for the corresponding
directory, but will be overridden by other types of configuration
sections from the main configuration files. This fact can be used to
enforce certain configurations, even in the presence of a liberal
AllowOverride setting. For example, to prevent script execution while
allowing anything else to be set in .htaccess you can use:
<Directory "/www/htdocs">
AllowOverride All
</Directory>
<Location "/">
Options +IncludesNoExec -ExecCGI
</Location>
Otherwise if you can't do that change because you don't have access, you would have to do all the rules in the root .htaccess file that also pertains to the sub folder which you can do instead of putting an .htaccess in the sub folder.
Edit based on comment
#just use this for all requests
RewriteRule (.*) http://redirectwebsite.com/$1 [R=301,L]
#Or you can do all non-existent folders and that sub folder too
RewriteCond %{REQUEST_URI} ^/sub [NC]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://redirectwebsite.com/$1 [R=301,L]

mod_rewrite (Apache 2.4) RewriteBase outside htdocs

I stuck on a problem trying to get a my rewrite rules working.
Assume the following setup:
Folder structure like this:
app/
web/ <--- Document-Root
.htaccess <--- My mod_rewrite rules
index.php
vendor/
company/
package/
Folder/
Route.php
The vhost points to "web/" as Document-Root and the security is reduced
so all overrides are allowed:
<Directory "~PATH~\web">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order deny,allow
allow from all
Require all granted
</Directory>
Now i try to rewrite simply every request (*) to "Route.php" (internal! no 301 or something).
For my understanding if if would call "www.example.com/a/b" then i would expect that the
request is internal redirected to ~PATH~/vendor/vendor/company/package/Folder/Route.php
where i can fetch everything and take the request in PHP for further processing.
But the rewrite fails. Maybe its not possible to traverse a directory up "/../" ?!
Content of .htacces:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /../vendor/company/package/Folder
RewriteRule .* - [E=ROUTER:Route.php]
# route calls to router /x/y/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %{env:ROUTER}?mode=htaccess [QSA,L]
</IfModule>
Any help appreciated :)
Thanks in advance!
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule .* ../vendor/company/package/Folder/Router.php [L]
</IfModule>
EDIT:
I've tried it on my local machine.
This Setup works:
RewriteRule .* vendor/company/package/Folder/Router.php [L]
Seems like its not allowed to forward the Request to directories higher than the Docroot.
I've found this post:
htaccess RewriteRule redirecting to parent directory?
The important Part:
You can't rewrite to outside the document-root. This is a security thing.
EDIT2:
More Information: mod_rewrite - Apache HTTP Server Version 2.4
Quote:
URL-path
A DocumentRoot-relative path to the resource to be served. Note that mod_rewrite tries to guess whether you have specified a file-system path or a URL-path by checking to see if the first segment of the path exists at the root of the file-system. For example, if you specify a Substitution string of /www/file.html, then this will be treated as a URL-path unless a directory named www exists at the root or your file-system (or, in the case of using rewrites in a .htaccess file, relative to your document root), in which case it will be treated as a file-system path. If you wish other URL-mapping directives (such as Alias) to be applied to the resulting URL-path, use the [PT] flag as described below.

How to use mod_rewrite in a subdomain's .htaccess?

I have a domain (let the name be) mydomain.com.
I added a subdomain, sub.mydomain.com. (It's an other site, the two sites don't have anything to do with each other)
I have mapped the subdomain (with godaddy.com's tool) to /sub directory. It works well with static file paths, ex. http://sub.mydomain.com/js/script.js.php.
However, I would like to be able to use the rewrite module to get http://sub.mydomain.com/js/script.js
My .htaccess files:
.htaccess in the root directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.mydomain.com$
RewriteRule ^.*$ http://mydomain.com/sub%{REQUEST_URI} [L,P]
.htaccess in the /sub directory:
RewriteEngine on
RewriteRule ^js/script\.js$ js/script.js.php [L]
(I tried adding RewriteBase /sub/, I didn't succeed).
It seems as if Apache didn't notice the /sub's .htaccess
How can I get the rewrite work on the subdomain's paths?
SOLVED!
After a long, exhausting debugging and googling I found an Apache setting that made the trouble:
I added Options -MultiViews and voilá, it works!
/////////////////////////////////////////////
Now my configuration:
-no .htaccess in the root.
-.htaccess in the root/sub:
DirectoryIndex site/index.php
Options -MultiViews
RewriteEngine on
RewriteRule ^site/js/script.js$ /site/js/script.js.php [L]