Apply .htaccess to all subdirectories - php

I have the following directory structure:
root
folder1
foldera
file.php
folder2
folderb
index.php
.htaccess
I would like to route all requests to index.php and have this apply to all folders. Here is my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
This seems to work fine for localhost/root. The request is correctly routed to index.php. But for localhost/root/folder1 or localhost/root/folder1/foldera/filephp it does not work. Is there an additional rule I need to define for this to work?

From: https://httpd.apache.org/docs/current/howto/htaccess.html
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.
If your htaccess is in the root, why the '../'? As it stands it is redirecting to an index.php above your root directory.
If you want it to work for all requests, remove the two rewrite conditions. Right now it is saying: "rewrite request only if the request is not for a file or a directory that exists".

If you don't want files in other folders to be directly accessible, it probably makes sense to move them outside of the web root entirely. So your front controller would be in a web/ subdirectory, and the webserver's docroot would point at that instead.
root
folder1
foldera
file.php
folder2
folderb
web <- DocumentRoot should point here
.htaccess
index.php

Related

Configuration .htaccess in SLIM Framework

How do I adjust the .htaccess file for my slim-framework?
I have this folders/files:
/src/slim.php
/public/.htaccess
My URL points to the "public" directory. And I want to create a virtual "folder" so if I call http://example.com/api/... it should open my slim application in the src folder.
I have created this .htaccess file, but it doesn't work.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api ../src/slim.php
This error message appears:
My URL points to the "public" directory.
:
The document root is "htdocs/project/src".
It's not clear how your "URL points to the public directory", when your document root appears to point elsewhere? Do you have another .htaccess file in the document root perhaps?
Anyway, it looks like you might be trying to rewrite to a file that is outside the document root? That is not possible using mod_rewrite in a .htaccess context. The server will likely respond with a 400 Bad Request.

How set sub directories/folders in public_html 403 forbidden

I'm using Codeigniter with Hostgator shared hosting.
How to set sub directories/folders in public_html 403 forbidden? but the files in this folders is accessable.
example:
/public_html/this_forlder_forbidden/this-file-not-forbidden.ext
example 1
/public_html/js/jquery.js
/public_html/css/style.css
below is my .htaccess file codes in /public_html/ directory.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
SetEnvIf Origin "http(s)?://(.+\.)?mydomain\.com(:\d{1,5})?$" CORS=$0
Header set Access-Control-Allow-Origin "%{CORS}e" env=CORS
Header merge Vary "Origin"
In the directories that are off limits to visit from a browser:
place a .htaccess file containing:
order deny,allow
deny from all
But if possible, move those directories outside the document root
Addendum
I forgot: for Apache 2.4 the syntax is
Require all denied
instead of the 2 lines for older Apache versions
Go to Codeigniter/application folder
copy index.html
and paste it in /public_html
when someone tries to open /public_html then index.html trigger automatically.
index.html show "Directory access is forbidden."
There is maybe better approach for what you want. Move application and system directories outside public_html and after that just set new paths to those directories in public_html/index.php file that should be:
$system_path = '../system'; // line 93
and
$application_folder = '../application'; // line 110

.htaccess reroute to sub-directory

I have site I am running on localhost http://example and I have directory in the root that contains my api so it's http://example/api
In the api folder I have my root directory for the api which is public directory. so /api/public/....
I want to have a .htaccess that redirects the user to the public directory when they hit http://example/api/{api parameters}. So I can have a clean url without having to write this http://example/api/public/{api parameters}
I tried this but to no avail
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/api
RewriteRule ^api/(.*) /api/public/$1 [L]
I got the above from a SO post but still could not get it to work.
Try using:
RewriteEngine On
RewriteBase /api/public/
Quoting a stack overflow answer, "By having the RewriteBase there, you make the relative path come off the RewriteBase parameter."
With this method I believe you will have to rewrite all paths that don't fall under the base directory. If this is a problem, you could create a rule for each page.
Update: You might be able to write a rule that checked for files at that path before/after it checks for the relative path.

.htaccess Sub Sub-Directory

I would like to know if it's possible to point my domain to a directory which is two folders in from the root.
My hosting company doesn't allow me to change this so I need to do this with an .htaccess file.
This is my structure...
public_html
.htaccess <-- Here will be my .htaccess file.
folder
folder
folder
folder
public <-- My desired folder.
All requests must point to the public folder 2 levels downwhere the frameworks .htaccess file exists and index.php file exists.
I hope this all makes sense.
Thanks.
Use RewriteBase
RewriteEngine On
RewriteBase /folder/public/
RewriteRule . /folder/public/index.php [L]

HTML Directory looking URL

My friend recently told me that I was able to do something like
http://example.com/about/
without having to create a directory and place a index.html inside of it, like this.
http://example.com/about/index.html
How in the world do you do this? I didn't know it was even possible to do unless you created a directory and placed a index.html inside of it, anyway thanks. :)
If you use Apache as WEB server, use .htaccess for you site root directory.
Write following rule here:
DirectoryIndex index.html
Also you can use more than one file here:
DirectoryIndex index.php index.html
In this case first found will be used.
If you don't want to use .htaccess, you can setup same in Apache config file (httpd.conf):
< Directory /foo>
DirectoryIndex index.html
DirectoryIndex index.php
< /Directory>
Here /foo - root directory of your site
Servers like Nginx can allow you to rewrite URLs, so you can place your file wherever you want.
If you use Apache web server then create a file named .htaccess
and write code like below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?load=$1 [PT,L]
If this code when you type a url like www.example.com/about its doing to open your aboutController
Write aabout in this url http://salopek.eu/content/28/create-a-simple-php-mvc-framework
If you does not use Apache or you want simple solution then then you can just create a folder named about and create a simple index.html or index.php file. Then when you type www.example.com/about
it opens your about\index.html or index.php file
I hope it helps

Categories