How to fix " Not found " when refresh page with vue js SPA in Apache Server? - apache

I just deploy my vue js project to apache server. My file /dist located in /var/www/html/dist. When i visit the page it's work fine. But when i visit in another page and i refresh the page, in browser say 404 Not found. How can i fix this ?

You need a configuration something similar to this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
It will automatically serve index.html file for every request that doesn't have a corresponding static file. You will have to put this .htaccess file. The use of IfModule is explained here.

I have same error, on ubuntu after i install apache.
First i added in /etc/apache2/apache2.conf
AllowOverride All
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
In /etc/apache2/sites-available
<VirtualHost test.ru:80>
ServerName www.test.ru
ServerAlias test.ru
ServerAdmin webmaster#localhost
DocumentRoot /var/www/test.ru
<Directory /var/www/test.ru>
#Разрешение на перезапись всех директив при помощи .htaccess
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then in console activate rewrite module and restart apache
sudo a2enmod rewrite
service apache2 restart

Related

Vuejs App still get 404 page after edit .htaccess

I have a Vuejs App deployed at apache2 server in digitalocean , when you hit the url it forwards you to login page which is working fine and then automatic navigate me to dashboard page that have cards when I click on one of cards I get 404 page even after I edit my .htaccess file in dist
NOTE the card route is domain.com/build
I tried .htaccess configuration from docs
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
and here is my virtual host configuration
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin email
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/public_html/app/dist
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPass /api/ http://localhost:8000/
ProxyPassReverse /api/ http://localhost:8000/
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
#RewriteCond %{SERVER_NAME} =domain.com
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Nothing wrong with virtual host file, the problem was in apache2.conf file in /etc/apache2. what I need is to change this part from
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to :
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
you need to change the AllowOverride attribute.

Setup subdirectory redirect to a Laravel project

