htaccess ignore rewrite to api url - angularjs

I’ve build the front end of a site using angular and have created the cms through Kirby. Kirby allows you to build a json api from the structure you create with the cms, like so.
What I’m trying to do now is use the api provided by Kirby which is:
address.com/projects/api
within my angular app. I’m using html5 and htaccess to rewrite my urls so I dont need to use a hash within the url and page refreshes on a subpage get redirected to the appropriate angular view.
address.com/work
address.com/about
address.com/single-project
my htaccess looks like this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html
Kirby requires its own htaccess to serve pages also, so my question... Is it possible to allow access to my json template at address.com/projects/api and still preserve the angular clean urls?

If I'm understanding you correctly, you want to ignore /projects/api? Then you can do this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/projects/api [NC]
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]

You can add more exclusions in your negative RewriteCond:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/(index|projects/api) [NC]
RewriteRule ^ index.html [L]

Solved it shortly after posting #facepalm.The additional line that did the trick for anyone in the same or similar situation
RewriteRule (api) index.php

I have the same issue but none of the above solved my problem.
My current .htaccess is as shown below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^app.mydomain.com [NC]
RewriteRule ^(.*)$ http://www.app.mydomain.com/$1 [L,R=301]
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html
my API folder path is http://www.app.mydomain/api

Related

.htaccess How to redirect to index.html with params but the param value is same as a folder name

The routing url is like 'my_website/:param'
But my location contain a folder name is as same as the params.
So when I browse with http://my_website/apple.
The browser will display the folder instead of pass the value 'apple' to the web.
How should I set the .htaccess?Thanks
The following only work when the folder with same value is not exist.
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) index.html [NC,L]
</ifModule>
Based on your shown samples, could you please try following. Please clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.html [L]
OR if you are looking only for link which starts with apple then try following.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^apple index.html [NC,L]
Problem in OP's tried code: RewriteRule ^(.*) index.html [NC,L] will create an loop because there is no condition mentioned, so its going true each time for each uri.

Rewrite url for Angular app within sub-folder

I've got an Angular app using html5mode, but it's currently sitting within a sub-folder of a site: http://somesite.com/za/
So I've managed to rewrite the url to direct to my assets folder correctly, but I'm still getting 404s when I hit refresh or navigate directly to a page.
So this works: http://somesite.com/za/assets/js/bundle.js
This doesn't work, 404: http://somesite.com/za/home
.htaccess
RewriteEngine On
#This apparently creates a redirect loop for angular
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(za/.*) /za/index.html [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:[^/]*/)*([^/.]+\.(?:jpe?g|gif|bmp|png||css|js))$ za/assets/$1 [R=301,L,NC]
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
So where is the rewrite failing to redirect to index.html when I hit or refresh a page?
Solved it with a more elegant rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /za/index.html [NC,L]
However, this does give read access to other files and directories, so it can have security issues.

htaccess with Angular and Phalcon

I'm trying to switch the frontend of my Phalcon app to AngularJS. I'm having some troubles with the .htaccess file though. Below is what I currently have.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/.* api/index.php [L,QSA]
RewriteCond $1 !^(api)
RewriteRule ^(.*)$ public/index.php?_url=/$1 [QSA,L]
It routes to Phalcon, but all the .css and .js files in the public folder now throw a 404. What am I doing wrong here?
If you want to ignore files and directories you need to add extra rewrite rules to your .htaccess. if you want to ignore extensions as well, there's another rewrite rule for that.
https://stackoverflow.com/a/17029882 will give you the information you need. In your case
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
is enough and in most cases also the files and directories are not rewritten with this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

rewrite rule for htaccess to two seperate directories

On a single server i have a directory called app which has angular and another directory called api which has laravel installed.
I have the following for the angular code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1 [L]
which works fine now i need to add another rule where in angular i can use angulars $http to call www.domain.com/api/users and it will point to api/public/index.php which will in turn call the router and forward it to the correct controller
Has anyone done something like this?
You can insert a new rule for /api/users:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/.+$ /api/public/index.php [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^((?!app/).*)$ /app/$1 [L,NC]

Rewrite rule - redirect dynamic url to a subdirectory without loop

I have these possible path to images:
/media/thumbnail/img.jpg
or
/media/thumbnail/product/img.jpg
or
/media/thumbnail/product/some-text/img.jpg
All have to be redirected to:
/media/thumbnail/uploads/img.jpg
I tried this:
RewriteCond %{REQUEST_FILENAME} \.(png|jpg|jpeg|gif)$
RewriteRule ^/media/(.*)/(.*)$ /media/$1/uploads/$2 [L,R]
but i get an error loading the page due to a loop so I need to stop if the path contains a folder called uploads.
EDIT:
There's another problem, the rule I'm asking for must works with this one:
RewriteCond %{REQUEST_FILENAME} .(png|jpg|jpeg|gif)$
RewriteRule ^(.*)$ uploads/$1 [L,NC,QSA]
Which redirects /img.jpg to /upoloads/img.jpg
Thanks in advance
RewriteRule in your .htaccess doesn't match leading slash.
Change your code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^./]+\.(?:png|jpe?g|gif)$ /uploads%{REQUEST_URI} [L,NC]
RewriteCond %{REQUEST_FILENAME} /(?=[^/]*$)([^.]+\.(?:png|jpe?g|gif))$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^media/([^/]+)/(.*)$ /media/$1/uploads/%1 [L,R]

Resources