.htaccess for subfolder of AngularJS app with HTML5 mode - angularjs

Overview :
I have an AngularJS application that uses $locationProvider.html5Mode(true) and it is served from an Apache server. Until now I used the source code to be accessed from the others and I just needed a redirect to index.html for the angularJS's HTML5 mode. So I had this for the .htaccess file for my /app folder.
.htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</IfModule>
Modifications :
I then added Grunt for minifying the whole solution that is generated in the /dist folder of the app(so now the minified solution is found at /app/dist). I would now like to route the whole traffic of my app to the minified version, without making the users change the link. So, in practice I would like to redirect every call from /app to /app/dist.
Directory Structure now :
app
dist
minified versions(css,html,js)
I tried using some conditions from here in /app's .htaccess and moved the other .htaccess file in /app/dist, but with no success. I don't even know if the two can be combined in this way. I know I can use the dist folder as /app instead, but I wouldn't really want this, as /app is a repo and it is way easier to keep it updated. So how can I manage to achieve this with redirecting?
Also, as a second thing, is it possible to put a flag somehow so I let alpha users use the normal (not-minified) version for better debugging?
Update:
I added a sample project for this here. I just kept the initial .htaccess files that i showed above and you can check that both versions(/test and /test/dist) work as stated above. Also, it probably has some unused code and dependencies, but I just cut the other parts from my project(don't judge :D).
For this I used the initial configuration with AngularJS 1.4.8 that could generate some errors like the one stated here and here. Feel free to update the AngularJS version to 1.5.8(the last stable release of Angular 1), but take in consideration the comments on Cosmin Ababei's post as well. Also, the project uses npm for installing grunt(and other extensions for grunt), bower for angular and some other basic components and grunt for building the minified version(actually, it's just concatenating resources in same files for now). If you need to change the dependencies, please don't forget to run bower install. If you want to regenerate the dist folder, don't forget to run npm install and grunt build.

I'll assume that inside the /dist directory you have index.html with the updated URLs which point to the minified resources and the correct <base> tag. Note that the <base> tag must point to the /dist folder - just append /dist to the value you're using for the uncompressed index.html and you'd be fine.
With those things in order, here's the updated .htaccess file:
RewriteEngine On
# Don't rewrite files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# If the ALPHA_USER cookie is set, serve the uncompressed files
RewriteCond %{HTTP_COOKIE} ALPHA_USER [NC]
RewriteRule ^ index.html [L]
# Rewrite everything
RewriteRule ^ dist/index.html [L]
You'll notice two things:
I've removed RewriteCond %{REQUEST_FILENAME} -d as most likely you don't need to serve whole directories
I've added the ALPHA_USER cookie which would redirect the users to the uncompressed files. This cookie can have any value you desire.

Inside public_html => /app => .htaccess try this :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ dist/index.html [L]
Note : HTML <base> tag should be point to the dist folder. I hope this solution will work as per your expectations.

Related

remove index.html from URL react app on S3 and CloudFront

I am hosting multiple react apps on one s3 bucket, that is connected to a CloudFront distro.
The react apps are placed in folders so for example the calculating app is located in /calculate in the S3 bucket.
I fixed issues that happened because react wasn't using paths in a relative way to the index.html (I added "homepage":"./" to the package.json). But I am struggling to remove the index.html from the URL for the end user.
Basically I want the index.html to appear when going on https://{domain}/calculate/ not https://{domain}/calculate/index.html . Does someone know how to fix this issue?
Thanks
You can make a file just called ".htaccess" in the root directory and add the following to it:
RewriteEngine on
# Remove .html (or htm) from visible URL (permanent redirect)
RewriteCond %{REQUEST_URI} ^/(.+)\.html?$ [nocase]
RewriteRule ^ /%1 [L,R=301]
# Quietly point back to the HTML file (temporary/undefined redirect):
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [END]

Angular deployment with no .htaccess

I'm trying to deploy a site, but the server this need deploying on can't have an .htaccess file. On our test server we have the following as the .htaccess file:
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html
How can we remove the need for the htaccess? It's fine to have the # in the URL if this is the only way?
I've found a way so posting the answer to help anyone with the same issue.
The index.html page was loading but all the assets, css and js files were 404ing, when building for production you can add a base-href which I added and pointed to the directory in the server.
ng build --prod --base-href /path/to/site/root/
This then worked, the only other thing I had to change was I was referencing some assets like this:
/assets/images/logo.png
Which I had to change to this for the base href to then work:
./assets/images/logo.png
Everything is working as I want it now without any .htaccess
The other thing to note is that without an .htaccess file the site will only work with the useHash enabled.

