如何在Apache中重定向和发送301代码

im9ewurl  于 7个月前  发布在  Apache
关注(0)|答案(1)|浏览(64)

我正在构建一个基于Map的单页面应用程序,它会在用户浏览时更改其URL。我的主要问题是Google Chrome的Lighthouse不喜欢我使用ErrorDocument 404 Apache规则重定向到根页面并保留URL。我认为404错误对SEO来说也不是很好,但我只是猜测。
我想Apache重定向我的用户到/,如果请求的URL不是一个文件,并发送一个合适的status_code(301而不是404),我需要能够访问用户的原始URL与JavaScript一旦页面加载。

# This works as intended except that it doesn't send a 301 code
ErrorDocument 404 /
# This works as intended except that JavaScript cannot access the user's original URL
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.[a-z]{2,4}$
RewriteRule ^(?!\/$).+ / [R=301,L]
# This redirects everything, including existing script, image, or style files back to / with a 301 code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!\/$).+ / [R=301,L]
zc0qhyus

zc0qhyus1#

这个答案可能是你需要的https://stackoverflow.com/a/12254270/4243067
但是,您可能会对依赖于历史API来更改客户端状态而不调用服务器的客户端路由器库感兴趣。对于vanilla JS,请查看这篇文章https://dev.to/rohanbagchi/how-to-write-a-vanillajs-router-hk3
这是一个著名的React应用程序https://reactrouter.com/en/main/start/concepts

相关问题