AngularJS navigating to a route gives 404 in Apache - angularjs

When loading a page using Angular an Apache myserver.com everything works fine, when going to a subroute from the main page myserver.com/credits by clicking a link it'll work as well.
However, if I try to go directly to myserver.com/credits from the browser navigation bar it'll return a 404 error message:
I'm aware that by working with node I can configure this so that it does work, however, my company website runs in an apache server which I have no access to.
I could make it so the server redirects to the main page myserver.com like so:
.htaccess
ErrorDocument 404 /index.html
However the optimal resolution would be that going to myserver.com/credits works outright.
Is there a way to make Apache behave this way? And if so, how?

The answer that solves this question can be found here:
https://stackoverflow.com/a/22740184/1224232
I have flagged this question as a duplicate. I have tested it and it works perfectly.
Special thanks to Kevin B for directing me to the answer, and to Rajasaur for providing the answer.

Created a .htaccess file in root directory if not exist.
then add the following code in it
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# not rewrite css, js and images
RewriteCond %{REQUEST_URI} !\.(?:css|js|map|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ /index.html?path=$1 [NC,L,QSA]

Related

react-router not working on production build in LAMP

Hey has anyone experienced issues with react-router on a production build. I'm running a LAMP server and I've built my project put it in the http folder and when I go to a page I get Object not found!. I have setup react-router to go to certain pages based on certain conditions if I go to the root of the server i.e. localhost/ it loads the main page but then if I click on links it works fine as soon as you try and manually go to a link by typing in the search box I get the error message Object not found
This is my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
You need to configure your server to return index.html if file not found (404).
Example:
Let's say you have /users route the will show your users list.
It will be ok if first you'll go to / (which will return index.html by default) and then click some router link and it will redirect you to that page. It will work because you wont make any additional requests to the server.
But if you go to /users first - you'll request it from the server. In that case server will try to find users file and it will not be there.
If this configuration is not possible consider using hash router. The url will look like this /#/users

htaccess Redirect to a folder and then rewrite again

Hopefully, this is not duplicated.
I´m trying to bring structure to my site and be clean, I´m separating content. It is an angular app, I´mean 2 apps in one domain separated thru the folder name
So I´m trying following:
domain.xy/folder
should internal redirect to
domain.xy/anotherfolder
after that, I´m using angular hashtag links and I don´t want them to be shown. When the file or folder doesn´t exist then add a #
domain.xy/link1
becomes to
domain.xy/#/link1
I´m trying around with this code, and it´s working fine if not separated. I thought When the first rule passed with flag [L] It will do another loop with the rewritten URL but Here I got the error that this is not working, and like he is not passing the first rule when I type something like
domain.xy/folder/link1
This is the latest code I was trying. Maybe someone sees the error in my thinking and give me a hint or advise? Thanks in advance.
RewriteEngine On
Options FollowSymLinks
RewriteBase /
# Rewrites folder
RewriteRule ^supplier/(.*)$ /_supplier/$1 [L,NC]
RewriteRule ^agency/(.*)$ /_agency/$1 [L,NC]
# Folder doesn`t exist then it is a angular link
# rewrite with # tag
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
I found myself with a false use of angular $locationProvider.html5Mode(true); and rewriting URLs.
I changed now the structure for the use of hashbang in my project. Now it's working correctly.
Thank you all for viewing.

Getting 403 Forbidden on direct access to page

I have a website that has 3 basic pages: Home, About and Contact. The website is built using AngularJS and I am using ui.router for the routing.
Everything is working OK if you click on the navigation options, but when you refresh while on one of pages, /about or /contact, it throws 403 forbidden. Same issue if you directly try to access one of these pages: http://pricebyitem.com/about
This is run on Apache 2.4 - I'm not sure what I need to configure to make the direct access work.
Website: http://pricebyitem.com
Im sure this has been answered elsewhere on StackOverfow and therefore this question should be closed.
You need to implement HTML5 pushstate in Apache. This will return your index.html for each route requested.
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.html

Refreshing a URL in AngularJS

We have built an application with AngularJS, it is currently hosted on a shared cPanel hosting. However, whenever we do a refresh on any page we are at with ? in the url. It will break.
I have read extensively on Stackoverflow, I understand it is because of the browser doesn't know which page to load and hence we need to set the base url in the header. We tried setting the base url and did some htaccess settings but it is still not working.
Try to click on any link from the website and refresh that page. You will notice it failed to work.
Any kind soul can point us to the right directions on how we can solve this?
Edit 1:
I have these in htaccess and notice the only url with ? will refuse to load correct.
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/api
# otherwise forward it to index.html
RewriteRule ^.*$ - [NC,L]
RewriteRule ^/* /index.html [NC,L]
I tried to reproduce the Issue but its not showing any # tag in the URL as mentioned. Can you please explain in details.
The issue didn't come from htaccess but the folder name. For instance,
config(function($stateProvider) {
$stateProvider.state('search', {
url: '/search?page&loc&seaters&owner&min_price&max_price',
The problem is when our folder is also call search, Apache will attempt to go to search folder first (due to the htaccess) and because they can find the folder, it will read the folder directory.
We fixed it by naming the folder example: search-html and change all the templateUrl required to search-html and it solves the issue.

Htaccess PHP Redirecting. Unable to do so.. 404 not found error

I am working on a ask answer website and thanks to Alex, I got the permalinks I wanted to have on the website (PHP Permalinks.. how to change?).
Here is the format..
Original: domainname.com/cat/how-are-you-|162
New permalinks: domainname.com/cat/how-are-you-|162.html
The new permalinks won't work i.e, it doesn't load the page and displays 404 Not found error. My knowledge of rewriterules are very basics, I did try some functions but to my dismay, they didn't work at all.
How do I redirect original to the new permalinks using htaccess file?
Thank you.
Edit1: #Ignacio - Currently, I haven't implemented it on the server as I don't want to lose traffic, but I can do it once again tonite and will update with the result.
#Pekka - Here is the .htaccess file http://pastebin.com/m23c7d2b6
You want to do two things:
Permanently redirect old permalinks to new permalinks and
Make new permalinks work.
To redirect the old permalinks, add something like this:
RewriteCond %{REQUEST_FILENAME} !.*\.html$
RewriteRule ^(.+/.+)$ $1.html [L,R=301]
To get the new permalinks to work, change this line:
RewriteRule ^([^/.]+)/([^/.]*)?$ view.php?title=$1&id=$2&%{QUERY_STRING}
to this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/([^/.]*)\.html?$ view.php?title=$1&id=$2&%{QUERY_STRING}

Resources