"Exploring the world of AngularJS routing and using htaccess for rewriting URLs

How can I seamlessly integrate AngularJS ngRoute and htaccess rewrite?

Currently, my ngRoute setup generates URLs like this:

http://domain.com/#/something/somestring

However, I would prefer cleaner URLs like:

http://domain.com/something/somestring

In essence, I want to remove the /# from my URLs. While I have accomplished similar URL restructuring in the past using .htaccess, mod_rewrite.c, and PHP, I'm unsure how to achieve the same outcome with AngularJS.

If anyone can provide guidance through tutorials, articles, examples or any resources on how to accomplish this, it would be greatly appreciated.

A few key criteria for achieving this: I need to maintain my current routing structure, specified as follows:

blogApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/page/:pagePermaLink', {
        templateUrl: './assets/templates/page.html',
        controller: 'pageCtrl'
      }).
      when('/article', {
      templateUrl: './assets/templates/article.html',
      controller: 'articleCtrl'
      }).
      otherwise({
        redirectTo: '/home',
        templateUrl: './assets/templates/page.html',
        controller: 'mainCtrl'
      });
  }]);

The URL query string, such as :pagePermaLink, must still be accessible from the scope:

blogCtrl.controller('pageCtrl', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
    var foo = $routeParams.pagePermaLink;
    // ...
}]);

Answer №1

If you've completed your rewriting tasks, simply implement

$locationProvider.html5Mode(true)
. For more details, visit this link

You may also find answers to similar queries on these Stack Overflow threads: Angular Direct URL without hash $location / switching between html5 and hashbang mode / link rewriting

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

URL Image Cropping

Hello, I have been utilizing the iTunes API and have successfully saved the search data into my database. Currently, I am saving the image URLs from iTunes to my database and am seeking advice on the most efficient method to crop these images using PHP. ...

How to Conceal File Extensions with .htaccess

Can anyone provide guidance on the best way to hide .php extensions using HTAccess, even for sub-folders? I have attempted various solutions from previous answers but none seem to work. ...

Using HTML URL parameter in PHP code

Hi there, I'm new to using PHP and would appreciate any help you can provide. I'm facing an issue with passing a URL parameter from an HTML page (mywebsite.com/f.html?survey_id=5d86055f35bf41f3a35f2c779fc478dc) to a PHP script that should save t ...

URL rewriting does not retrieve information from the $_GET superglobal

I have defined certain rules for URL rewriting on my website. Currently, I am working on localhost I have configured this in the .htaccess file php_flag output_buffering on Options +FollowSymlinks RewriteEngine On RewriteRule ^p/([^/]*)\.html$ /?p= ...

Retrieve the result set rows and store them in an array as individual objects

I'm attempting to fetch data from a MySQL database using Angular and PHP. My Angular code looks like this: $http({ url: "http://domain.com/script.php", method: "POST", headers: {'Content-Type': 'applica ...

Dealing with incoming data in a yii2 RESTful service

When sending an array using $http post, I follow this approach: var results = services.transaction('orderdetails/insert', {customer_id: $scope.order.customer_id, data: $scope.orderDetail}); The variable $scope.orderdetail ...

Setting the permissions of the current folder to 777 in the ht

Is it possible to create an .htaccess file that changes the permissions for the current folder and allows PHP execution? The server administrator has implemented a rule to block PHP extensions in all directories. For example, I have: [site]/forum/ and ...

Identifying the user's location within the application and dynamically loading various Angular scripts

Currently, I am working on a large-scale web application using Laravel and Angular. In this project, I have integrated various angular modules that come with their own controllers, directives, and views. One challenge I am facing is the need to load diffe ...

Converting HTML code to JSON using PHP scripting

Would appreciate your help in resolving a small issue. Although I came across this post, I am still encountering some errors: How can I convert HTML to JSON using PHP? I have created a PHP file that extracts a post from WordPress with the following forma ...

CodeIgniter is functioning perfectly on my localhost but unfortunately encounters issues when running on the server

My organization's staff has individual profile pages. When their email is typed in, the information related to that staff member will be displayed. For example: http://localhost/people/zkanoca This link shows my own profile information. However, whe ...

Cannot access Codeigniter subfolder

I am facing an issue with codeigniter htaccess and need some assistance. My demo site URL is sampleurl.com and all my files are located in a subfolder named demo. -sampleurl.com -demo - application - system - index.php -demo1 I hav ...

Eliminating .php from the URL and the GET request

After implementing .htaccess, I successfully transformed the URL: http://www.websitename.co.uk/directory/users.php?id=idValue&data2=data2Value Into http://www.websitename.co.uk/directory/users?id=idValue&data2=data2Value HTAccess: RewriteE ...

Could Absolute Paths be the ultimate fix for problems with htaccess URL Rewrite?

Trying to accomplish a simple task of changing URLs from website.com/user?slug=usernameexample to website.com/user/usernameexample? I implemented the following code: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?us ...

Is it advisable to load 10,000 rows into memory within my Angular application?

Currently, I am in the process of creating a customer management tool using Angular.js that will allow me to directly load 10,000 customers into the $scope. This enables me to efficiently search for specific data and manipulate it without the need for serv ...

Sending post parameters from Angular and receiving JSON data from PHP using $http

Exploring the world of Angular and delving into the realm of $http, I find myself faced with a perplexing challenge: How to post parameters using $http (necessary for PHP to execute the call) Retrieve a JSON response from that call Here's what I&ap ...

The perfect combination: Symfony2 and AngularJS working together

Currently, I have been working on a web app that utilizes Symfony2 and recently integrated AngularJS. Within this app, there is an input field where users can filter products by name. The issue arises when it comes to querying the database in PHP and passi ...

Unable to successfully redirect index.php to the root domain from the specified URL

Greetings to the stackoverflow community users, I am facing a little trouble with a URL redirection issue. Currently, my webpage URLs include index.php (), and I am attempting to redirect it to the root domain using an htaccess file. I have added the co ...

After relocating to the live server, Codeigniter routing is now displaying the error message "file not found"

In the main directory, I have an .htaccess file with the following contents: RewriteEngine on RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.ph ...

Using symbolic links prior to the controller/method in CodeIgniter

Feeling a bit stuck and in need of help, I've decided it's time to reach out for some assistance. Here's my situation - I've been attempting to use a symbolic link to mask my URL like this: www.website.com/uk/controllers/method/etc www ...

Tips for structuring a SQL query result in an AngularJS application

Forgive me if this code is not up to par with the best standards, as I am still relatively new to angular.js. The issue I am facing is that when the data is returned from my query, it appears as a block of text. Despite using echo statements in search.php ...