vhosts conf for single page app - angularjs

I have an angular app that I'm serving locally using apache. My vhosts.conf is:
<VirtualHost *:80>
ServerName my.app.com
DirectoryIndex index.html
DocumentRoot /export/www/app
<Directory "/export/www/app">
order allow,deny
allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html/$1 [L]
</Directory>
</VirtualHost>
If I go to the root of the app everything loads fine. I do some navigation within the app which brings me to my.app.com/some/location and still no problem.
However if I reload the page at this point I get a 404 error. To get back to the point I want I need to either go back to the root and navigate via the app again or else search by url my.app.com/#/some/location
Is there something that I can do with a rewrite rule so that I don't need the hash to reload the route? I tried to edit my existing rule to:
RewriteRule ^(.*)$ index.html/#/$1 [L]
Any ideas greatly appreciated
C

The issue was that I was using a newer version of apache where the config rules have changed. My updated vhosts now reads
<VirtualHost *:80>
ServerName my.app.com
DirectoryIndex index.html
DocumentRoot /export/www/app
<Directory "/export/www/app">
order allow,deny
allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
</Directory>
</VirtualHost>

It's weird that my.app.com/some/location works. With AcceptPAthInfo ON, httpd would serve index.html and pass PATH_INFO of /some/location but that shouldn't do the right thing on the client-side JS.
The rewrite you attempted seems like it shiould replace your existing rewrite entirely. But, you'd want to redirect explicitly to non-relative /index.html and add the [R] flag to make sure the redirect goes all the way to the client. Otherwise, it's just an internal substitution, and you need your browser to read the # because that's only handled on the client side.

Related

My React JS app crashes when I realod the page using subdomain apache ubuntu server 20

could someone else help to me how I would configure apache/htaccess/reactapp in order to avoid the webapp crashes
My subdomain is eg. https://subdomain.example.com (it's working fine) the page loaded well if I refresh the page the page loaded well too. I think just right it is because the index.html is serving route home page.
But if I go to eg. https://subdomain.example.com/about (it's working fine as well) but here is the problem when I reload the page "Not Found - The requested URL was not found on this server". all pages after the domain crashes when I refresh the page
my htaccess
`
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
`
Server Ubuntu server 20.04
virtual host
`
<VirtualHost IP:443>
ServerName subdomain.example.com
DocumentRoot /var/www/my-app
<Directory /var/www/my-app>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
`
I just added this one in my htaccess root folder of ReactJS app
The credits for this guy: https://putridparrot.com/blog/react-router-dom-direct-url-or-refresh-results-in-404/
<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>

Configuring htaccess file for React Router on Apache Server

I have deployed a React app with React Router to my Bluehost server, and need to configure the htaccess file to redirect all of my routed URLs (/portfolio, /about, etc) to index.html instead of trying to fetch a new file from the server and throwing a 404.
I have read about countless similar problems in which the solution seems to be to add this into your 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>
I tried this, but I am still getting 404's when I try to visit any page of my site directly that isn't the homepage. I'm wondering if there is anything else in my existing htaccess file that is preventing the above code from working?
There was some code already in there from Bluehost, and I see another IfModule statement, so I'm wondering if that one is overwriting the first one. However I am afraid to edit it and break something, as it clearly says "do not edit." Here is my full htaccess code:
Header always set Content-Security-Policy: upgrade-insecure-requests
<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>
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
# END WordPress
Any ideas? I've double-checked that my BrowserRouter is set up correctly and also tried a few other htaccess configurations. I want to avoid using HashRouter or Node if possible but am getting frustrated. I can provide my React code as well if needed, but I'm pretty sure the error is not with the React setup.
You can create a virtual host file in the /etc/apache/sites-available folder and add this:
<VirtualHost *:8080>
ServerName example.com
DocumentRoot /var/www/httpd/example.com
<Directory "/var/www/httpd/example.com">
...
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]
</Directory>
</VirtualHost>
This worked for me

Angularjs HTML5 mode on doesnt work even after rewriting URLs on server side

