.htaccess 500 internal server error when routing api - reactjs

I'm currently running php on an apache server locally, with a React frontend.
This is how my current .htaccess is laid out:
Options -MultiViews
RewriteEngine On
RewriteRule ^api/(.*)$ api/$1\.php [L]
RewriteCond %{REQUEST_URI} !^/api.*?
RewriteRule ^ index.html [QSA,L]
The bottom condition is so that routing works in my React app. I'm then taking the production build and copying it into my htdocs.
The routing works, however, I want to be able to call the .php files inside my /api directory without using the file extension. So I want anything that comes after /api/ to be redirected to whatever is entered, followed by .php.
E.g. /api/authentication would go to /api/authentication.php, and /api/register would go to /api/register.php, and so on.
With this current setup, I'm getting a 500 internal server error when making requests to /api/authentication etc.
Is there something wrong with my .htaccess file?

Your first rule is looping as you're matching .*. You may use:
Options -MultiViews
RewriteEngine On
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteRule ^api/(.+)$ api/$1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^api index.html [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC] will skip rewriting when a URI ends with .php.

Related

What is the proper setup of an .htaccess file for headless WordPress and React site

I have a Wordpress site with a headless React frontend. With the default .htaccess file that Wordpress puts up on install, the site was not correctly handling requests to any page other than the root. Those requests ignored React altogether. I modified the .htaccess to point all requests to the index.html file like the following...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
This loads the React site just fine on all requests, but it's also failing on any api request to either wp-json or more importantly graphql. This means the page loads, but the content is essentially absent. How can I set up the .htaccess file to allow requests to those api endpoints on top of allowing the initial requests going to the index.html?
This seems to be working so far...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^graphql(.*)$ index.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Stole part of this from this answer...
How to redirect all API requests using .htaccess, while keeping asset requests intact?
I'm not sure I need that url=$1 part since I'm not using it elsewhere, but I'm not that good at regex and at least the request are getting handled correctly.

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

Handling URL routing with Apache and React Router

I have a pair of apps which will be running on a shared Apache host. The apps are housed in a common directory on the server. Each application is housed within a "build" directory within its main directory. The client directory should be the default display, while admin should display at example.com/app/admin. And, to further complicate matters, admin uses React Router for its internal routing.
Here's a visual representation of the goal:
admin
|- build
|- index.html (example.com/app/admin, example.com/app/admin/page1, etc.)
client
|- build
|- index.html (example.com/app)
.htaccess
So I need to load the target pages while also enabling React Router to handle the routing for admin. I've tried about every combination of RewriteCond and RewriteRule I can think of in the .htaccess, but no luck so far. I feel like I'm getting closer with the rules below, but I'm still not quite there.
// .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/admin/build
RewriteRule ^admin$ admin/build [L]
RewriteCond %{REQUEST_URI} !^(/client/build|/admin)
RewriteRule ^(.*)$ client/build [L]
</IfModule>
This shows /build in the address bar, and any request to /admin/(.*) fails to redirect the .css and .js file requests to the build folder (so it is instead looking for /admin/static/css/ instead of /admin/build/static/css), although they load successfully on requests to /admin.
EDIT: Changed "App1" to "client" and "App2" to "admin"; masking those serves no purpose, and the fact that the directory shares a name with the page might somehow be relevant.
Finally got this sorted out. Final rewrite below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} -f
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} !^/app/admin [OR]
RewriteCond %{REQUEST_URI} ^/app/admin/build
RewriteRule .* - [S=2]
RewriteRule admin/\w*/?$ admin/build/index.html [L]
RewriteRule admin/(.+)$ admin/build/$1 [L]
RewriteCond %{REQUEST_URI} !^(?:/app/client/build|/app/admin/build)
RewriteRule ^(.*)$ client/build/$1 [L]
</IfModule>

.htaccess - Redirect everything to homepage except one directory

I'm creating an AngularJS app and want to avoid the # in the URL. I learned that I needed to add the following .htaccess rules to make it work:
RewriteEngine On
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /#/$1 [L]
That worked great. However, now my calls to my web services are not working. The web service files are inside an /api/ directory, e.g. http://example.com/api/. My guess is that when the api calls try to access those files, they also get redirected and break. So how can I modify the rules above to redirect everything to the homepage, except links that are going to any file inside the api folder? I tried to find an existing answer to this, but while there were many similar ones, none of them was exactly what I needed. Thanks!
I found the code I needed:
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 ^ /
That's all you need. Got it from: https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/

.htaccess fine on localhost, 500 internal server error on webserver

I'm trying to redirect show files in folder/dist when accessing folder/ in a webapp using .htaccess.
This works as desired on localhost (xampp apache) but when pushed to the server (AWS Elastic Beanstalk Apache) I get a error code 500 Internal Server Error.
I've created the webapp using the Yeoman Angular generator and it's running the webapp on an Apache server. The project is in a subfolder of the whole site (http://url.com/pages/webapp). Grunt creates the application in the dist folder and I this content is what I want the visitor to see when accessing the root of the webapp.
Basically I want to redirect/rewrite http://url.com/pages/webapp/ to http://url.com/pages/webapp/dist/
Im trying to accomplish this with a .htaccess that looks like this:
RewriteEngine On
RewriteRule ^$ dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^dist/
RewriteRule ^(.*)$ dist/$1
The exact same htaccess-content is used in other projects on the same server and they work fine. Only projects created with this generator has this issue.
Is the problem in my htacceess-file, other part of webapp or Elastic Beanstalk...?
I have tried adding AllowOverride All (root directory) and LoadModule rewrite_module modules/mod_rewrite.so to /etc/httpd/conf/httpd.conf but it didn't work.
I also didn't find anything interesting in error_log
I realize the problem is probably hard to solve with the information provided but I am hoping someone else has had a similar issue.
Thanks
Try this code in root .htaccess:
RewriteEngine On
RewriteRule ^$ dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/dist/ [NC]
RewriteRule ^(.*)$ dist/$1 [L]
Note leading / before dist in RewriteCond.

Resources