首页 > 解决方案 > .htaccess 掩码原始网址

问题描述

我有这个网址:

localhost:1001/project/index.html

如果我写这个 URL:

localhost:1001/project/index.html.twig

我希望它被屏蔽为第一个(localhost:1001/project.index.html),但处理的 URL 必须是第二个(不知道是否可能)。

我已经尝试了所有这些,但我认为我.htaccess的项目文件夹根目录中的所有内容都是错误的,因为没有按我的意愿工作:

一个)

RedirectMatch 302 /project/index.html.twig /project/index.html

b)

RedirectMatch 301 /project/index.html.twig /project/index.html

C)

RewriteCond %{HTTP_HOST} ^/project/index.html.twig$ [NC]
RewriteRule /project/index.html [R=301,L]

注释

我想获得的过程:

  1. 我在浏览器中写:localhost:1001/project/index.html.twig
  2. 不知何故,我希望我.htaccess将浏览器中的 URL 转换为并在浏览器中localhost:1001/project/index.html查看该 URL
  3. 之后我想在内部处理原始 URL:localhost:1001/project/index.html.twig

标签: apache.htaccess

解决方案


如果您想要外部重定向,这可能就是您正在寻找的内容:

RewriteEngine on
RewriteRule ^/?project/index\.html$ /project/index.html.trig [R=301]
RewriteRule ^/?project/index\.html\.twig$ /project/index.html [END]

如果您想要内部重写,请使用该变体:

RewriteEngine on
RewriteRule ^/?project/index\.html\.twig$ /project/index.html [R=301]
RewriteRule ^/?project/index\.html$ /project/index.html.trig [END]

这些规则应该在 http 服务器主机配置或动态配置文件(“.htaccess”样式文件)中同样起作用。如果您可以访问它,您绝对应该更喜欢第一个选项。如果您需要使用动态配置文件,请注意它的解释是否已启用并且它位于主机的DOCUMENT_ROOT文件夹中。

如果您使用该规则收到服务器内部错误(http 状态 500),则可能是您操作的是旧版本的 apache http 服务器。[END]在这种情况下,您会在 http 服务器错误日志文件中找到有关未知标志的明确提示。在这种情况下尝试使用较旧的[L]标志,它可能在这里工作相同,但这取决于您的设置。


推荐阅读