.htaccess to point to correct Laravel installation - .htaccess

In my document root, I have deployed two Laravel applications to two different subfolders. (i.e. http://example.com/dev and http://example.com/test). Here dev and test are the subfolders.
I want http://example.com/demo/task/132 to be directed to http://example.com/demo/public/task/132.
\[document root]
-- \dev
-- [laravel home]
-- \test
-- [laravel home]
Is there a way to handle public folder in the paths using .htaccess? Basically I want to get rid of public from the URL.

Can you try this :
dev/public/.htacess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dev/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
test/public/.htacess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

Related

Configurating .htaccess file for my CakePHP 3 app located in a git repo subdirectory

Found a similar question
Cakephp 3 .htaccess File Configuration
but no answer there.
My GIT repo structure
ROOT
- .htaccess
- /htdocs (CakePHP 3 app)
- - .htaccess
- - /webroot
- - - .htaccess
ROOT .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ htdocs/ [L]
RewriteRule (.*) htdocs/$1 [L]
</IfModule>
htdocs .htaccess (default)
# Uncomment the following to prevent the httpoxy vulnerability
# See: https://httpoxy.org/
#<IfModule mod_headers.c>
# RequestHeader unset Proxy
#</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(\.well-known/.*)$ $1 [L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
htdocs/webroot .htaccess (default)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
https://***.com/bestprice.com/ (GIT repo location)
I am actually redirected to htdocs (htdocs not displayed in url, just like I need), but get a Missing Controller error.
Bestprice.comController could not be found.
https://***.com/bestprice.com/htdocs/ works fine
I have tried many different ways and this is the best result I could achieve.
Could you please tell me what I am doing wrong? Thanks a lot for your help

How do you redirect all request to public/ folder in laravel 5

I have a classic Larevel 5 project structure and I need to redirect all requests to public/.
I am on a classic hosting environment so public/ is a subfolder of my document root.
I shall imagine it can be done via .htaccess but I still need to figure out how. Anyone can help?
Thanks
There are two solutions:
1. Using .htaccess with mod_rewrite
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
2. You can add a index.php file containing the following code and put it under your root Laravel folder (public_html folder).
<?php
header('Location: public/');
You don't need to change anything in Laravel's default public/.htaccess file.
Just create a new .htaccess in the same level your public folder and add the following content to it:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
That simple!
This is an extract from another answer which may also help you.
--
Modify your public_html/.htaccess to redirect all requests to the public subfolder.
# public_html/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect All Requests To The Subfolder
RewriteRule ^ /public
</IfModule>
Make sure you have the proper public_html/public/.htaccess (GitHub).
# public_html/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization}
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
If you use cPanel, then:
1.Go to folder:
/var/cpanel/userdata/my_domain
2.Edit the both domains:
my.domain and my.domain_SSL
Add to the documentroot section /public:
documentroot: /home/user/public_html/public
3.Rebuild Apache config:
/scripts/rebuildhttpdconf && service httpd restart
if you using apache , add .htaccess file to your root directory with this lines :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
and make sure your apache redirect and rewrite modules is working fine .
you can use vhost to redirect the all request to your public directory with set public as root of your project .
The ideal scenario is to have /home/user/public as a symlink from /home/user/laravel/public.

CakePHP [.htaccess], static files as the root of project

I would like to be able to access static files at the root of my project, in a folder at the same level of app/. It's because that is the only directory to wich I have the permission to read-write files on my server, so our images are uploaded there.
So if someone writes this URL:
www.mysite.com/img-rw
It displays images in the folder at [project-root]/my-rw-dir
Any ideas on how to edit .htaccess files for something like this to be done?
Thanks
Taking into account that this is a course project (as the OP said in comments) and you are NOT concerned about security flaws, here is what you could do.
By default CakePHP has 3 nested .htaccess files:
/.htaccess (Project root folder)
/app/.htaccess
/app/webroot/.htaccess
The first (topmost) of them is the first to respond when you try to access www.mysite.com:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Clearly, this sends the user request right into the /app/webroot folder. We could use the conditional check that exists in /app/webroot/.htacess to, instead of promptly redirecting to webroot, check if the given URL matches to a file or folder. In order to do so, you'll need to update the parent (/.htaccess) file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d # Checks if the given URL matches to a folder
RewriteCond %{REQUEST_FILENAME} !-f # Checks if the given URL matches to a file
#RewriteRule ^$ app/webroot/ [L] # This line is commented, should be removed
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
This way, the request will only be redirected to /app/webroot if none of the conditions are met. If you need only to return files, than you could remove RewriteCond %{REQUEST_FILENAME} !-d, which verifies if the URL matches to a folder ( -d = directory).
A cleaner version that would only check for files should look like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
With this tweak, accessing www.mysite.com/image.jpg should return the file image.jpg hosted at your project root (/image.jpg).
For anyone wondering, this works great :
.htaccess (project root)
<IfModule mod_rewrite.c>
RewriteEngine on
# This folder is a subfolder of project root but contains static images
# Accessed like so: www.mysite.com/imgrw/[file name]
RewriteRule ^imgrw/(.*)$ /dir-at-root-level/img/$1 [L]
# Everything else is handled by cakephp webroot
RewriteCond %{REQUEST_URI} !(dir-at-root-level) [NC]
RewriteRule ^(.*) app/webroot/$1 [L]
</IfModule>

Redirection to Desired Url

I have a url like:
http://x.x.x.x/~mmi/
which maps to the public_html folder on my site. I have installed cakephp on my site. The default htaccess file for cakephp looks like below:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I want to be redirected to:
http://x.x.x.x/~mmi/app/webroot/
instead of :
http://x.x.x.x/app/webroot/~mmi/
which is happening now. How should i modify htaccess file to get this desired result?
Following does the trick:
RewriteEngine on
RewriteRule ^~mmi/(.*)$ http://x.x.x.x/~mmi/app/webroot/$1

Exclude a root folder from CakePHP app

I am trying to exclude a first level directory from Cake (root folder app), as it should hold a different app.
/ holds the app
/development/ holds the tested version of the app
The directory structure:
Public folder
.htaccess [modified]
app
.htaccess [unmodified]
webroot
.htaccess [unmodified]
index.php [unmodified]
...
...
lib
Cake
...
development
app
webroot
index.php [dumps $_SERVER for test purposes]
So, my development structure still doesn't have an app inside (nor .htaccesses), just to test if my root .htaccess works.
This is my modification of the root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/development.*
RewriteRule (.*) - [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
What happens:
/development/ shows the apache index of development folder
/development/app/ shows the apache index of app folder
/development/app/webroot shows the root app (request is captured in spite of the development url match).
/development/app/webroot SHOULD show me my /development/app/webroot/index.php file, right?
What the hell am I missing here?
This turned out to be an oddball bug on my server.
The resulting .htaccess which works now is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/development(?:$|/)
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/development(?:$|/)
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
For reasons unknown to me, if I SSH'd to my user account and edited the .htaccess files through the command line, it wouldn't work!
When I uploaded the same files (via SFTP), it started working properly.
I am using cPanel (11.34.1) on CentOS (5.9).
Another example: if you wish to "ignore" multiple sub-folders on a CakePHP installation, you can use this pattern:
<IfModule mod_rewrite.c>
RewriteEngine on
# /development, /version1.5 and /version1.8 directories
# they all hold independent apps
# or completely non-cake-related content
RewriteCond %{REQUEST_URI} !^/(?:development|version1\.5|version1\.8)(?:$|/)
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/(?:development|version1\.5|version1\.8)(?:$|/.*)
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Note that both RewriteCond instructions are identical.

Resources