最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - htaccess for React app using React router hosted on cpanel - Stack Overflow

programmeradmin3浏览0评论

So I have a react single page application that I am hosting on cpanel. I have a pretty mon issue from what I have been researching but none of the post in SO have helped me so far.

I am using React Router which I know is purely client-side routing. The problem is when the user refreshes the page on a url such as I get the stand Not Found error 404.

My folder structure in cpanel is as follows:

home/mysite
public_html
  all the folder from build folder (after npm run build locally)
  index.html(important for the post)
  .htaccess(important for the post)

The htaccess file is as follows:

RewriteBase /
RewriteRule ^index\.html$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

I was hoping this was going to redirect all the requests to index.html and therefore react would know which ponent to render once that has been redirected.

It isn't and I'm a bit lost on what I'm doing wrong.

Any help is appreciated :)

So I have a react single page application that I am hosting on cpanel. I have a pretty mon issue from what I have been researching but none of the post in SO have helped me so far.

I am using React Router which I know is purely client-side routing. The problem is when the user refreshes the page on a url such as https://example./privacypolicy I get the stand Not Found error 404.

My folder structure in cpanel is as follows:

home/mysite
public_html
  all the folder from build folder (after npm run build locally)
  index.html(important for the post)
  .htaccess(important for the post)

The htaccess file is as follows:

RewriteBase /
RewriteRule ^index\.html$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

I was hoping this was going to redirect all the requests to index.html and therefore react would know which ponent to render once that has been redirected.

It isn't and I'm a bit lost on what I'm doing wrong.

Any help is appreciated :)

Share Improve this question asked Jun 10, 2022 at 14:59 Jorge GuerreiroJorge Guerreiro 84510 silver badges28 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

To host ReactJS website on cPanel it is important to use homepage attribute in your package.json. For example, you can use:

"homepage": "http://example.tld"

Once you are done with setting up the homepage attribute, then run the npm build mand to pile your code.

npm run build

This mand will generate a folder named build in your project's root folder. Then upload the build folder contents to cPanel and use the below .htaccess file configuration.

<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>
发布评论

评论列表(0)

  1. 暂无评论