.htaccess to VueJS in non-root directory - vue.js

I have a problem with .htaccess for VueJS. I'm using the example available in the official documentation.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
It turns out that this example works perfectly in the root folder, but my front-end is in another folder, because on the root folder is the site's home page, which was not developed with VueJs. So I'm creating inside the /admin folder. The directory hierarchy looks something like this:
/
|_ /index.php
|_ /.htaccess (to full site)
|_ /admin
|_ /.htaccess (to vue)
|_ /index.html (vue page)
The problem is that if I access the browser https://example.com/admin works perfectly, but https://example.com/admin/user works only by clicking on the link contained in site body, which leads to this URI, but it does not work with direct browser access. Only the index.html page accepts direct access from the browser. Does anyone have a suggestion on how to solve this problem? Try putting the .htaccess in the root folder and not in /admin folder, but it didn't work. I also tried replacing the RewriteBase / line to RewriteBase /admin but it didn't work.
The .htaccess file in root folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Basically removes the .php extension.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
If this .htaccess file is located inside the /admin subdirectory and /admin is part of the URL then it should be written as follows, otherwise, it will try to rewrite the request to /index.html in the document root, which presumably does not exist.
DirectoryIndex index.html
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
The changes made:
Remove the RewriteBase directive entirely.
Remove the slash prefix from the RewriteRule substitution string. ie. /index.html becomes index.html. Rewriting to a relative path. This is a file-path relative to the directory that contains the .htaccess file.
The DirectoryIndex directive may not be required, depending on how this is configured in the server config.
Note that this will completely override the .htaccess file in the parent/root directory.
I also tried replacing the RewriteBase / line to RewriteBase /admin but it didn't work.
That didn't work because of the slash prefix on /index.html (the RewriteRule substitution string. The RewriteBase only applies to relative path substitutions.
Try putting the .htaccess in the root folder and not in /admin folder, but it didn't work.
You could do something like that, but you will need to modify the directives accordingly (it won't work as written). However, you could get conflicts, you might as well keep the sites/config separate.

Related

point root to sub-directory with .htaccess

i have the rule to redirect all request to https and point to index.php
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:CF-Visitor} !{"scheme":"https"}
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Protocols h2 http/1.1
the root directory is htdocs, but i need change it to point to htdocs/public because some framework implement public folder has root. the problem is that i fin that example:
RewriteRule ^subfolder/$ /yourfile.php [L]
It's not clear to me how to implement it.
update:
my .htaccess file is in the root directory: htdocs, then index.php is located in htdocs/public, in this subdirectory I don't have the .htaccess
finally what I am looking for is that when entering a url like:
https://your-example.com/
https://www.your-example.com/
https://www.your-example.com/test-foo/
https://www.your-example.com/test-foo?data=data&foo=foo
all these requests point to index.php in the subdirectory:
htdocs/public/index.php
update 2:
my current routing in /public/.htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
the root directory is htdocs, but i need change it to point to htdocs/public because some framework implement public folder has root.
Ordinarily, if you have access to the server config then you would "simply" change the DocumentRoot in the server config to point to the /public subdirectory. Alternatively, if you have access to the directory above the document root (as you appear to) then move the file structure "up" a level, so that the files in the /public subdirectory are moved to the document root (ie. htdocs/) and the framework/system files are in a directory above the document root, outside of the public HTML space (as they should be).
However, if you don't have access to the server config and/or are unable to move the files, and you have a /public subdirectory off the document root then proceed as follows...
I assume your current HTTP to HTTPS rule/redirect is working OK for you (since this is specific to Cloudflare)? However, the rule you posted does not route anything to index.php as you appear to suggest and the Protocols directive is not permitted in .htaccess (and should be triggering an error) - so not sure what that is doing there?
You need to add a rewrite to the root .htaccess file (ie. /htdocs/.htaccess) that rewrites all requests to the /public subdirectory. And create an additional .htaccess at /htdocs/public/.htaccess that routes all requests to public/index.php (your front-controller).
For example, try it like this:
# htdocs/.htaccess
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:CF-Visitor} !{"scheme":"https"}
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite all requests to the "/public" subdirectory
# You could do this unconditionally, depending on your requirements.
# ie. You don't necessarily need to check for existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) public/$1 [L]
Also consider canonicalising the www vs non-www hostname (ie. redirect one to the other). This would go in the root .htaccess file before or after the HTTP to HTTPS redirect (depending on requirements, ie. are you implementing HSTS?).
Create a second .htaccess file at htdocs/public/.htaccess with the following:
# htdocs/public/.htaccess
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
# Route all requests to "index.php" (front-controller)
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
UPDATE:
I quick analysis of your existing rules, but as I mentioned in comments, the version I described above is preferable.
my current routing in /public/.htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
The RewriteBase / directive here is incorrect and will break the rules when located in the /public subdirectory. This would need to be either set to RewriteBase /public or removed altogether (preferable).
The QSA flag is not required since you are not including a query string in the substitution.
^(.*)$ - the capturing group is less efficient and not required (you are not making use of any backreferences here).
The <IfModule> wrapper is not required and should be removed. This would only be required if these directives are optional (they are not) and you are moving these directives to multiple servers where mod_rewrite might not be enabled.
The filesystem checks to make sure the request does not map to a directory (!-d) and is not a symbolic link (!-l) are generally not required (and consequently is an unnecessary overhead if they are not reqd). These should be removed, unless you specifically need to access filesystem directories (or symbolic links) directly (which is rare).