In my Angularjs application the '#' was present initially in the URL. So I set the HTML5 mode for the app to true and also made the necessary changes all over the application code for URLs. PFB my code for that.
app.config(function($locationProvider){ $locationProvider.html5Mode({
enabled: true,
requireBase: false
});
});
Now as per this link I made the necessary changes in my configuration file. Those changes are as follows:
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1 [L]
The Problem:
The application still won't work and it still doesn't refresh the URL i.e. I still get the 404: Page not found error.
Please help me regarding this.
Well, setting requireBase: true and inserting <base href="/">(or what ever else, depending on the env) in the header, and using this settings
RewriteEngine On
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]
works for me in every places that i ever used it. Your DirectoryIndex must be set to index.html or the name of the file you are using and this is very important, your server must always send your initial file.
The following is an example, of what i'm using right now, but it is in my pc, no public hosting.
<VirtualHost 127.0.0.1:80>
ServerName www.myproject.localhost
DocumentRoot "d:/Work/myproject/"
DirectoryIndex index.html
<Directory "d:/Work/myproject/">
Options +FollowSymlinks
RewriteEngine On
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]
AllowOverride All
Allow from All
Require all granted
</Directory>
</VirtualHost>

CakePHP 2.x on CentOS 6

I am having issues with CakePHP 2.4.3 (tried on 2.5.2 too, same problem) installation on CentOS. Database can be accessed (read/write) from cakephp.
Tried for the past few hours to fix it, but here is where I am stuck :
1) This error keeps on appearing. "URL rewriting is not properly configured on your server"
- I have check, and the file is accessible from the site, "www.mysite.com/cake/app/webroot/css/cake.generic.css"
- I have updated httpd.conf to have "LoadModule rewrite_module modules/mod_rewrite.so"
- The files ".htaccess" are in 3 folders :
/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
/app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
httpd.conf
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
2) My controllers cannot be called directly. It needs the "index.php" to be there.
Example :
Works on : "www.mysite.com/cake/index.php/grabs/myFunction"
Unable to work on : "www.mysite.com/cake/grabs/myFunction"
Please advise.
An important notice for people like me, who struggled with problem exactly like this one.
One of the POSSIBLE REASON for this behavior is when there are two or more CakePHP apps residing on one server. Anyway, try changing this line:
// Prefix each application on the same server with a different string, to avoid Memcache and APC conflicts.
$prefix = 'myapp_';
From your /app/Config/core.php to something distinct, let's say:
$prefix = 'coolRpgGame_';
It really saved my life and it worked immediately.

Running a CakePHP project on Localhost

I have downloaded a CakePHP project from an online server and need to make it work locally, to do some changes.
I'm facing a problem similar to this one and have tried all possible solutions, with no luck.
Here's the situations:
Site home is in localhost/xpto (c:\xampp\htdocs\xpto) -> here, the site appears unformatted (no CSS) and too slow.
If I access localhost/xpto/something, I get the site with the CSS, but some links wont work.
I have mod_rewrite loaded (phpinfo()) together with the 3 .htaccess files, but still experience this problem.
Is there any known issue that I can explore to fix my site?
Please redirect me to any guides or tutorials that you may feel relevant to my issue
Use RewriteBase directive in .htaccess.
e.g my dev space is on my localhost, is located in /home/ati/public_html/cakerbs, under a subdir in my userdir. The rewriteBase looks like as follows in the root of the cake:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~ati/cakerbs
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
in the app directory .htacces:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~ati/cakerbs
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
in the webroot directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~ati/cakerbs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Check your XAMPP apache settings, it's very likely your current config is blocking things like the .htaccess files (I know EasyPHP does this by default with Cake). What I do to get around this error is to add a VirtualHost to my config like such:
<VirtualHost *>
DocumentRoot "C:/xampp/htdocs/xpto/"
ServerName xpto.dev
ServerAlias www.xpto.dev
ErrorLog "logs/xpto-error.log"
CustomLog "logs/xtpo-access.log" common
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
It may be better to update your whole config properly, but seeing as it's a dev environment for me, this does just fine. (Don't forget to add any hosts to your windows hosts file)
Then just open your favourite browser and go to http://xpto.dev/ and it should load up.
This may help your more: http://ailoo.net/2008/07/set-up-multiple-virtual-hosts-on-xampp-for-windows/

Resources