Deploying vue js app and getting 404 error in routes - vue.js

I am using a webpack simple but after deploying my app to an hosting server my index page works fine but other pages gives a 404 error .Please i dont know if anyone have any idea what is happening .The webpack simple only generate build js file for me and thats all, i dont get an index.htm file in my dist folderc

I am going to show you how to do it but before read this from vue.js docs
Step 1
Run the following command
npm run build
Step 2
Copy the dist folder and put it in a new folder
Step 3
In the new folder create a new file and name it .htaccess
Step 4
Inside .htaccess file insert the following code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Now you application is ready to deploy!You can even run it using apache on local machine.
Please again, read docs you can find more configurations there for nginx, node.js and learn more about all server configurations.
Additional info: I am using laragon + apache and after the step 4, I put the new folder inside www directory of Laragon, and serve it with Laragon. I guess you can do the same by using xampp or wamp by putting the new folder in docs directory.

That is due to the history push mode on your vue router.
If you use Apache, follow these steps, else you can read the vuejs doc https://router.vuejs.org/guide/essentials/history-mode.html:
If Apache version is above 2.2, you can use Fallback ressource instead of mod_rewrite in your apache config. It works for me.
In /etc/apache2/apache2.conf
<VirtualHost *:80>
ServerName YourServerName(like yourwebsite.com)
DocumentRoot /var/www/yourAppLocation/dist
<Directory "/var/www/yourAppLocation/dist">
FallbackResource /index.html
</Directory>
</VirtualHost>
Or you can use classical mod_rewrite
<VirtualHost *:80>
ServerName YourServerName(like yourwebsite.com)
DocumentRoot /var/www/yourAppLocation/dist
<Directory "/var/www/yourAppLocation/dist">
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
</Directory>
</VirtualHost>

You need to set up your hosting to route every 404 error to index.html and let vue handle the errors , which hosting are you using? if you cant set up your error pages you could remove mode:'history' mode from your router or use hash mode instead, here is the official docs to how to setup your host, as for your index.html you can find it in your project root not in dist folder

Related

Url Not found on reload + Angular5

I am facing one issue related to the angular app, using components in my app then when I go with the single page the page loaded successfully but when I open that URL in a new tab or reload it, give error Not found.
I am using AWS apache server for this and only facing this issue on it, in my Godaddy server page reloading working properly.
Here I am attaching my screenshot of the error.[
Thankx in advance
I think your .htaccess file is not reachable,
Create .htaccess file inside /var/www/html i.e. in the folder where your index.html is.
Put following code in it:-
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
Than goto /etc/apache2 folder and open apache2.conf file with sudo.
cd /etc/apache2
sudo vim apache2.conf
Remove comment(#) of AccessFileName it will look like
AccessFileName .htaccess
Then find the line where there is
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
replace "None" with "All"
AllowOverride All
Done!
Now run command
sudo service apache2 restart
And check now!
Add .htaccess file in your project root folder.
bellow code add in your Appache .htaccess file:
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
Nginx .conf file :
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.html break;
}
}
execute command :
sudo a2enmod rewrite
sudo service apache2 restart

How to setup apache server for React route?