htaccess rule to redirect all requests to directory

My file structure is like so:
app
/some
/private
/app
/directories
public
/assets
/images
/js
/css
index.php (app entry point)
robots.txt
If someone tries to access anything (existing or not) in /app/* the request will be rerouted to /public/index.php.
If someone tries to access any *existing file in the /public directory, they can.
Everything else all gets routed to /public/index.php.
Here's what I have:
# If not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# Allow access to the public directory
RewriteRule ^(public)($|/) - [L]
# Redirect everything to this file
RewriteRule ^ public/index.php [QSA,L]
RewriteBase /
This is working great for me, but what I need to do is if a file exists in the public directory, I would like to be able to access it like so:
site.com/robots.txt
site.com/images/image.jpg
whereas now, I must use:
site.com/public/robots.txt
site.com/public/images/image.jpg
How can I modify these rules so that any existing files within the public directory can still be accessed without using /public in the path/URL?
You may use these rules in your site root .htaccess:
RewriteEngine On
# If file exists in public directory then rewrite to public/...
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.+?)/?$ public/$1 [L]
# Redirect everything to this file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [L]

.htaccess to serve static files; route all other requests to index.php in same folder

I'm looking to make an .htaccess file that serves all static images as normal, but routes any other requests to the index.php file that's in the same folder as said .htaccess file. This is my code so far, which doesn't seem to work. It defers to an index.php that's higher up in the folder structure.
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
RewriteRule ^index.php/?(.*)$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]
As this is at sub-folder level, the issue is that the RewriteBase is pointing at the root directory. You can either remove the RewriteBase directive, or change it to be the folder that the .htaccess file is in so the rewriting can work from that point.

htaccess RewriteRule not working - (404 error) from server

I'm trying to rewrite my URL from this:
http://www.example.com/admin/index.php?id=title
to:
http://www.example.com/admin/title
I'm using this code in my htaccess:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /admin/index.php?id=$1 [L]
But then when I try out rewritten URL's i get a 404 error from my server. What is the mistake I'm making? The .htaccess is in a subfolder called admin and the rewrite rule should only work for that folder.
This .htaccess should be placed inside the folder admin that must be inside your root folder:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?id=$1 [L]
The 2 conditions make sure we are not redirecting an existent file or folder and rule tells we want to extract anything not a / and use as the ID.
The RewriteBase tell us your parent folder is admin and as such we work from there and onwards.

can not access directory without specify index.php

I want to make my app root directory can be accessed without specify index.php like
www.domainname.com/dev/
My .htaccess now is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
In my localhost it works, but in my hosting, when i access, it says "directory access is forbidden"
What should i change?
regards
Maybe this will solve the problem.
Add something like this in your .htaccess file at root directory:
DirectoryIndex index.php index.html
The first file found, from left to right, will be loaded by default. You can add more files or remove index.html as appropriate. This is just an example.