How set sub directories/folders in public_html 403 forbidden - php

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

Related

Remove public_html from url through htaccess

I have a legacy project on a server where the actual framework is in a folder above the public_html folder. So the directory structure is like this:
domainname.com
app/
framework/
public_html/
index.php
Now I want to place this on our own server. This is an application created by the hosting company. The root folder is default. So now my directory structure is like this:
default/
app/
framework/
public_html/
index.php
Now he's pointing to the default folder instead of the public_html folder. That's the only difference with the old version. So I've added a .htaccess file to the root folder default. The content of this file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
</IfModule>
In my public_html folder I have a .htaccess file like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule . index.php [QSA,NS]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule . /index.php [L,QSA,NS]
So when I go to my website it works. But when I click on a link I get: http://domainname.com/public_html/theclickedpage/.
But I don't want the public_html in my url. How can I fix this?
You may want to search for a way to make a virtual host. I know some of my webhost let me configure the vhost through my admin page. It will let your server consider the url without including the specified folder, but back in the stage it performs needed redirection.
You applied correct rule ,
but the problem is link for the solution of this issue you have to
remove public_html from
your all links and it will automatically redirect to your required
path without public_html.

Remove public from URL Laravel 5.3

I'm using Laravel 5.3 is there a easy way to remove public from URL? is it necessary to add a htacces. when I change the htacces it is getting a n error like below,
copy both htaccess and index.php file from public folder to root directory and then change root index.php code as below
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
After installing Laravel, you should configure your web server's document / web root to be the public directory. The index.php in this directory serves as the front controller for all HTTP requests entering your application.
https://laravel.com/docs/5.3/installation#configuration
Do not edit .htaccess or Laravel related files, just point web server to a public directory instead of Laravel project directory:
DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">
Then restart Apache.
Add a .htaccess file in root which contains -
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
Yes it is very simple no need to add extra htaccess,
Create a new folder in your laravel project folder.
Name it whatever you like. Example :- source
Move all the files from root into 'source' folder except 'public' folder.
4 . Move all the files from public folder to root.
5 . Change the autoload.php path
6 . Thats all. remove public folder if necessary.
In the root directory create a .htaccess file and put the following in
it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Even above solution(s) work(s) for you,but I would recommend not to do this.
Here is the reason why.
-You should be pointing your Apache host root to the $LARAVEL_PATH/public directory instead of $LARAVEL_PATH.
-you have to configure apache for user not access .env, git files , config folder's file
public directory is there for reason to make project a bit more secure. Solve the actual problem and don't try to break this simple but nice security addition.
Copy all files and folder from public folder to root folder and edit your .htaccess file as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And dont forget to change the paths in index.php file.
For details refere following links
Installing Laravel in a subfolder
https://ellislab.com/forums/archive/viewthread/151342/#734511

Apply .htaccess to all subdirectories

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

Codigniter Unable to find model when installed on subdomain

I created my application in XAMPP and it worked fine, now when I have shifted it on my website's subdomain. When I try to open the login page of my application it gives error Unable to locate the model you have specified
Directory Structure on subdomain:
public_html/
Subdomain/
Codeigniter/
application/
models/
views/
controllers/
Login Page of my Codeigniter application is:
On local host
http://localhost/INvoice/index.php/admin/Login_Controller/
On website
http://subdomain.example.com/index.php/admin/Login_Controller/
In xampp for codeigniter I use this htaccess with windows 7 & 8.
Options +FollowSymLinks
Options -Indexes
<FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
Order deny,allow
Deny from all
</FilesMatch>
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I use sub domains on live and that htaccess works fine so long it is in main directory of the subfolder and parent directory. I think because you have deny all which is causing you issue.
Main Directory
Sub Dir Demo
I figured out the problem. Basically I have problems in my file naming convention for models. I have been using Capital letters in File Name i.e. Login_Model.php and when I changed it to login_model.php it is fixed.
Just to you know, My .htaccess is still empty but I have no problem in my URLs.

Avoid public folder of laravel and open directly the root in web server

