How to set up apache2 for react - apache

I have react app running on apache2.
In my app, there are 3 pages: home, about, register
I have 3 route like this:
<Route path="/home" component={....} />
<Route path="/about" component={.....} />
<Route path="/register" component={....} />
So, app works perfectly, when I go to www.onlyexampleurl.com, app is loaded correctly. I can list in home, about, register fine.
But, problem is, when I go directly to www.onlyexampleurl.com/about i am getting 404 from apache, because app is download from root path '/'.
How can I fix it? When I put www.onlyexampleurl.com/about to browser I want load about page from my app, but I am gettint 404 from apache.
Thank you for any help!

You need to redirect all requests to your entry file (assume it's index.html). Put in .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
This is standard solution for Single Page Apps.

This is a late response to the question, but I though I'd share what worked.for me, in case anyone bumps into the same problem.
This is assuming Ubuntu OS, I have not been successful for Linux:
I've followed certain steps from this post from Reddit and I can attest this works for making SPAs run in Apache web server with proper routing.
To quote:
1) In the apache2.conf located in etc/apache2/ I turned AllowOverride to All. I also added Servername localhost in the file. (I somehow skipped this last sentence, but still it worked fine in the end)
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
2) In the hosts.debian.tmpl file found in /etc/cloud/templates I added 127.0.0.1 <yourdomain.com> to the file where the other ips of the similiarity are.
127.0.0.1 yourdomain.com
3) I ran sudo a2enmod rewrite. Then I restarted the apache server via service apache2 restart. This turns on mod_rewrite.
Lastly inside of my project folder /var/www/html , along side my index.html file I created a .htaccess file and added in the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ / [L,QSA]
End quote
Assuming you've run npm run build and then copied all the contents inside the build folder to your public directory, this should work.
When I first started web dev, I wasn't really sure I made the VirtualHost config right, so what I usually did was first to make a dummy index.html with, say, Hello World example. When I've confirmed that I'm able to see Hello World in the browser (which means I got the VirtualHost config right), I dump the contents of the build folder to where the dummy index.html was.
Hope this helps you now as much as it did for me!

Related

I can't pass URL Parameters with Vue-Router and Apache2?

I am deploying an Vue3 Applikation with Vue-Router 4 on an Apache2 Server.
I observed some obstacles while doing it.
So my main problem is, that I can't pass Url Params to Vue via the Apache2 Server.
While developing the app and debugging it at localhost, everything worked perfectly.
e.g localhost:8080/myParam
Matches and redirects me to the index.html and passes the myParam to vue-router and the correct view is displayed.
So far so good.
Now I am deploying on an apache2 server and exactly that redirecting doesnt work.
I have read the docu and found this description:
https://router.vuejs.org/guide/essentials/history-mode.html#apache
The redirect works. But when I type in example.org/browse/myParam it redirects me to example.org/browse/index.html and my myParam is gone.
My Setup:
Apache 2.4, Vue3, Vue-Router4
The App is a Subapplication of a bigger project (thatswhy the app is not served in the root folder).
At the moment my App is running in a docker-container on the Server at port 8895, so I needed to create a proxypass in the apache.conf.
Because the files were not actually accessible at the server I also couldnt just use {REQUEST_FILE} (as shown in the vue-router example), so I solved it kind of hacky via the request uri and a pattern matching. But in my view that shouldn't be the problem, right?
#------------#
# BROWSER #
#------------#
ProxyPass /browse/ http://localhost:8895/
ProxyPassReverse /browse/ http://localhost:8895/
RewriteEngine On
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteRule ^/browse/index\.html$ - [L]
RewriteCond %{REQUEST_URI} !/browse/js/.*.js(.map)?$
RewriteCond %{REQUEST_URI} !/browse/css/.*.css$
RewriteRule ^/browse/ https://example.org/browse/index.html [L]
</IfModule>
Conclusion: I can visit my website, but only the homepage. I can't visit https://example.org/browse/Person/Sebastian directly. I can only go to https://example.org/browse/index.html and the click myself through the application.
I can't imagine that there are not many other people who had a similar problem, but I couldn't find any.

How to make the Website run Index.html file using .htaccess?