I have three projects, First one is a static html website and other 2 projects are Laravel projects.
I have an SSL certificate set up for the main website. I want to setup the other Laravel projects as subdirectories of the main URL.
I added an alias to my conf file, but it doesn't work. I get this error: "Too many redirects occurred trying to open “https://example.com/admin/”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page."
Are there any changes that I should add to htaccess file?
Here is my website.conf file
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/***********.crt
SSLCertificateKeyFile /etc/apache2/ssl/*********.key
SSLCertificateChainFile /etc/apache2/ssl/*********.crt
ServerAdmin admin#example.com
DocumentRoot /var/www/html/example/public_html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /admin "/var/www/html/admin"
<Directory "/var/www/html/admin">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Alias /admin2 "/var/www/html/admin2"
<Directory "/var/www/html/admin2">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I would suggest taking a look at Vagrant with Laravel Homestead prebuilt box. This will achieve what you want to do by editing the Homestead.yaml file, the /etc/hosts file, and then provisioning the vagrant machine. You will need virtual box installed to work with vagrant. You can SSH into the machine by typing vagrant ssh inside the homestead directory.
Documentation: https://laravel.com/docs/5.4/homestead
YouTube Video about Vagrant: https://www.youtube.com/watch?v=LyIyyFDgO4o
Youtube Videos about Installing Laravel Homestead: https://www.youtube.com/watch?v=tbPGuikTzKk&list=PLfdtiltiRHWEaImCreOC7UkK4U5Xv9LdI
You should use the public folder as your alias in the apache setup.
Try "/var/www/html/admin2/public" and also for the Directory as well.
Something like this:
Alias /admin2 "/var/www/html/admin2/public"
<Directory "/var/www/html/admin2/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Depends on the installation of Laravel, you should have an .htaccess file in the public directory, which might require some additional tweeking.
Also, restart apache once you have made the change.t
You need to correctly set 2 things :
Your sites's alias in WAMP's alias folder e.g. C:\wamp\alias to redirect URL to public\index.php in Laravel's folder
Rewrite base .htaccess in your Laravel's public folder so that Apache rewrites on top of a base path.
Assuming that your site's directory is in C:\wamp\www\site and your alias is localhost/alias
Setting alias to point to public folder:
Alias /site "/var/www/site/public"
<Directory "/var/www/site/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Add RewriteBase in .htaccess in Laravel's public folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase '/site/'
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Not able to access laravel routes in apache2 server

I've deployed a new laravel installation to my server, While doing so I configured my apache2 as following:
I added 000-default.conf file in /etc/apache2/sites-available/ as following:
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
Require all granted
</Directory>
I've my laravel installed into /var/www/html/stellar folder now I access my installation through:
http://52.39.175.55/stellar/public/
But while calling the routes it is not working, Like
http://52.39.175.55/stellar/public/oauth/token
Here is the screenshot:
But suppose I call through this:
http://52.39.175.55/stellar/public/index.php/oauth/token
I get the access,
I tried changing my public/.htaccess file to something like this:
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /stellar
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Still there is no change, Help me out in this.
Your VirtualHost on /etc/apache2/sites-available/ should look like this:
<VirtualHost *:80>
DocumentRoot "/var/www/html/stellar/public"
ServerName yourdomain.com
<Directory /var/www/html/stellar/public>
AllowOverride All
</Directory>
</VirtualHost>
Then restart apache2 and it should work.
I hardly recommend you to duplicate the 000-default.conf file, rename it and include the VirtualHost, then enable it with a2ensite command, just because it's more easy to manage.

Laravel 5.2 shows apache screen instead of index.php

I checked most of question regarding to my problems as this seems to be very common error for Laravel beginners but I can't find the solutions so far :(
I use the digital ocean VPN, centos7, php5.6, apache, windows 10.
Installed laravel and configured httpd.conf, mysite.com.conf, .htaccess and hosts fies then restart apache but still welcome screen doesn't show and it shows apache screen instead.
+==================================================+
here is my settings...
1) /etc/httpd/conf/httpd.conf
<Directory "/var/www/html">
Options -Indexes +FollowSymLinks
AllowOverride None (default setting)
And added below the end of the file
ServerSignature Off
ServerTokens Prod
IncludeOptional sites-enabled/*.conf
For reference, other default settings are below
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
2) /etc/httpd/sites-available/mysite.com.conf
<VirtualHost *:80>
ServerAdmin admin#mysite.com
ServerName mysite.com
ServerAlias mysite.com
DocumentRoot /var/www/mysite.com/public_html
ErrorLog /var/log/httpd/mysite.com_error_log
CustomLog /var/log/httpd/mysite.com_access_log combined
AddDefaultCharset UTF-8
<Directory /var/www/mysite.com/public_html>
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
AllowOverride All
</Directory>
</VirtualHost>
3) /var/www/mysite.com/public_html/.htaccess
<IfModule mod_rewrite.c>
 <IfModule mod_negotiation.c>
  Options -MultiViews
 </IfModule>
 RewriteEngine On
 RewriteRule ^(.*)/$ /$1 [L,R=301]
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^ index.php [L]
</IfModule>
4) C:/windows/system32/drivers/etc/hosts
digitaloceanDropletIp mysite.com
5) publi_html folder img
I would be really appreciated if someone can tell me what is wrong my settings.
Thanks in advance!
Change
DocumentRoot /var/www/mysite.com/public_html
To
DocumentRoot /var/www/mysite.com/public_html/public
Then restart your apache
[Solution]
By kindest people's help, I finally managed to show Laravel logo! Stupidly I didn't realised index.php is in public folder!
I kept httpd.conf and hosts as same as above, but changed mysite.com.conf below and completely removed/deleted public_html/.htaccess (I may need it future but this stage I don't need).
<VirtualHost *:80>
ServerName mysite.com
ServerAlias mysite.com
ServerAdmin admin#mysite.com
DocumentRoot "/var/www/mysite.com/public_html/public"
ErrorLog /var/log/httpd/mysite.com_error_log
CustomLog /var/log/httpd/mysite.com_access_log combined
AddDefaultCharset UTF-8
<Directory "/var/www/mysite.com/public_html/public">
AllowOverride All
</Directory>
</VirtualHost>
After this change, 403 Error came up (seems to be this happens for apache) so I added the below line just before 'RewriteEngine On' in public/.htaccess Refer: non English website
Options +FollowSymLinks
Then 500 Error came up, so I used putty for the blow command (I had to do this even thought I previously did 'chmod 777 storage') Refere: Laravel blank white screen.
chmod -R guo+w storage
Then finally 'Laravel' showed up :) Anyway thanks for all help!

localhost redirects to one of local websites

I added to C:/windows/system32/drivers/etc/hosts:
127.0.0.1 mywebiste
and in httpd-vhosts.conf added:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "C:/xampp/htdocs/mywebiste"
ServerName mywebsite
ErrorLog "logs/error.log"
CustomLog "logs/access.log" common
<Directory "C:/xampp/htdocs/mywebiste">
Options Indexes FollowSymLinks Multiviews
Options -Indexes
AllowOverride None
Order allow,deny
allow from all
#RedirectMatch ^/$ / index.php
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</Directory>
</VirtualHost>
And now when I want to add new website to hosts file and httpd-vhosts.conf it renders 'mywebsite' and I cant access that website, even if I delete my virtualhost in httpd-vhosts.conf and remove my entry from hosts file it still renders 'mywebsite'. What should I do to fix this, I have no clue?
I believe you need
NameVirtualHost *:80
Prior to any <VirtualHost...> lines
See here
Nevermind, you have to restart the apache to apply new settings :) sorry. Btw it doesnt work at all wit NamevirtualHost.