Overriding an htaccess file within a sub-folder - php

I'm using Codeigniter, and I've separated out the resources (CSS, JS, Images, etc.) out of the Application folder, into a folder like so: root/resources. CodeIgniter has the typical htaccess file in the root folder, and the contents are:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /index.php?/$1 [L]
If I wanted to allow resource files to live inside a sub-folder, inside the application folder, like so: /application/plugins/some_plugin_name/resources. What contents would I put inside the htaccess file inside the above resources folder that would allow me to access CSS, JS, and other resources files like images, etc.?
This is in regards to future plugins as well, so the "some_plugin_name" folder can be anything.

You just need this:
RewriteEngine On
By turning on the rewrite engine inside a subfolder, its rules (which are none) will take precedence over any rules in the parent directory.

Jon Lin was close, but there is an additional rule to add onto it. Here's the complete htaccess file that works inside the resources folder. You may want to add it to any folders below the resources folder (but I may be wrong there):
RewriteEngine On
allow from all
The reason I posted my own answer is because it truthfully took me like 30 minutes to find the answer to it.

Related

How to redirect subfolders to read all codes from the main directory? similar source codes in subfolders but different contents

I'm developing a website which has three sub folders in the main directory as /a/, /b/ and /c/. Contents in main directory like site.com, site.com/a/, site.com/b/ and site.com/c/ are different; however, the codes and files are completely similar. In order to reduce the volume of the codes, I want to find a way to delete all code files in my sub folders and so all requests to be responded by the main directory files while I keep the sub folders. Could you please give me your opinion about changing the index.php, .htaccess or etc to solve this problem?
You can do this, for example:
RewriteEngine On
RewriteRule ^(a|b|c)/(.*) $2?folder=$1 [L,QSA]
This will make all requests to a/smth, b/smth, c/smth be rewritten to smth (in the root directory) and a/b/c passed as query-string parameter 'folder'.
However, when you access static files like this, a/image.png, b/image.png (for instance) are still considered different uris - and as such will be downloaded separately by the browsers (instead of caching). So you should consider treating resources in a different way. for example, make a separate folder for static resources and address it directly from each subfolder.
For more information, read mod_rewrite manual
Make sure sure there is not .htaccess in /a/ OR /b/ OR /c/ directories
Place this rule in root .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[abc]/(.+)$ /$1 [L,NC]

How to block access to all .php except index.php in root folder (via .htaccess)?

Note: This question has been asked before several times, but the answers are really bad, totally wrong and/or do not fit the above scenario (because there are several files called index.php). If you like, see [1].
I want to block direct access to all .php files in the application folder (see file structure image) via the .htaccess file in the root folder. There are some solutions for this on the web, but they miss one thing: They don't work if there is more than one file named index.php (which is a realistic scenario like the screenshot shows, see the file in the view/xxx/ folder):
Question: How to block access to all .php files, except the index.php in the root folder ?
In .htaccess:
RewriteEngine on
RewriteRule ^/application - [F]
The [F] option instructs it to issue a 403 Forbidden response on all matching URLs.
Or add a separate .htaccess file in /application containing just:
deny from all
Or in your Apache vhost definition:
<Location /application>
deny from all
</Location>
In addition to Niels Keurentjes excellent answer I would like to extend his solution according to my .htacces that uses some very common rewriting patterns (as a lot of people might run into the same problem):
When using URL rewrite rules, then the line RewriteRule ^/application - [F] has to be at exactly that place. It will not work if the line is placed before or below:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# The new line, blocking direct access to every file in /application and deeper
RewriteRule ^/application - [F]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

mod_rewrite trouble

So I'm using a simple rewrite rule like:
RewriteEngine On
RewriteRule ^foo/?$ foo.php [NC,L]
It redirects perfectly to foo.php, but it seems like all links and images in foo.php are being taken from folder foo on the server which doesn't exist. For instance, 1.png will now be foo/1.png or index.html will now be foo/index.html. So my question is: is there any way to make thing right without changing paths to the files in foo.php?
Your visitors' browsers see the current page as being at /foo/, thus all relative URLs will be resolved under /foo/. You will need to set the base URL, or update all your relative URLs to point to your site root (e.g. do not use relative/path/url.jpg but /relative/path/url.jpg).
In your code you should provide a rewrite rule for your resources (images, css, etc...) or add conditions for real files / folders like...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
The two RewriteCond lines test to see if the requested URL points to an actual real directory (the !-d part), and the second one tests if it's a real file (!-f)
In the future, you can easily debug your mod_rewrite stuff by adding this two lines to your .htaccess file:
RewriteLogLevel 3
RewriteLog "/path/to/a/file.log"
2 simple ways
absolutize the references as suggest by Ianzz
remove the foo path for referenced object still using htaccess
RewriteRule ^foo(/.*.(jpg|html|gif|css))$ $1 [L]
I prefer the 2nd solution because the htaccess do the mess and htaccess fix the situation, and no changes to your code are needed.

.htaccess for site in sub-directory using Yii framework

I have looked at several examples of htaccess configs for websites within sub-directories, and tried most of them without 100% success.
My setup is:
using Yii framework
htaccess at public_html/.htaccess
site located inside public_html/mysite directory
index handling all requests located at public_html/mysite/frontend/www/index.php
The status of the URLs:
www.mysite.com works fine [ok]
www.mysite.com/controller/action shows me the homepage [wrong]
www.mysite.com/mysite/frontend/www/controller/action works fine [wrong, the item above should work instead]
My .htaccess at the moment looks like this:
AddHandler application/x-httpd-php53s .php .html
Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$
RewriteCond %{REQUEST_URI} !^/mysite/frontend/www
RewriteRule ^(.*)?$ /mysite/frontend/www/index.php [L]
I have tried everything, but I have no idea why www.mysite.com/controller/action won't work :(
Any help would be really appreciated! Thanks!
I found the answer to this similar question to be helpful. Here is how my rewrite rules ended up:
#Forward all non-existent files/directories to Yii
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) subdir/index.php/$1 [QSA,L]
This takes all non-existent files/folders and sends them to the yii script with initial url appended. QSA appends any query string that may be present in the initial url.
You didn't mention if you configured Yii's Url Manager for clean URLs. You need to, otherwise Yii expects the "route" to appear as a GET param named "r". If you didn't, consult this section of the definitive guide
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory
I also didn't update the .htaccess file, easier to modify the httpd.conf virtual host for the subdomain and change the DocumentRoot to point to your yii folder.

URL Rewriting and actual directory names matched by regex

I created an application using the Front Controller pattern, so basically everything is sent to index.php
This is what I currently have in my .htaccess file
RewriteRule ^([a-zA-Z0-9]+)(\/([a-zA-Z0-9]+))?(\/([a-zA-Z0-9\/]+))?(\/)?$
index.php?class=$1&method=$3&args=$5
(I had to break the line into two, for presentation but you know what I mean)
So it works fine however if I create a directory named /js for example that has to be directly accessible. That means, I can't put an .htaccess file inside it and put:
Deny from all
Then accessing my site using the URL http://mysite.com/js redirects to http://mysite.com/js/?class=js&method=foo&args=
Putting Options -Indexes in the main .htaccess file doesn't really help. Any thoughts?
Any clarifications are welcome, I'm not really an good at explaining things.
Thanks
Use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Exclude real files and directories from the redirection rule (redirection rule will not apply to the real files and directories).

Categories