PHP public folder - php

I have been seeing several PHP online tutorials where they recommend that you have a public folder where you have your index.php and other views, css, js, etc and another folder (usually called app) where you have your classes and such. The idea is for the site to use the public folder as default so no one will be able to access the classes. The question is, once I upload the public and app folders to my server, how do I get the users to public/index.php when they type in mypage.com (without them having to type mypage.com/public).

You can use this .htaccess file, Place it in root folder of your website:
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /public/$1 [L]

Related

Access files outside public folder in Codeigniter 4

I am working on multi tenant application in CodeIgniter4 in which I have a folder on root of my domain that caters to specific tenant like http://example.com/test and http://example.com/test1
I have implemented the multitenant architecture by placing the contents of public folder in tenant specific folder (test and test1 in my example). each tenant having its own set of files (which are usually kept under public folder).
Screenshot of current directory structure
I need to store some common Javascript, css and images files accessible to all the tenants.
Add this to your public/.htaccess file to create a fallback for common files.
If a request is made for /test/app.js, and the file does not exist, then this rule will try to serve /app.js instead.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/(.*\.(css|js|jpg|jpeg|png|ico|txt))$
RewriteRule (.*)/(.*\.(css|js|jpg|jpeg|png|ico|txt))$ /$2 [L]
Add this above the rewrite rule for index.php.

How to remove 'public' from Laravel URL (v5.6) when Laravel is installed in a subdirectory

I'm using WordPress for my business site and Laravel for Project management application.
Here is my directory structure.
public_html
-mainsite (my WordPress site is in this directory)
-work (Laravel Application is in this directory)
When user visits my primary domain (e.g. www.example.com) I'm redirecting the visitor to www.example.com/mainsite using following .htaccess rules
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/mainsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mainsite/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ mainsite/index.php [L]
</IfModule>
My project management application which is located in 'work' directory is easily accessible with this URL
www.example.com/work/public
Now I want to allow users to access my project management application with this URL
(I want to remove 'public' from URL)-
www.example.com/work
Could you please help me to resolve this issue?
(It is easy to remove 'public' from URL when Laravel is installed in the root directory and I know how to do that)
This is not a duplicate question and here is the reason-
My Laravel installation is in subdirectory of my root directory and I'm also using different site (a WordPress site) for my base URL
Move Laravel to outside of public_html
Move content of Laravel's public folder to public_html/work
Put the following code in the \App\Providers\AppServiceProvider register():
$this->app->bind('path.public', function() {
return base_path('public_html/work');
});

htaccess - Redirect parent folder PHP script

I have this web api project which is developed by other company. The file structure is:
/project
---/app
---/ApiEndpoint.php
---/public
---/index.php
The DocumentRoot is pointing to /project/public. The index.php is working (http://myapi.com/), however when I try to browse into the api endpoint http://myapi.com/api/endpoint I got 404 error.
How do I configure the .htaccess to rewrite this condition?
/project/public/.htaccess config
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
</IfModule>
The current configuration is there to make sure every request go to index.php (that is probably dispatching requests in some way) except for the files that actually are in the public directory (probably needed for static files like images, and such).
If you wrote a php yourself to be directly called by http://myapi.com/api/endpoint, you should put it in /project/app/public/api/endpoint/index.php.
BUT I suspect you should study that application more and understand the current dispatching method, before doing that.

htaccess for domain and subdomain with laravel

I'm trying to figure out how to set up a domain and subdomain to work on a shared hosting account. It is a Laravel 5.1 application.
My access file is
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ index.php [L]
I just purchased another domain and added it on, but I get a 500 error. I renamed the access file and then it worked. So it has something to do with the access file. Essentially I want two separate domains with and I'm wanting two separate laravel applications, one for each.
I'm not familiar with atacceess.
Maybe, you get a redirect loop, because the rule isn't protected by a condition. Although the default htaccess of Laravel should already contain them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Olaf Dietsche's answer does work for me.
Here is another thing I came upon a website that also worked just before I saw his post. I guess I was reading that this would send a 404 to that directory.
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?main-topdomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain-folder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
So along with this comes another question if anyone is in my boat. I have my subdirectory inside of my root directory.***
dlaugh.com/public_html
laravel folders and access**
inluding
app
bootstrap
config
database
etc...
but also I have my sub folder
app
bootstrap
config
database
etc...
**mysubdomain in that folder**
Is it better practice to put
-main_domain_folder and
-subdomain_folder
in public_html
and then the
/app
/config
/database
would be in the main_domain_folder rather than passing the subdomain through the main domain?

Removing index.php from Code Igniter

I've done a search for my question but have yet seen one similar to mine.
I have a newly installed CI on my server, for security reason I have shifted the "application" and "system" folder outside of public_html and into a folder called "app". I tried a lot of methods that the forums were using but none seem to help.
Below is my folder structure, would like to know what to type for the .htaccess and where does this .htaccess go to?
/
app (system and application folder for CI resides here)
public_html (index.php - the one which defines ENVIRONMENT, styles, other htmls and my current .htaccess resides here)
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$1 [PT,L]
Please help! Thank you for your time!
EDIT
Currently my url is something like this
xxx.xxx.xxx.xxx/~app/index.php/user/check/id
(it's on a shared hosting but not linked to domain yet and hence the ip address and partition in the url.)
I want to achieve something like
xxx.xxx.xxx.xxx/~app/user/check/id
and it should work when I link up my domain. (e.g. www.mydomain.com/user/check/id)
Alright, I think I've solved the problem.
This has most probably got to do with the 'unique' url that I have. I assume if it is a normal domain name it shouldn't have so much problems.
My url > xxx.xxx.xxx.xxx/~app/index.php/user/check/id
My directory structure remains the same >
/app (application and system directories goes here)
/public_html (index.php and .htaccess goes here)
.htaccess code as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~app/ << NOTE the addition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
In config.php, set
$config['base_url'] = 'http://xxx.xxx.xxx.xxx/~app/';
$config['index_page'] = '';
That's all!

Categories