I am trying to run a index.html file automaticlly once clicking on the folder on the Website using .htaccess
code:
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteRule ^$ /index.html [R,L]
and then i restarted apache and refreshed the website but the folder disappeared. i try to wrote the whole link in the Url then comes (Internal Server Error)
i have here centOS 7 as Operating system runing apache
thanks for helping me and excuse please my english :D
best regards
Jackob
Yeah it works (:
Adding the following to my httpd.conf (if you have access to it) is considered better form, causes less server overhead and has the exact same effect:
<Directory /myDirectory>
DirectoryIndex index.html
</Directory>

How do I set short URL's for MediaWiki?

I used this guide to install mediawiki version 1.27.0. The install went great, and everything is working.
Currently in the url it still has the index.php in the url, like this: http://example.com/index.php/Main_Page
There is documentation here, and here, but there are sections of these instructions that don't make sense to me.
Here's what I'm using:
Ubuntu 14.04
Apache2
mediawiki 1.27.0
My VM is on Amazon AWS
I'll go through the instructions, and list the problems I'm having.
1. In the instructions they assume that your mediawiki install is setup in /w.
Well, mine is installed here /var/www/html/
2. Find the right apache2 files
I have root access so I'm assuming I will be using two files /etc/apache2/apache2.conf for the AllowOverride All, and /etc/apache2/sites-available/000-default.conf to edit anything related to the VirtualHost section.
3. Setting the rewrite rules
In my /etc/apache2/sites-available/000-default.conf file, in the <VirtualHost *:80> section, I added these lines
RewriteEngine On
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
I understand that this shouldn't work with my setup, because it's using the /wiki, and /w filepaths, and those don't even exist, but I'm not sure what I should put.
4. Change LocalSettings.php
This step also has me confused for the same reason. It says to set $wgScriptPath = "/w";, which is a directory I don't have, and set $wgArticlePath = "/wiki/$1";, which is not only a directory I don't have, but that variable doesn't even exist in the LocalSettings.php file.
I think if I understood the first step, of changing my directory to /w, then I could get the rest, but obviously, I can't change /var/www/html, to /w, without breaking everything. Thanks in advance.
For all who are not running MediaWiki in the default subdirectoy on your HOST (means not like /w/ or /wiki/) configure your .htaccess like:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ index.php [L]
And add the article path to your LocalSettings.php:
$wgArticlePath = "/$1";

Laravel 5 pages are not being displayed on live server

Main Points:
- I am using Laravel 5, apache, and ubuntu
- mysite.com is WORKING
- mysite.com/login works on localhost, but not on live server
I'm building my first website, and my home page is 'live' (it is up and working) My problems arise when I want to create a 'mywebsite/login' page. I am getting a 404 Not Found error on live, when the page works on my local machine.
I believe this is happening because my .htaccess file not rewriting correctly in regards to my folder structure. The problem is I'm not able to figure it out, as this is all new to me.
My document root is /var/www/laravel/public
My .htaccess is in the above folder.
It looks like this
<IfModule mod_rewrite.c>
<IfModule mod_negotians.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I have a virtual host file for my site in /etc/apache2/sites-available
It's contents look like this
<VirtualHost *:80>
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel/public>
Options -Indexes +IncludesNOEXEC +FollowSymLinks +ExecCGI
Allow from all
Allowoverride All
</Directory>
RewriteEngine on
<Directory /var/www/laravel>
AllowOverride All
</Directory>
**A few more lines...**
</VirtualHost>
I have also enabled mod_rewrite on the server.
I am thinking that I have a smaller 'pointing' error here somewhere, but I'm not experienced enough with these files to see it. Is there anyone that can give me a hand?
Thanks
Move All the files on xyz(your folder name for which domain/ subdomain is mapped) folder
Then follow these steps
1 – go to public folder
2 – copy .htacsses file(this make your routes work)
3 -paste it in xyz folder
4 change server.php to index.php
5 – enjoy and happy coding
This is working fine with hostgator.in shared hosting.
When you push your code from local to server then sometimes it's not working the same. For that please try following commands. It works for me and I hope it works for everyone as well.
$ sudo chmod 777 -R folderpath //To give 777 permission to your project
$ php artisan config:clear //To remove the configuration cache
$ php artisan view:clear //To remove the views blade cache
$ php artisan route:clear //To remove the route cache
$ php artisan clear-compiled //To clear the compiled classes and services application cache
// If you use passport then:
$ php artisan passport:install
// At last:
$ composer update

Apache DirectorySlash seems not to be working

I am facing problems with a site that I am trying to deploy. The site is working fine on my development host, but fails on deployment. Both hosts have a very similar configuration, both are Ubuntu Linux distros (dev: 11.10, deploy: 10.04), both use apache2, etc.
Both sites have mod_dir enabled, but the deployment one seems not to add the trailing slash to directory names, while the development one does. So, when I enter this URL (removed http to avoid silly stackoverflow antispam filtering):
devel.mydomain.com/admin
The development host redirects it to:
devel.mydomain.com/admin/
In deployment http://mydomain.com/admin is not redirected to mydomain.com/admin/ for unknown reasons, and I end with a 404 error. Of course if I enter mydomain.com/admin/, adding the trailing slash by hand, it works as expected. But I rather like to redirect also mydomain.com/admin to mydomain.com/admin/
The question is WHY in devel mod_dir seems to be doing the redirection and in deployment it does not.
I have done a grep within configuration files to see if the DirectorySlash directive was being disabled somewhere and found nothing. It is neither enabled explicitly on devel, so I think it should be on by default. Anyway I add this to my .htaccess file on deployment host:
DirectorySlash on
But it did not work neither.
Any hints?
The /admin folder is password protected. I did not think that this was relevant, but in fact it is.
Also I have these rules on my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
The problem is related to this - see here for a good explanation.
Basically, when Apache generates a 401 error to ask the browser for a password, it also generates another request that does not use any folder, directory, or symlink, so it is redirected to index.php.
The workaround is to add this to the .htaccess file on site root folder:
ErrorDocument 401 /error/null.html
ErrorDocument 403 /error/null.html
and create that file, for example like this:
touch ./error/null.html
These are not best practices but they'll do for now while I look for a better solution.