【问题标题】:Hide actual URL and display alternate URL using htaccess使用 htaccess 隐藏实际 URL 并显示备用 URL
【发布时间】:2013-10-08 13:27:42
【问题描述】:

我想从localhost/test 重定向用户 --> localhost/new/test/index.php 但是用户应该在地址栏中看到localhost/test,尽管内容是从 localhost/new/test 获取的

如何使用 htaccess 来实现。任何帮助表示赞赏。

隐藏实际 URL 的现有解决方案对我不起作用。请帮助解决这个问题。

我们正在尝试的示例

folder level 1 => localhost/test/index.php
folder level 2 => localhost/new/test/index.php

用户在 URL 中输入localhost/test/index.php,但应该显示localhost/new/test/index.php 的内容而不显示localhost/new/test/index.php

用户仍应看到旧 URL localhost/test/index.php

【问题讨论】:

  • 请提供您迄今为止尝试过的示例。我们可以帮助您改进它们。

标签: apache .htaccess url-rewriting


【解决方案1】:

您需要的是不使用R 标志的内部或静默重定向(用于外部重定向)。

通过httpd.conf 启用mod_rewrite.htaccess,然后将此代码放入您的DOCUMENT_ROOT/.htaccess 文件中:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# if URI doesn't start with /new
RewriteCond %{REQUEST_URI} !^/new/ [NC]
# redirect to /new/$1
RewriteRule ^(.+)$ /new/$1 [L]

建议阅读:Apache mod_rewrite Introduction

【讨论】:

  • 我尝试了你的建议,但很遗憾它没有隐藏 URL。我仍然可以在地址栏中看到 localhost/new/test
  • 只有在系统中有其他规则或代码时才有可能。您的 .htaccess 中是否还有其他规则,或者您的系统中是否还有其他 .htaccess?
  • 不,除了您建议的之外,我没有在我的 htaccess 中编写任何规则,我也没有任何其他 htaccess 文件。请让我知道您是否尝试过此代码以及它是否按要求工作。
  • 我在自己测试之前从未发布过答案:P 你确定你的 Apache 配置中启用了 mod_rewrite 和 .htaccess 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 2011-10-09
  • 2015-07-25
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
相关资源
最近更新 更多