Htaccess with subfolders - vue.js

I break my head with .htaccess 😀
My project is setup like this :
www/
admin/ (vuejs)
api/ (php)
frontend/ (vuejs)
.htaccess
What i want and what is my problem ? 😁
http://example.com has to be my root url and display the website in frontend folder.
http://example.com/admin has to be my backoffice in admin folder
So in my root folder (www), my .htaccess is :
RewriteEngine On
RewriteRule ^admin/ - [L]
RewriteCond %{REQUEST_URI} !^/frontend/dist/
RewriteRule ^(.*)$ /frontend/dist/$1 [NC,L]
⚠ My problem is when i access in my backoffice to the admin route and then admin/users route for exemple, i have no problem because i'm on the vue js route application.
But if i want go directly to http://example.com/admin/users, as you can see my .htaccess, my frontend application catch the route and i have an error.
if someone explain how can i handle this, it will be amazing.
Thanks a lot and have a nice day.
Camille

Related

After deployment of vue app only homepage works, subpages not found

I have created a subdomain test.example.com from cPanel. I then built my Vue app using
npm run build
and zipped the whole dist folder to upload to the server. Once uploaded I expanded the zip file so the files are on the server.
This works perfectly for my home page, check, however, any of the subpages return the 404 while this issue is not observable on the localhost.
I don't understand how to fix this.
I use the vue.config.js with
module.exports = {
publicPath: '/'
}
and also the links to subpages look fine but just don’t work.
You are using history mode in your router, in order for it to work all requests need to go back to index.html
All you need to do is add a simple catch-all fallback route to your server. If the URL doesn't match any static assets, it should serve the same index.html page that your app lives in.
Since you are using cPanel I would assume that your server is using Apache.
Therefore you will need to create a .htaccess file to redirect all requests to index.html. You will need to create the .htaccess file inside your public_html folder (assuming that is where your application code lives). Then add the following.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

Single page app in sub directory, working html entry point but 404 on assets (css, js, ...)

Im my Apache root directory a have 2 things, a .htaccess and a directory named app. Inside that directory there is a single page app. In the .htaccess I have:
RewriteEngine On
RewriteRule ^$ /app/ [L]
If I go to mysite.com, I can see the index document working, but all the assets called by index.html are missing:
And in fact if I try to query them directly from the browser they all go in 404, like mysite.com/vendor.js. What am I missing?
Converting my comment to answer so that solution is easy to find for future visitors.
You may use this rule to forward all request to app/ folder:
RewriteEngine On
RewriteRule !^app/ app%{REQUEST_URI} [L,NC]

Unable to access sub-domain using .htaccess in my Laravel5.8 site on cpanel shared hosting

I got a Laravel 5.8 site hosted on shared hosting. I want to access my subdomain using .htaccess file but I'm unable to figure out how to make it work.
My directory structure looks like this:
public_html/
cdn/ (subdomain directory)
index.php
public/ (assets and index.php file reside here)
css/
js/
images/
fonts/
index.php (loads laravel site)
.htaccess (responsible to load index.php from the public directory)
So, basically, right now, if I try to access cdn directory like this http://example.com/cdn, that takes me to http://example.com/public/cdn and if I try http://cdn.example.com, the browser gives me the error message that This site can’t be reached.
I tried this but it does not work.
RewriteEngine On
# Handling subdomain
RewriteCond %{HTTP_HOST} ^(www.)?cdn.example.com$
ReWriteRule ^(.*)$ cdn/$1 [L]
# Loads index.php located inside laravel's public directory
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
ReWriteRule ^(.*)$ public/$1 [L]
When I try to access, http://cdn.example.com, the browser gives me the error message that This site can’t be reached.
It supposed to load index.php file located inside cdn directory.
Please help me out.
Thanks in advance!

RewriteRule for a file in a parent directory

I've got a site set up on an apache server with the desktop site in /public_html and a mobile site /in public_html/mob
I'm trying to set up an .htaccess rewriterule to send users to an index.php file in /public_html if they visit the /mob folder. My current rewrite rule, in the mob subfolder is:
RewriteRule ^(/)?$ ../index.php
I can load up the same file in the mob subdirectory with:
RewriteRule ^(/)?$ index.php
However I can't seem to get the site to load the index.php file from the parent directory (public_html).
When attempting to load http://www.domain.com/mob in a browser I receive:
Bad Request
Your browser sent a request that this server could not understand.
This same rewriterule worked fine on our development server, but doesn't work in our live environment.
The .htaccess file from the /public_html/mob folder is as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(/)?$ ../index.php [L,QSA]
When index.php is reached a mobile device detect script decides whether to load the content from the desktop or mobile site.
Check DOCUMENT_ROOT of your m.domain.com.
If DOCUMENT_ROOT for your m.domain.com is /public_html/mob then you cannot load /public_html/index.php without doing a full redirect (or proxy) to http://domain.com/index.php
Just to clarify any web site cannot access files above its DOCUMENT_ROOT folder level.
If your rule's target starts with a /, that makes it an absolute URI starting from the document root, which I assume would be the public_html folder:
RewriteRule ^(/)?$ /index.php

Redirecting to a new site unless in a specified directory

I run an image hosting site. Lets just go with the following information.
Site: imagehosting.com
Tiny: imgho.st
Directory: n/
Directory is where the images are stored. Anyways. I'm trying to figure out an apache rewrite method to redirect imgho.st to imagehosting.com UNLESS in the n/ directory. So unless the user is imgho.st/n/83md.png redirect to imagehosting.com.
Could anybody help me out with this?
Thanks!
You can put RewriteCond before your RewriteRule
RewriteCond %{REQUEST_URI} !\/n\/.*$
RewriteRule ...