Getting 404 page on refresh on AngularJS site? - .htaccess

I am getting a served the 404 page whenever I :
1.) refresh any page on my Angularjs site (besides the root page)
or
2.) click Back to go from a 404 page to the root page (the root page will be a 404)
Here is my .config() code
'use strict';
angular.module('mmApp', ['ngResponsiveImages'])
.config(function ($locationProvider, $routeProvider) {
$locationProvider.html5Mode(true);
$locationProvider.hashPrefix = '!';
$routeProvider
.when('/', {
templateUrl: '/views/design.html',
controller: 'MainCtrl'
})
.when('/about', {
templateUrl: '/views/about.html',
controller: 'MainCtrl'
})
.when('/contact', {
templateUrl: '/views/contact.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
});
});
I have come across this similar question but do not quite understand the answers or how to implement them.
AngularJS - Why when changing url address $routeProvider doesn't seem to work and I get a 404 error
The accepted answer says to setup Apache to reconfigure all paths to the root. My site is on Github Pages. I have a .htaccess file in my gh-pages repo but I have no idea how to configure it to send all paths to the root.
The answer says another thing to consider is using the <base> element like <base href="/" /> but where would I place that? In my index.html? If so at the top or bottom of that file? Also, would I just leave the href value to /?

I came across a solution here: https://coderwall.com/p/kfomwa
Here's the short post:
angularjs provides html5Mode, which makes your app use pushstate-based URL instead of hashtags. However this requires server side support, since the generated urls need to be rendered properly as well.
This actually works fine with github pages' custom 404 pages, though it's only available for custom domain enabled pages.
First, copy everything inside index.html to 404.html. Then, add this to your app:
angular.module('app', []).config(function($locationProvider) {
$locationProvider.html5Mode(true);
});
Note that if you are on angular 1.1.5, make sure you set for html5Mode to work properly.
Following these suggestions I was able to get my site working properly. Now when I hit Reload, the page loads properly.

The better option would be to redirect any url to the index.html:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.html
This will NOT cause a 404 response on any page.

Github pages only serves static files. It won't read any configuration settings you have for Apache in .htaccess.
If you want to run an AngularJS app out of Github pages, you cannot use html5Mode.

I'm running my angularjs application using Nginx on Vagrant. And facing the similar kind of issue.
On reloading the browser with any url, it gives me a 404 page.
I have enabled the html5mode in main js file. When you have html5Mode enabled, the # character will no longer be used in your urls. The # symbol is useful because it requires no server side configuration. Without #, the url looks much nicer, but it also requires server side changes.
Here are some changes to do:
Open nginx configuration file (nginx.conf) in /etc/nginx/sites-enabled folder (path will vary based on your nginx installation directory).
Find try_files $uri $uri/ =404; and replace it with try_files $uri $uri/ /index.html;
Save the nginx configuration file
Restart the nginx sudo service nginx restart
And I have tried to reload the page after doing above changes. It works as expected.

Related

When trying to access index.php it redirects to index and shows 404 page

Im using IIS and I recently installed a module called URL Rewrite 2.
I tried changing the way index.php shows in the url.
I wanted to change from http://localhost/index.php to http://localhost/index
I did that and it wasn't working so I reverted everything.
Now when I search for http://localhost/index.php it redirects me to http://localhost/index and shows a 404 page.
I don't have any web.config in the server folder. I even unnistalled URL Rewrite 2. Everything was working before I tried this.
If you are sure you have reverted everything the redirect might be cached in the browser, try clearing the cache (or using another browser).

How to set proxy pass for react app on nginx?

