简体   繁体   English

.htaccess 重写 URL 到模拟子文件夹

[英].htaccess rewrite URL to simulated subfolder

I would like to rewrite the URL from我想重写 URL

https://www.example.com/index.php?page=SOMEPAGE&user=SOMEUSER

to

https://www.example.com/settings/user/SOMEUSER

where settings is the page parameter from the URL.其中settings是来自 URL 的页面参数。 My Solution was:我的解决方案是:

RewriteEngine on

RewriteRule ^([-0-9a-zA-Z]+)/user/([-0-9a-zA-Z]+)$ index.php?page=$1&user=$2 [QSA,L]

The problem is, that all files respond with a 404 error code, because the browser seems to think that /settings/user are actual folders on the server.问题是,所有文件都以 404 错误代码响应,因为浏览器似乎认为/settings/user是服务器上的实际文件夹。 How can I achieve that the actual URL path stays the same?我怎样才能实现实际的 URL 路径保持不变?

The problem would seem to be that you are using relative URLs to your resources.问题似乎是您使用的是资源的相对URL。 If you are changing the path depth of your URLs then you need to ensure you are using either root-relative (starting with a slash) or absolute (with a scheme + hostname) URLs throughout.如果您要更改 URL 的路径深度,则需要确保始终使用根相对(以斜杠开头)或绝对(使用方案 + 主机名)URL。

See my answer on the following question on the Webmasters SE site for more information:有关更多信息,请参阅在 Webmasters SE 网站上对以下问题的回答

because the browser seems to think that /settings/user are actual folders on the server.因为浏览器似乎认为 /settings/user 是服务器上的实际文件夹。

No, the browser sees that as the URL-path.不,浏览器将其视为 URL 路径。 It IS the URL-path as seen in the browser.它是在浏览器中看到的 URL 路径。 Any relative client-side URLs are going to be relative to this URL-path.任何相关的客户端 URL 都将与此 URL 路径相关。 The browser has non concept of what are server-side folders, it only deals with URLs.浏览器没有什么是服务器端文件夹的概念,它只处理 URL。

How can I achieve that the actual URL path stays the same?我怎样才能实现实际的 URL 路径保持不变?

You are changing the URL-path in the browser, so the only way round this is to make your URLs non-relative.您正在更改浏览器中的 URL 路径,因此解决此问题的唯一方法是使您的 URL 不相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM