htaccess for laravel without affecting subdomains - apache

I'm trying to get htaccess working for laravel 5.4, but without it affecting the sub-domains that are created.
My current htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
What I've tried is setting an htaccess in the sub-domain folders with the follwing:
RewriteEngine On
As I read elsewhere this should stop the top domain htaccess, yet when for example I have a sub-domain like dev.example.com, it will redirect to dev.example.com/dev
Anyway of getting rid of the /dev at the end?
Folder structure:
app
bootstrap
config
database
public
storage
resources
routes
dev --> subdomain

Upfront, I don't see how this /dev is related to the directives, you have shown. None of them do anything to add a /dev anywhere. Maybe it's just the subdirectory applied somehow.
The claim "this should stop the top domain htaccess" is not entirely true. From Apache - How directives are applied
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 an .htaccess doesn't stop another .htaccess, but a directive overrides a directive. This means, you may have some directive from one .htaccess and another unrelated directive from the top level .htaccess.
In your case, RewriteEngine on just overrides RewriteEngine on from the main .htaccess file.
If you want to prevent any RewriteRule from the top .htaccess, I would rather try
RewriteEngine off

Related

mod_rewrites in nested .htaccess causing strange 404

I have an apache HTTP server with a directory structure as such:
/
---- api/
---- ---- index.php
---- ---- .htaccess
---- index.php
---- .htaccess
/.htaccess:
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/
/api/.htaccess
RewriteEngine On
My objective was to display the index.php of a directory when it was called without a trailing backslash. However, calling http://example.com/api results in a 404. Commmenting out the one line in /api/.htaccess causes everything to work as expected.
I'm having a hard time understanding this behavior as the doc for RewriteEngine On says nothing about it. Could someone shed some light on mod_rewrite's workings here?
The directory, zipped up: mega.nz
Commmenting out the one line in /api/.htaccess causes everything to work as expected.
Yes, because in a per-directory context, mod_rewrite directives are not inherited by default (which differs to other modules). By enabling the mod_rewrite engine in a subdirectory you are overriding the mod_rewrite directives in the parent directory - and these directives in the parent directory are required to make your http://example.com/api request work as expected.
Note that the DirectorySlash directive is part of mod_dir, so this is not overridden. So DirectorySlash is still Off. This then results in Apache attempting to serve the directory /api, which fails. (Although I would have expected this to have resulted in a 403, rather than a 404?)
The mod_rewrite directives in the .htaccess file in the document root apply to all subdirectories by default, so there is no need to do anything special here.
If you want to inherit mod_rewrite directives from the parent .htaccess file then you need to look at the RewriteOptions directive - but per-directory inheritance with mod_rewrite has additional caveats.
Since I don't want Apache to send a 301 when a request to a directory doesn't end in a slash.
Ah OK. (Those directives are otherwise rather "unusual"!) However, that 301 redirect is intended to "fix" the request. By internally rewriting the request you now have a potential duplicate content issue.

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]

.htaccess - Redirect all to index.php for root folder or subfolder

I need an .htaccess file that will work whether it is put in the root folder or a subfolder without modification. The script below is the normal one that I've been trying to adapt without success. I tried the solution on htaccess rewrite index.php on root and subfolders and couldn't get it to work.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Layout
.htaccess
index.php
subfolder1
- .htaccess
- index.php
The route /blah should go to /index.php and /subfolder1/whatever should go to /subfolder1/index.php. Currently, the above script will send /subfolder1/whatever to /index.php.
[Update]
This should also work for any path under subfolder1, like /subfolder1/1/2/3/idunno.
If you are using Apache 2.2.16 and later, you can just stop using mod_rewrite, which although extremely useful and powerful, can get messy as hell.
A new directive in mod_dir was introduced, FallbackResource which does just that, redirecting to the uri of your choice if there is no hit on the file system. It is available in .htaccess files as long as AllowOverride Indexes is specified for the directories in the configuration.
As .htaccess files are evaluated depth-first, you just have to have each .htaccess file describe your fallback resource in the current directory, and the one in the subdirectory subfolder1 will take precedence:
subfolder1/.htaccess:
FallbackResource index.php
.htaccess:
FallbackResource index.php
They're both the same, and work just right.
It seems this directive is not well known yet even though it's been around for a few years, and its goal is precisely to solve that problem in an elegant way.
There is only one limitation with that setup. Calling urls in non-existing sub-directories of the root dir or subfolder1 will yield subrequest recursion and subsequently an error 500, because the fallback resource is local to the given directory.
The best approach is to have absolute uris (beginning with '/') as parameter to FallbackResource, which is why it is true that the requirement in itself is kind of odd, and is probably not playing too well with the inner workings of Apache.

Apache Rewrite: domain specific gallery images in %{HTTP_HOST} directory

On shared web-hosting my software supports multiple domains (all domains point to the same public_html root directory). Each domain has it's own gallery (in the gallery directory). Since multiple domains can not have their own resources (e.g. images) in the same directory for all sites I have root directories that match each HTTP host name. Here is what the directory structure looks like...
/public_html/
/public_html/.htaccess
/public_html/gallery/
/public_html/www.example1.com/gallery/topic_x/image.png
/public_html/www.example2.com/gallery/topic_y/image.jpg
/public_html/www.example3.com/gallery/topic_z/image.gif
A request to...
http://www.example1.com/gallery/topic_x/image.png
...needs to be rewritten to...
/public_html/www.example1.com/gallery/topic_x/image.png
This needs to be done using the .htaccess file as is noted above, there are no .htaccess files in the HTTP Host matching directories (/public_html/www.example1.com/.htaccess does not exist).
I have been trying with numerous modifications of the following...
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(gallery)(/.+\.(gif|jpg|png))$ %{HTTP_HOST}$1$2 [L,NC]
...without success.
This one works for me:
RewriteRule ^(gallery/.+\.(gif|jpg|jpeg|png))$ /%{HTTP_HOST}/$1 [L,NC]
If you want -- you can add extra check to rewrite only if such final image is present (although I have not tested this):
RewriteCond {%DOCUMENT_ROOT}/%{HTTP_HOST}/$1 -f
RewriteRule ^(gallery/.+\.(gif|jpg|jpeg|png))$ /%{HTTP_HOST}/$1 [L,NC]
But even your pattern should work (at least it passes the test) -- maybe .htaccess is not enabled .. or you forgot to activate rewrite engine (RewriteEngine On)?
I would also recommend adding this line before (or after) activating engine:
Options +FollowSymLinks
or
Options +SymLinksIfOwnerMatch
Some shared hosting may only work with 2nd, for some 1st is enough.

Setting up Drupal and Wordpress under a single document root

I have a hosting account which provides me a folder to publish my files for my domain (say www.example.com). I have set up Drupal for www.example.com with .htaccess at the top folder to enable clean-urls for the Drupal installation. Now I want to have a Wordpress installation under www.example.com/blog/ and have clean URLs for that blog. But while using .htaccess it is not working ok as the .htaccess at the top folder will override the sub-folder one. How to achieve what I intend to?
This really depends on the exact content of your respective .htaccess files.
One workaround is to add a RewriteCond to the head of the main .htaccess file that, if the request URI matches the sub-directory, stops parsing:
RewriteCond %{REQUEST_URI} ^/blog
RewriteRule .* - [L]
this should lead to the blog URLs being parsed properly, based on the rules specified there.