My react app has got different routes to consume different functionalities
e.x.
localhost:3000,
localhost:3000/Dashboard,
localhost:3000/Hub,
localhost:3000/Person
etc....
I wanted to configure the react app routes in the nginx in the production environment. What I have actually done so far in the nginx configuration at production env is,
server_name api.vesta-project.net;
location /vqcc {
proxy_pass http://localhost:3000/;
}
My problem here is with the current settings, the homepage works well when I say "api.vesta-project.net/vqcc". Whereas, when I click a button that navigates to /Dashboard. I get 404 error as it does not append "vqcc" to the path in the react app internally thus it becomes like api.vesta-project.net/Dashboard" when inspecting the request which is wrong per nginx conf. So I need a solution whenever the client make a request, it should append "vqcc" to the path so that it will become a valid url as per nginx routes.
e.x when client request for api.vesta-project.net/Dashboard, it should become
api.vesta-project.net/vqcc/Dashboard
Pls help me if I can handle this at nginx or package.json without being changing any routes in the react app internally
You can try rewriting the uri in catch-all location.
location / {
rewrite /(.*) /vqcc/$1;
}
location /vqcc{
proxy_pass http://localhost:3000/;
}

Paths in Node.js

I used to use Apache + .htaccess to serve a index.html page in a subdomain. The .htaccess redirected every pathname in any subdomain to the index.html itself.
For example:
In http://path1.myserver.com/chat, chat wasn't treated as a path, but as a "parameter" to the index.html file.
Also, http://path2.myserver.com/chat would be redirected to the same index.html file.
When I changed the server to Node, I couldn't (shouldn't) use .htaccess anymore.
How can I move from Apache to Node, keeping this functionality?
I tried to use vhost + router, but unsuccessfully.
I can set vhosts to serve *.myserver.com to a specific router, so http://path1.myserver.com and http://path2.myserver.com are correctly redirected to the same index.html, but when I try http://path1.myserver.com/chat, chat is being treated as a path, giving me a 404 error.
I need to make sure that every pathname in the pattern *.myserver.com/*/.../* be served by the same index.html (like done in .htaccess).

Using the same styles for Jekyll Blog in subdomain of Node.js + nginx app

I'm fairly new to working with nginx and server configs.
So my personal website is a Node app using nginx as a reverse proxy. I want to setup a Jekyll blog as a subdomain of my website in website.com/blog. I just got it working to serve a simple static html 'Hello World' page by adding an index.html file to /var/www/blog and modifying my sites-enabled/defaults to handle the /blog route by doing:
location ^~ /blog {
root /var/www/;
index index.html index.htm;
}
So as for my next steps, I'm a little confused on what to do. For Jekyll, I will probably set up the git hooks like this tutorial says but the issue is that I want to keep the page style the same as my website running on node.js.
I was wondering if there was a way - maybe in the config file or something - to set it so that Jekyll uses the same stylesheets as my node app so that the pages have a unified look to them?
My node app is the root directory of my server and the static page for the /blog endpoint is in the /var/www/blog/index.html file.
From what I understand from your setup, I guess that :
website.com serves stylesheet at website.com/style.css,
your jekyll blog is a new install, configured with baseurl: /blog
So when you're visiting website.com/blog/, your css is 404 because your blog page is looking for it at website.com/blog/styles.css and not at website.com/style.css.
In order to resolve this, in your blog _includes/head.html you can change :
<link rel="stylesheet" href="{{ "style.css" | prepend: site.baseurl }}">
to
<link rel="stylesheet" href="/style.css">
Your css will now be requested as website.com/style.css.

nginx - Replace page content on homepage

I've been having a bit of difficulty setting up nginx on my ubuntu server. Right now its configured to proxy another website. I want to add some content to the main page right after <body>. I used:
subs_filter '<body>' '<body>' My content;
This works great, the only problem is that it's at every page, How can I use an if statement or something to make it only appear on the homepage. I tried going with
if ($uri ~ 'index.php') then do the above filter but that gives an error saying nginx: [emerg] "subs_filter" directive is not allowed here.
I looked it up but had a lot of trouble finding what I need :(.
According to nginx documentation subs_filter may in used in the http, server and location contexts.
Thus, in order to have the filter activated only for the index.php page,
location = /index.php {
subs_filter '<body>' '<body>' My content;
# other things you would do for index.php
}

Resources