After page refresh react page not working

I have create a bundle of react project by npm run build and create a virtual host file which is pointing to build folder, the application is working fine with different route, but when I page refresh other then root route then page not working.
This image is without page Load:
https://i.stack.imgur.com/EnBOU.png
But after page load it giving me this error.
https://i.stack.imgur.com/GooRM.png
Hi Please make base href on build index.html file inside head tag like this.
<base href="/reactapp/" /> // your root folder path
and add a .htaccess file in the root, paste below code inside the .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html
# to allow html5 state links
RewriteRule ^ index.html [L]
</IfModule>
I hope it's useful for you.
Thanks

Deploying ReacjtJS App to Linux

I have created a test application on my Mac. After I run npm run build, it created a build directory. Here starts the problem.
I have uploaded build directory to linux running an apache server. But i don't see it working. Do i need to install node, npm or upload whole directory to get it working?
The strange thing is, after i upload the build directory to the root of web server, I only see the first page. I am using react-router and first page renders the first component. example.com works fine but example.com/browse or example.com/fruit/apple does not work. Think of that you are missing htaccess file.
So are there any docs to show how can i make this work? Because when i search for deployment the only think i see is npm run build and serve
I have solved the problem adding the following .htaccess file to my root directory
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ / [L,QSA]

.htaccess redirect to index.html and index.php doesn't work together

I have a project with angularjs for front end and slim for backend api. Therefore I want to map any api/(...) to server/index.php, and map anything else to index.html. However I can't get them work together with .htaccess. api/(...) will direct to index.html instead of index.php.
This is the htaccess I'm using:
RewriteEngine on
RewriteBase /
RewriteRule ^api/ server/index.php [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !\.private/
RewriteRule \.(jpg|png|js|css|html)$ - [L]
RewriteRule ^.* index.html [L]
This is the file structure I have now (with some files omitted):
root/
index.html
server/
index.php
.htaccess
I tried to add some random characters in .htaccess file, and got a 500 error, so the .htaccess file works. I also tried to remove the last line for index.html rule, and the api works after commenting out.
Please help me with this problem, thanks!
I finally figured out why from the best answer in this link -> view
The problem is that [L] command will not terminate the rewriting process totally, it will match from the beginning again. So in the first iteration, api/ will be changed to server/index.php. Then in the next iteration, server/index.php will be matched to index.html.
Apache will terminate rewriting process when the pattern and substitute are the same. So the way to work around it is to add termination rules. I update my htaccess, and now it works.
RewriteEngine on
# Final rule for assets
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !\.private/
RewriteRule \.(php|jpg|png|js|css|html)$ - [L]
# Final rule for back end index
RewriteRule ^server/index\.php$ - [L]
# Redirect to back end index
RewriteRule ^api$ server/index.php [L]
RewriteRule ^api/.* server/index.php [L]
# Final rule for front end index
RewriteRule ^index\.html$ - [L]
# Redirect to front end index
RewriteRule ^.* index.html [L]
I know that the question got answered but it might help those people who want to host their React (front end) and Laravel (backend) on cpanel of GoDady.
Assuming that you people know the basics of cpanel, first go to the public_html and to your folder where your domain or subdomain is located.
you need to upload the application to the public html but remember when you are building react static files (npm run build) you need to define the domain name or the sub folder name with the key homepage in you package.json
or if you want to add the static files in sub folder then it is must to do
"homepage": "http://ghulam.covidbaml.com/yourSubFolderNameHere",
so, I am assuming my folders structure on the cpanel (public_html)
public_html/Static React build Files
public_html/ server (the server folder is also located in same place which contained laravel files)
Now we can write .htaccess which is really imported assuming godady linux hosting.
htaccessFile below:
Options -MultiViews
RewriteEngine on
# Rule to start the laravel api on work
RewriteRule ^server/public/index\.php$ - [L]
# Redirect to index.php
RewriteRule ^api$ server/public/index.php [L]
RewriteRule ^api/.* server/public/index.php [L]
# now I am redirecting the React index file (index.html)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
note:
RewriteRule ^ index.html [QSA,L]
The above command is for the purpose of react routing that it doesn't give 404 error while you refresh the page.

Resources