I was going through all possible sample on internet to solve this. Still it is an headache.
I just want to avoid the 'public' in www.mylaravelsite.com/public/
and make it like www.mylaravelsite.com for the root directory.
Now I do not want to avoid the security concern,So I learned .htaccess would be the best way.
Any solution friends ?
& advance thanks for interacting !
Let's assume you have this folder structure in your server
.cpanel/
public_html/
public_ftp/
..
And the laravel folder structure is
app/
bootstrap/
public/
vendor/
composer.json
artisan
..
You can create a folder name mylaravelsite on your server inline with public_html and public_ftp folder, and copy to it the whole laravel application except the public folder because you will paste all of it contents on the public_html, so you have now:
.cpanel/
public_html/
public_html/packages
public_html/vendor
public_html/index.php
public_html/.htaccess
...
public_ftp/
mylaravelsite/
mylaravelsite/app
mylaravelsite/bootstrap
...
On your public_html/index.php change the following line:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/start.php';
to
require __DIR__.'/../mylaravelsite/bootstrap/autoload.php';
$app = require_once __DIR__.'/../mylaravelsite/bootstrap/start.php';
and also don't forget to change /mylaravelsite/bootstrap/paths.php public path, you might use it.
'public' => __DIR__.'/../public',
to
'public' => __DIR__.'/../../public_html',
Your site should be running.
Create .htaccess in root folder and write these line
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
For production server it is recommended that you point your domain directly to the public folder
It sounds like the information you are missing is:
Documentroot
This is also called the "web root". Apache specifically calls it the DocumentRoot - Click that link for the official explanation.
Basically what it does is set which directory the server pulls files from. In Laravel, the document root should be the public folder.
Setting the DocumentRoot to public means that going to http://mylaravelsite.com in the browser will infact be "pointing" to the public folder as you want.
In the .htaccess file (actually, more likely in the virtual host configuration), you can set the DocumentRoot for your site:
DocumentRoot /path/to/laravel-app/public
How you set this up depends on your hosting. Each hosting has different ways to setup a website and so we cannot answer that specifically for you - you should consult your web hostings tech support. The key point is that the public directory should be the web root, while everything else should be "behind the web root".
Fair warning tho: some hosting providers do not give you enough access to accomplish that.
Just add .htaccess file on Laravel root if it's not present and add following lines in it, that's it,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
create .htacess file in the root directory of your laravel project and put this code in.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
point your web directory to public/. If you are using apache this will work:
<VirtualHost *:80>
DocumentRoot /var/www.mylaravelsite.com/public/
ServerName www.mylaravelsite.com
</VirtualHost>
Here is the solution, But Must not do it in real projects, it is just for starter to test Laravel and i have tested it with laravel 5.0 and it is ,cut index.php and .htaccess files from public folder and paste it in the root directory and change these two lines as
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
It is Highly Recommended not to use the above method without testing environment, and should use virtual host instead.
All you just need to change .env file in your laravel project files. if your laravel project inside in
Root/domain/public_html/laravel/
Go to .env file and edit
you need to edit app url path which should be
APP_URL=http://www.domainname.com/laravel/public/
Thats it your project will run now. if your laravel files is in public_html, then your app url must be like this
APP_URL=http://www.domainname.com
Thanks
For Lumen
Let's think you have a folder path like: /var/www/project/microservice(lumen src)
/var/www => document root for localhost/
Target you want:
localhost/project/microservice[/foo...] => localhost/project/microservice/public/foo...
I do this by .htaccess (placed at /project folder) like below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*/public/.*
RewriteCond %{REQUEST_URI} ^(/[^/]+/[^/]+/).*
RewriteRule ^[^/]+(.*)$ %1public$1 [L,R=301,P]
On Server(Apache)
Create .htaccess in the root folder and write below line
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
Lean more on Gist
On Local Development
You can create a virtual host by adding the below line to your apache vhost config file.
Generally, you can find this file in C:\xampp\apache\conf\extra\httpd-vhosts.conf file
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/mysite/public/"
ServerName www.mysite.local
</VirtualHost>

Categories