如何解决手动编写.htaccess时React无法路由
我正在尝试设置.htaccess
文件以路由React
页。这是我的路由器:
<Route path = "/" component = {App}>
<Header propContext={{useUserContext,setUserContext}}/>
<Route exact path="/" component={Index} />
<Route path="/index" component={Index} />
<Route path="/xxx/:id" component={Xxx} />
<Route path="/somepath" component={Somepath} />
<Route path="/anotherpath" component={Anotherpath} />
<Route path="/yyy/:id" component={Yyy} />
<Footer />
</Route>
这是我在服务器上的htaccess
文件:
<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>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://example.com%{REQUEST_URI} [R=301,L]
当我手动编写URL时,https://example.com/index
,https://example.com/somepath
,https://example.com/anotherpath
URL可以正常工作。但是https://example.com/xxx/14
或https://example.com/yyy/53
URL不起作用,显示空白页,并在控制台上引发以下错误:
Uncaught SyntaxError: Unexpected token '<'
Uncaught SyntaxError: Unexpected token '>'
Manifest: Line: 1,column: 1,Syntax error.
我找到了this的答案,但在here上有人建议htaccess。
解决方法
我已经用HashRouter来解决了这个问题:
<HashRouter>
<Route path = "/" component = {App}>
<Header propContext={{useUserContext,setUserContext}}/>
<Route exact path="/" component={Index} />
<Route path="/index" component={Index} />
<Route path="/xxx/:id" component={Xxx} />
<Route path="/somepath" component={Somepath} />
<Route path="/anotherpath" component={Anotherpath} />
<Route path="/yyy/:id" component={Yyy} />
<Footer />
</Route>
</HashRouter>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。