.htaccess 如果文件或文件夹不存在则打开主页,但保留 URL + URL路由
我读了很多类似的主题,但没有找到正确的答案,所以请帮助我。
假设用户在我的网页上输入一个不存在的子目录:
www.example.com/subpage-1
我想要实现的目标:
我想要我的主页(www.example.com
- 实际上带有隐藏的 index.html< /code>)打开,但保持 URL 不变,使用不存在的子页面 (
www.example.com/subpage-1
)。
我需要它的原因: 我的网站只有主站点 (index.html
),一切都是通过 JavaScript 动态控制的。
我想引入子页面 - 但我只想使用我的主 index.html
站点和 JS 来控制它。 (就像单页应用程序一样。)
因此当用户输入 URL 时 www.example.com/subpage-1
, 我的主站点打开,但由于 URL 保持不变,因此 JS 脚本可以检查 URL,看看是否请求了 subpage-1
,并为其生成正确的内容(如果 subpage当然,支持-1
)。
这也可能是一个 SEO 友好的解决方案,因为我也可以向 Google 提供带有可用子页面的站点地图 - 但是一切都将通过相同的 index.html
和 JS 进行控制。
我怎样才能实现它?
到目前为止我发现了什么:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.html?url=$1 [QSA,L]
我的问题是它每次都会打开主页,我在任何地方都找不到查询(?url=
),所以我无法使用它。
还有一个我不知道如何处理的问题: 假设用户输入 www.example.com/subpage-1
由于我的 JS 脚本处理“subpage-1”,因此它工作正常。
但如果
www.example.com/non-existing-subpage
输入了怎么办?使用上面的解决方案,它会再次打开主页,但 JS 无法为其加载任何内容。我仍然希望所有不存在的子页面都使用 404。 我怎样才能实现它?
I read through a lot of similar topics, but didn't find the right answer, so please help me.
Let's say the user types in a non-existing sub directory to my webpage:
www.example.com/subpage-1
What I want to achieve:
I want my mainpage (www.example.com
- actually with hidden index.html
) to open, but keep the URL unchanged with the non-existing subpage (www.example.com/subpage-1
).
The reason why I need it:
I have the website only with the main site (index.html
), and everything is controlled via JavaScript dynamically.
I want to introduce sub pages - but I want to use only my main index.html
site with JS to control it. (Just like a single page application.)
So when the user enters the URLwww.example.com/subpage-1
,
my main site opens, but since the URL is kept unchanged, the JS script can check the URL, see, that subpage-1
is requested, and generate the right content for it (if subpage-1
is supported, of course).
Also that could be a SEO-friendly solution, since I could provide Google a sitemap with the available subpages as well - however everything would be controlled via the same index.html
and JS.
How can I achieve it?
What I found so far:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.html?url=$1 [QSA,L]
My problem with this is that it opens the main page every time, I can't find the query (?url=
) anywhere, so I can't use it.
Also a problem what I don't know how to handle:
Let's say the user enterswww.example.com/subpage-1
and it's working fine since my JS script handles "subpage-1".
But what if
www.example.com/non-existing-subpage
is entered? With the solution above it would open the main page again, but JS can't load any content for it. I still want 404 for all of the non existing subpages.
How can I achieve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是服务器上的内部重写。因此,
url
URL 参数(仅在内部重写时出现)仅适用于服务器端脚本,而不适用于客户端 JavaScript。浏览器/客户端只能看到来自服务器的响应 - 它不知道哪个文件产生了该响应。但是,客户端 JavaScript 可以查看浏览器地址栏中的内容,可以通过window.location
对象访问该地址栏。因此,您可以简化
RewriteRule
指令:在 JS 中,您可以从
window.location.pathname
属性读取请求的 URL 路径。例如,如果您请求example.com/foo
,则pathname
属性包含/foo
(带有斜杠前缀)供您执行操作在你的脚本中相应地。如果您只使用客户端 JavaScript,则不能。 “404 Not Found”状态是从服务器发送的 HTTP 响应。
在客户端 JS 中你能做的最好的事情就是向用户显示类似“404 Not Found”的消息,并确保你有一个阻止索引的
robots
元标记。但仍然以 200 OK HTTP 状态提供服务。搜索引擎(即 Google)可能会将其视为软 404(即看起来像 404 的页面,但提供 200 OK 状态)。如果您想要提供 404 HTTP 响应状态,那么服务器需要知道哪些是有效/无效的 URL。
This is an internal rewrite on the server. Consequently, the
url
URL parameter (present only on the internal rewrite) is only available to a server-side script, not client-side JavaScript. The browser/client only sees the response from the server - it is not aware of what file produced that response. However, client-side JavaScript can see what is present in the browser's address bar, which is accessible via thewindow.location
object.So, you can instead simplify the
RewriteRule
directive:And in your JS you can read the requested URL-path from the
window.location.pathname
property. For example, if you requestexample.com/foo
then thepathname
property contains/foo
(with a slash prefix) for you to act on accordingly in your script.You can't if you are only using client-side JavaScript. A "404 Not Found" status is an HTTP response sent from the server.
The best you can do in client-side JS is to display what looks-like a "404 Not Found" message to the user and make sure you have a
robots
meta tag that prevents indexing. But this is still served with a 200 OK HTTP status. Search engines (ie. Google) will likely see this as a soft-404 (ie. a page that looks like a 404, but is served with a 200 OK status).If you want to serve a 404 HTTP response status then the server would need to be aware of which are valid/invalid URLs.