htaccess with Angular and Phalcon - angularjs

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

Related

htaccess with two indexes for locales

I am using Reactjs client-side, and I have two indexes index.html and indexAR.html I am doing it for SEO for every language make an index.
URL example: example.com/ar/{pages}
URL example: example.com/en/{pages}
so I need to tell the HTTP server if find ar in URL returns indexAR.html if anything else returns index.html
#.htaccess
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html?path=$1 [NC,L,QSA]
Have your htaccess rules file in following manner. make sure your htaccess rules file is present along with ar, en folders(not inside them, besides them).
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ar indexAR.html [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.html?path=$1 [NC,L,QSA]

Ignore folders in htaccess

I'm making a webapp using AngularJS. For routing, I used a htaccess rewrite rule to allow direct access to routing pages (for example going directly to /settings). However there is one folder I would like to exclude from this rule, so I can check if a file exists (now every URL returns 200). I've tried every combination of rewrite conditions and rules I could find, but no luck.
So far this is my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
#Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} !(orders)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
</IfModule>
ErrorDocument 404 /404.php
I would appreciate any help
EDIT:
I would like to exclude all files and folders under orders
Try
RewriteCond %{REQUEST_URI} !^/orders [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
%{REQUEST_URI} always starts with a / and because you aren't specifying something like .* at the beginning of the regex, you have to have the slash. I also added the [NC] flag so that a url like "/ORDerS" would be skipped as well.

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 ignore rewrite to api url

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

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