After refreshing, Angular route using html5Mode leads to a 'Page Not Found' error page

I have created several Angular routes as illustrated in the code snippet below.

app.config(function($routeProvider, $locationProvider, $provide) {
    $routeProvider
    .when('/', {
         templateUrl: 'home.html',
         controller: 'AppCtrl'
    });
    .when('/portfolio', {
        templateUrl: 'portfolio.html',
        controller: 'AppCtrl'
    })
    $provide.decorator('$sniffer', function($delegate) {
         $delegate.history = historyCompatCheck();
         return $delegate;
    });
    $locationProvider.html5Mode(true);
});

Everything seems to be working fine. When I set the base href to "/", it accepts an anchor with the href of "/portfolio". However, when I navigate to "" or try to reload the page while on the portfolio route, it results in a server error. Is there a solution to this issue?

Thank you in advance.

Answer №1

After some troubleshooting, I was able to solve the issue on my own. It turns out that creating a .htaccess file in the root directory of your project is necessary. Here's what you need to include:

 <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
 </IfModule>

Answer №2

#BEGIN

Options +FollowSymLinks

<ifModule mod_rewrite.c>
    RewriteEngine on

    # Do not rewrite files or directories
     RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
   RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]

   RewriteRule ^(.*) your root folder/index.php [NC,L]

 </ifModule>
   #END

This code is functioning perfectly

<base href="/"></base>
also make sure to include in the <head> section of your index.html file

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create specification for the properties of the child component

I am interested in working with the props of a parent element's children and validating those props. Can I achieve this using prop-types? export default function MyComponent(props) { return ( <div> {React.Children.map(props.chil ...

JavaScript/jQuery - dynamic name selector with animation

I've been working on developing a raffle ticket picking script using JS and jQuery. Initially, my script is functioning well. However, I am now looking to enhance its visual appeal so that it can be operated in assembly. In order to achieve this, I ...

When a function is transferred from a parent component to a child component's Input() property, losing context is a common issue

I have encountered an issue while passing a function from the parent component to the child component's Input() property. The problem arises when the parent's function is called within the child component, causing the this keyword to refer to th ...

Ways to display form choices that are influenced by other form selections

In my form, the user is supposed to choose a specific item at the end. As they fill in the initial options, the subsequent fields below change dynamically. Here is an example: Type: { t1:{ Number of X:{ 1:{...} 2:{...} ...

The MEAN application encountered a crash due to a loss of internet connection

I'm currently working on a MEAN application. Everything runs smoothly when accessing it through http://localhost:3300 with an active internet connection, but I encounter the following error when disconnected from the internet: process.nextTick(functi ...

The issue arises when trying to access the value that is not defined in the combination of angularjs $

I am currently working on retrieving the Balance value of an ethereum account using the web3 API. My goal is to extract this value and store it in the $scope so that I can access it in my HTML. However, I seem to be encountering an issue where I consistent ...

Implementing multiple content changes in a span using javascript

Having an issue with a pause button where the icon should change on click between play and pause icons. Initially, the first click successfully changes the icon from a play arrow to a pause icon, but it only changes once. It seems like the else part of the ...

What is the best way to transform a JSON object from a remote source into an Array using JavaScript?

Attempting to transform the JSON object retrieved from my Icecast server into an array for easy access to current listener statistics to display in HTML. Below is the JavaScript code being used: const endpoint = 'http://stream.8k.nz:8000/status-json ...

Tips for triggering onclick before changing to a new page?

Currently, I am developing a React application and have encountered the following situation: <a href={link} variant="primary" onClick={this.onClickDoSomething}> here. </a> My goal is for the onClick event to trig ...

Is there a way to export a modified OBJ geometry from a Three.js scene?

Every time I make changes and export my Three.js scene with a SkinnedMesh model, the original imported model gets saved instead of the updated version. Despite rotating bones and adjusting morph targets, the exported model remains unchanged. Even though t ...

Leverage Typescript to convert string literal types to uppercase

type x = 'first' | 'second' I am looking to create a type y that is similar to this: type y = 'FIRST' | 'SECOND' Here is what I attempted: type x = 'first' | 'second' type y = {[key in x]: key[& ...

I'm curious about utilizing jsviews in conjunction with jquery sortable

Check out my jsFiddle Example where I am using jsViews in conjunction with JQuery sortable. By default, the remove function works fine; however, when you change the order of items and then try to delete one, multiple items are removed. How can this issue ...

Issue with handling .bind in Angular's karma/jasmine tests Angular's karma/j

When writing unit tests for my functions, I encountered an issue with a bound function in the test runner. The problem arose when trying to bind a function to have reference to 'this' inside an inner function. Here is the code snippet in question ...

Error message "invalid function call this.job.execute" when using Node.js node-schedule npm package

Having just started with nodejs, I created a nodejs program and set it to run every minute using the node-schedule library. However, after running for some time and producing several logs in the console, I encountered an error stating that this.job.execute ...

Exploring Three.js raycasting in a non-full screen scenario

I've encountered some challenges with raycasting in three.js recently. The div element I'm using is not fullscreen, which I believe is causing issues with the raycast positioning that I'm struggling to resolve. let mouseVector = new THR ...

Deleting an item using jQuery

In the Document Object Model (DOM), there is a button available to remove the parent element here: <i class="fa fa-times remove-product-compare" aria-hidden="true"></i> Here is an example of my DOM structure: <div class="col-lg-12 col-md- ...

Show information from a JSON file in a tooltip on a Highcharts' pie chart

I have a pie chart that shows two percentages. I am looking to update the tooltip content to display information from my JSON data. Here is an example of how my JSON data looks: {"object1":{"percentage": 0.7, "numberOfObject": 167}, "object2":{"percentage ...

jQuery includes inArray as the final element in an array or object

There's a strange issue I've noticed where, in some cases, when jQuery is imported, it appends the inArray function as the last element of a defined object or array. This can cause problems with for ... in loops since it counts this unexpected fu ...

What is the method for implementing conditions within a button element in Vue.js 2?

Here is an example of my component code: ... <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> ... <script> import { mapGetters } from 'vuex' export default{ ...

code snippet for callback function in javascript

Recently, I've been working on a project with angularjs and phonegap and stumbled upon this interesting code snippet. While I have a basic understanding of what it does, the inner workings are still a bit unclear to me. Since I'm still getting fa ...