I have my react app running great on my local dev server but it did not work when I dump my production ready files straight into Apache's htdocs directory:
Here is what I have:
/var/www/index.html
/var/www/bundle.js
and I have
DocumentRoot /var/www
in /etc/apache2/sites-available/000-default.conf
The fact is that
1). when I access http://...com/ that routed me to Login page
2). After I clicked a link
<Link to="main"><button>Log In</button></Link>
the content in the browser location field become:
http://...com/main
3). Now if I reload this url (http://...com/main), I got
The requested URL /main was not found on this server
My rounting in React:
<Router history={browserHistory }>
<Route path="/" component={TopContainer}>
<IndexRoute component={Login} />
<Route path='main' component={MainContainer} />
</Route>
</Router>
What else I am missing in the apache configuration?
thanks
Change the VirtualHost configuration (typically found in /etc/httpd/conf.d\vhosts.conf) by adding the following Rewrite* lines:
<VirtualHost *:8080>
ServerName example.com
DocumentRoot /var/www/httpd/example.com
<Directory "/var/www/httpd/example.com">
...
RewriteEngine On
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
This tells Apache to serve any files that exist, but if they don't exist, just serve /index.html rather than a 404: not found.
Apache Reference: Configuring Apache Virtual Hosts
react-router History Reference: Configuring Your Server
Complete answer gratefully stolen from here
Edit: 'On' need to be uppercase in current apache version
The above solution works for Ubuntu as well but I have struggled a bit with it so here are the steps necessary to make it work.
Location of the file where you need to place the above mentioned configuration is under
/etc/apache2/sites-enabled
default is
/etc/apache2/sites-enabled/000-default.conf
Then you need to make sure that RewriteEngine is running (otherwise you will get an error when restarting Apache server).
sudo a2enmod rewrite
Finally, restart Apache server
sudo /etc/init.d/apache2 restart
Now, it should work.
When you are using default configuration (root of the website is under /var/www/html), then all you need to do is to place
<Directory "/var/www/html">
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
to the above mentioned file under <VirtualHost ...>
If you have to use .htaccess and a sub directory then following works for me.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
What worked for me, echoing many of the answers and comments here:
sudo a2enmod rewrite
Open up /etc/apache2/apache2.conf
Paste in this with the path to your root:
<Directory "/var/www/PATH_TO_YOUR_ROOT">
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
sudo service apache2 restart
Pasting into the site-specific conf file did not work as earlier answers suggested.
None of the solutions posted so far appear to address the issue where missing ressources incorrectly return 200 instead of 404, which can make debugging when certain files are missing rather annoying.
My solution is to instead watch what type of resource the request expects to recieve, since browsers will ask for HTML when navigating to a page (Firefox asks for text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8) but not when accessing resources after the initial load (JS files imported via <script> or as ES6 modules ask for */*, CSS files ask for text/css,*/*;q=0.1, accessing JSON via the fetch() API will specify application/json, text/plain, */* and so on). By relying on that assumption, one can configure Apache to serve the Single page app when trying to access a non-existent file (such as a route that only works within the Single-page app) without also sending it whenever said SPA asks for a CSS file that has been renamed or a missing JSON file.
EDIT: MDN has a list of common values for the Accept header.
<Directory "/var/www/httpd/example.com">
RewriteEngine on
# Browsers will specifically ask for HTML (among other things) on initial page load
# That is, if the *user* tries to access a *nonexisting* URL, the app is loaded instead
# but if a webpage attempts to load a missing resource it will return 404.
# (You can still go to /myreactapp/favicon.ico, but a missing /myreactapp/favicon.png resource won't return 200)
# if (HTTP_ACCESS.contains('text/html') && file_not_exists(REQUEST_FILENAME))
RewriteCond %{HTTP_ACCEPT} text/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [last]
# Any ressources loaded by index.html should behave correctly (i.e: Return 404 if missing)
RewriteRule ^ - [last]
AllowOverride None
Options FollowSymLinks Multiviews
Require all granted
</Directory>
Thank you! This worked for me.
I am pasting my config if you are serving multiple sites (virtualhost) and also SSL certificates (SSL was made with certbot), with redirect http to https
This setting works on Linode / Ubuntu
yoursite.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin webmaster#yoursite.com
ServerName yoursite.com
ServerAlias www.yoursite.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/yoursite.com/public_html
<Directory "/var/www/html/yoursite.com/public_html">
RewriteEngine on
# Browsers will specifically ask for HTML (among other things) on initial page load
# That is, if the *user* tries to access a *nonexisting* URL, the app is loaded instead
# but if a webpage attempts to load a missing resource it will return 404.
# (You can still go to /myreactapp/favicon.ico, but a missing /myreactapp/favicon.png resource won't return 200)
# if (HTTP_ACCESS.contains('text/html') && file_not_exists(REQUEST_FILENAME))
RewriteCond %{HTTP_ACCEPT} text/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [last]
# Any ressources loaded by index.html should behave correctly (i.e: Return 404 if missing)
RewriteRule ^ - [last]
AllowOverride None
Options FollowSymLinks Multiviews
Require all granted
</Directory>
# Log file locations
LogLevel warn
ErrorLog /var/www/html/yoursite.com/log/error.log
CustomLog /var/www/html/yoursite.com/log/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/yoursite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yoursite.com/privkey.pem
</VirtualHost>
</IfModule>
This is what we use at work for our production react app which is using BrowserRouter from react-router:
httpd.conf
<VirtualHost *:3000>
DocumentRoot "/var/www/html"
<Directory /var/www/html/>
Header set Cache-Control "no-cache"
# https://stackoverflow.com/a/34154531/2089675
FallbackResource /index.html
</Directory>
<Directory /var/www/html/static/>
# https://create-react-app.dev/docs/production-build/#static-file-caching
Header set Cache-Control "public, max-age=31536000"
# https://stackoverflow.com/a/54943214/5600537
RequestHeader edit "If-None-Match" '^"((.*)-gzip)"$' '"$1", "$2"'
</Directory>
</VirtualHost>
As you can see most of the comments in there are answers from SO, so I'm just giving back :)
configuration
Place the above file in /usr/local/apache2/conf/httpd.conf.
The config also assumes that you have put the contents of the build folder inside /var/www/html/. If you've placed them elsewhere, then adjust the path accordingly.
ports
The VirtualHost *:3000 part is just for exposing the server's port in the docker container (httpd:buster) used to run it. This is also the same port CRA defaults to in dev. An external proxy is used to manage where the application can be accessed from.
compression
Finally, if you are interested in serving gzipped files you may want to remove the RequestHeader edit line, and then do some more work to make sure .gz files can be served:
ex.
AddOutputFilterByType DEFLATE text/html application/javascript
React routing issue fixed on ubantu server
Solution:
Open the file using the console.
If you are using SSL
nano /etc/apache2/sites-available/000-default-le-ssl.conf
Add the following lines
===================================================================================
DocumentRoot /var/www/project
<Directory "/var/www/project">
RewriteEngine on
RewriteCond %{HTTP_ACCEPT} text/html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [last]
RewriteRule ^ - [last]
AllowOverride None
Options FollowSymLinks Multiviews
Require all granted
Solution:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
If you've multiple virtual host then follow these steps
Goto to that VH and open the .htaccess file
add these lines and save it
restart the apache service again so that it can reflect into the settings
Go on this directory
/etc/apache2/sites-available
open File : 000-default.conf
Change its permission : 777
Paste code on bottom of file
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
Restart server

Laravel 5 pages are not being displayed on live server

Main Points:
- I am using Laravel 5, apache, and ubuntu
- mysite.com is WORKING
- mysite.com/login works on localhost, but not on live server
I'm building my first website, and my home page is 'live' (it is up and working) My problems arise when I want to create a 'mywebsite/login' page. I am getting a 404 Not Found error on live, when the page works on my local machine.
I believe this is happening because my .htaccess file not rewriting correctly in regards to my folder structure. The problem is I'm not able to figure it out, as this is all new to me.
My document root is /var/www/laravel/public
My .htaccess is in the above folder.
It looks like this
<IfModule mod_rewrite.c>
<IfModule mod_negotians.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I have a virtual host file for my site in /etc/apache2/sites-available
It's contents look like this
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel/public>
Options -Indexes +IncludesNOEXEC +FollowSymLinks +ExecCGI
Allow from all
Allowoverride All
</Directory>
RewriteEngine on
<Directory /var/www/laravel>
AllowOverride All
</Directory>
**A few more lines...**
</VirtualHost>
I have also enabled mod_rewrite on the server.
I am thinking that I have a smaller 'pointing' error here somewhere, but I'm not experienced enough with these files to see it. Is there anyone that can give me a hand?
Thanks
Move All the files on xyz(your folder name for which domain/ subdomain is mapped) folder
Then follow these steps
1 – go to public folder
2 – copy .htacsses file(this make your routes work)
3 -paste it in xyz folder
4 change server.php to index.php
5 – enjoy and happy coding
This is working fine with hostgator.in shared hosting.
When you push your code from local to server then sometimes it's not working the same. For that please try following commands. It works for me and I hope it works for everyone as well.
$ sudo chmod 777 -R folderpath //To give 777 permission to your project
$ php artisan config:clear //To remove the configuration cache
$ php artisan view:clear //To remove the views blade cache
$ php artisan route:clear //To remove the route cache
$ php artisan clear-compiled //To clear the compiled classes and services application cache
// If you use passport then:
$ php artisan passport:install
// At last:
$ composer update

Yii2 After configuring httpd.conf, still appending the web folder in url

This is the set up of my httpd.conf to my yii application
Alias /attendance "C:/wamp/www/myproject/web"
<Directory "C:\wamp\www\myproject\web">
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# ...other settings...
</Directory>
This works fine if I open up in browser
http://localhost/attendance/
but when I navigate to the page like about,contact,Login
it will use the myproject in the url like this
http://localhost/myproject /web/site/about
also as you can see that the web folder is appended in the url
how can I use my alias and remove the appended web in url
Set DocumentRoot to your project path "C:/wamp/www/myproject/web".

Using mod_vhost_alias with CakePHP (which uses mod_rewrite)

I am not an apache guru. But I want to configure my server for mass virtual hosting using CakePHP. The idea is that we will be able to easily set up multiple versions of the same application based on directory location:
production.domain.com
testv1.domain.com
etc...
So I know I have mod_vhost_alias working just fine. I have a basic directory set up where I have added a test index.html file (/var/www/htdocs/cake/test/webroot). When I point my browser to the location (test.domain.com), the index.html is displayed in the browser. My vhost is configured to pull %1 from the URL to know what directory to point to:
VirtualDocumentRoot /var/www/htdocs/cake/%1/webroot
But when I point my browser to the cake application, I get a page not found error. I suspect it has something to do with the mod_rewrite in the .htaccess file. Here are the full configs for both:
mod_vhost_alias (in .conf file)
<VirtualHost *:80>
ServerAlias *
UseCanonicalName Off
VirtualDocumentRoot /var/www/htdocs/cake/%1/webroot
<Directory /var/www/htdocs/cake/%1/webroot>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
.htaccess (in webroot - default as it comes from CakePHP)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Any ideas how to get them to work together?
Turns out all it needed was:
DirectoryIndex index.html index.php