Unable to open subdirectory laravel installation - laravel

In my /public_html/ I have installed an WordPress site. Now I have installed an laravel application inside /public_html/app/.
Then in /public_html/app/.htaccess I have added:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
In /public_html/app/public/.htaccess I have added:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /app/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
</IfModule>
When I trying to open https://example.com/app/ I've got error 404. When I try to open directly https://example.com/app/public I've got half working site because it is searching for the css/images in https://example.com/.
What is need to be changed in the htaccesss in order to work.
The goal is to have a button on the main WP site and when I click it to load the laravel site.

RewriteBase /app/
You need to remove the RewriteBase directive. This ends up rewriting the request to /app/index.php, when it should be /app/public/index.php. The default is to rewrite to the current directory (the directory that contains the .htaccess file), so the RewriteBase directive is not required here.
(Or, you could set this "correctly" to RewriteBase /app/public - but that is not necessary and would then hardcode this installation to the /app directory.)
I've got half working site because it is searching for the css/images in https://example.com/
It depends on where your images are. If images are located at /app/public/assets/images/myimage.jpg then you should be referencing your images using a root-relative URL (starting with a slash), excluding the public directory, eg. href="/app/assets/images/myimage.jpg".
UPDATE:
is this means that I now have to manually edit all images, links, buttons, etc on the site in order to add /app/... in front of the assets?
Ordinarily, yes. In the same way you have presumably added /app/ before all your internal links to your pages. (?)
Logo for example - <img src="/assets/main/img/logo.png">
However, since you are using a root-relative URL and your Laravel assets are in a known location, then you could workaround this by implementing a rewrite in the root (WordPress) .htaccess file to rewrite your Laravel assets to the correct location (presumably /app/public/...).
However, this does mean that you cannot then have an /assets subdirectory in the root (without implementing additional filesystem checks), nor have a WordPress URL that starts /assets, since it will conflict and won't be accessible.
For example, at the top of the /.htaccess file, before any other WordPress directives you could do something like the following:
# Rewrite Laravel assets to the correct location
RewriteRule ^assets/.+ app/public/$0 [L]
# BEGIN WordPress
:
Where $0 is a backreference that contains the entire URL-path that is matched by the RewriteRule pattern.
Now, any request for /assets/<something> will be internally rewritten to /app/public/assets/<something>. /assets/ itself won't be rewritten.

Related

.htaccess rewrite to index.php or index.html based on condition

I'm not so good with htaccess and tried to find an answer to my question but no luck so far.
So I have this .htaccess rewrite:
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php
Which works well.
The website is an Angular site where I have dynamic URLs which are routed by JS.
So if I open base domain: example.com works well because index.html is served.
But if I open a route like: example.com/example-route. It says 404.
Could you please help me how should I modify the .htaccess file?
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php
You would seem to just need to rewrite the request to index.html after your API rewrite to index.php. However, you should modify your existing rule to add the L flag and the regex that matches the request should be anchored (although the condition is not required at all since the URL check should be performed in the RewriteRule directive itself).
For example, try the following instead:
# "index.html" needs to take priority when requesting the root directory
DirectoryIndex index.html
# Abort early if request is already "index.html" or "index.php"
RewriteRule ^index\.(html|php)$ - [L]
# Rewrite certain requests to Laravel API
RewriteRule ^(api|nova|nova-api)($|/) index.php [L]
# Rewrite everything else to Angular
# (unless it already maps to a file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Since the "homepage" is already working OK, it would suggest DirectoryIndex is already set OK in the server config (and prioritising index.html), although explicitly setting this to just index.html (as above) is more optimal, if this is all that's required.

Deploy Laravel App

I'm trying to deploy my Laravel app and block the access to the others files like .env
I put all my laravel app in the www folder, and I add this htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But when I go to my domain url I have all the files.. seem like my htaccess is not working (he's on the Laravel app root)
Here's a simple method using only a .htaccess file placed in Laravel's root directory - e.g. alongside app, bootstrap, config, ... No changes whatsoever are necessary to your code.
The file rewrites all the requests so that requesting /file.png would in fact return /public/file.png and anything else is routed to /public/index.php. This also ensures that nothing outside the public folder can be accessed, thereby protecting any sensitive files like .env or database/*.
The simple method
This method assumes that DOCUMENT_ROOT is set properly by your Apache server. Try this first and use the 2nd method only if it doesn't work on your server.
.htaccess
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond %{DOCUMENT_ROOT}public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
The slightly more complicated method
If your server doesn't set DOCUMENT_ROOT properly, then you'll need to use an absolute path in RewriteCond. That is, an absolute path on the server's filesystem. You can get it by copying the following script to the directory where your Laravel installation will reside and visiting its URL - i.e. http://example.com/get_doc_root.php.
get_doc_root.php
<?php
echo getcwd();
This should result in something like /var/www/example.com/web. Use the following .htaccess file and replace [[your path]] with the actual path you got.
.htaccess
RewriteEngine on
# serve existing files in the /public folder as if they were in /
RewriteCond [[your path]]/public%{REQUEST_URI} -f
RewriteRule (.+) /public/$1 [L]
# route everything else to /public/index.php
RewriteRule ^ /public/index.php [L]
In our example case, the RewriteCond line would look like this:
RewriteCond /var/www/example.com/web/public%{REQUEST_URI} -f

Removing index.php from URL laravel 5.4 when virtual hosts points to public folder

There are countless posts here and elsewhere requesting help in taking out the public/ and index.php from a fresh laravel install on a live server. So many in fact that I'm a little embarrassed to ask this. However, Given that there is more than one way to skin a cat, I feel that the many different solutions can sometimes conflict with each other and cause more headaches.
I have taken out the "public" in the url from a recently launched laravel site by pointing the virtualhost domainname.com.conf to the public folder.
This however causes the need to put index.php after the domain in all requests or else the page doesn't work correctly.
When I do put index.php into the urls manually, some of the images can't be loaded since they don't have this hardcoded.
Furthermore, if I point the virtual hosts addresses directly to public/index.php, the site works but after about 10 minutes it stops working and nothing on the site will load, css, js images. It all stops. I assume this has somemthing to do with laravel view caches but clearing them doens't change any thing.
At this point I'm at a loss. I've put +8 hours into this off the clock and am getting pretty desperate.
This is my htaccess folder in public(no htaccess in laravel root is needed?)
<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>
This is the same setup I've used for other domains & laravel live sites with success. For some reason the index.php simply won't go away using anyone's solutions.
Any help? Have I been staring at the same htaccess/virtualhost files for too long?
Seems like you don't have the DirectoryIndex properly set.
Check your configuration, in my case I got it on dir.conf file:
file: /etc/apache2/mods-available/dir.conf
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

I want to keep the assets folder inside the application folder not outside

My question as in the title
I want to keep the assets folder which contain (css, js, images) inside the application folder not outside.
If I keep the assets folder inside the application folder, I can't access to the files that inside it.
.htaccess file code
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
The standard Application structure:
Application
app
assets
sys
.htaccess
index.php
The previous structure has no errors.
I want the structure to be like the following:
Application
app
assets
config
controllers
......
sys
.htaccess
index.php
How can I do that ?
Your problem is that the .htaccess file you're using is rewriting the URL of your static assets, breaking the links, so you need to change it.
If you keep your assets/ folder inside the root of the application and amend your .htaccess to something like this, that should solve the issue:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
The change is on the second line - essentially the condition is "If the route doesn't start with index.php, assets, or robots.txt, then rewrite it to include index.php at the start". The addition of assets should solve the issue.
EDIT: By default, CodeIgniter has an .htaccess file inside the application/ folder that denies access to anything inside it. If you really, really want to be able to serve your static files from that folder, you can amend that file. However, it would be a really bad idea. Your static assets should live in the web root, and trying to serve them from the application folder will be fraught with problems - if you allow access to the application/ directory from outside, it could cause some serious security issues.

mod_rewrite with codeigniter

im using shared hosting. i build my web application with codeigniter and my new web directory will be as follows:
/home/projName/public_html/proj_v3/
under this dir, there are index.php, .htaccess. as shown above, i separated different project version in different directories (ie: proj_v1,proj_v2, proj_v3). the web applications allows photo upload and it's stored in /home/projName/public_html/proj_v3/application/uploads. proj_v2 currently has the latest uploaded photo since it's still in use. because i have different version of projects, i want to place directory uploads in a common directory such as /home/projName/public_html. so that for any new version of projects, i won't have to move the upload folder to the new project folder. this might cause downtime.
currently, the .htaccess file in /home/projName/public_html/proj_v2 is:
Options -indexes
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1/ [L]
i have another .htaccess in /home/projName/public_html/ is:
Options All -Indexes
RewriteEngine On
Rewriterule ^(.*)\.*$ proj_v3/index.php/$1/
RewriteOptions MaxRedirects=3
basically how should i go about editing the 2nd .htaccess file in /home/projName/public_html/ so that if the web directory is domain.com/uploads/image.png, it gets the image from /home/projName/public_html/uploads. currently, the 2nd .htaccess file tells domain.com to point /home/projName/public_html/proj_v3
RewriteRule \.(jpe?g|gif|png|wmv|bmp)$ /uploads/$1
Might work for you. You can add other formats as needed.
What you want to do is allow the paths to real files and folders to exist, while redirecting everything else to CodeIgniter, and you can achieve this using this in your second .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Definitely worth reading this article on the CodeIgniter website about mod rewrite
I actually gave a similar answer before: mod_rewrite in Users/<username>/Sites directory on OSX

Resources