【问题标题】:add directory name in url with .htaccess使用 .htaccess 在 url 中添加目录名称
【发布时间】:2017-09-26 12:20:28
【问题描述】:

我有一个名为 http://example.com 的网站,它指向我的 public_html 目录。 我希望我的网站应该使用 http://example.com/cms URL 。 http://example.com/cms 应该指向 public_html 目录。是否可以不在网站根目录创建子目录?

如何使用 .htaccess 做到这一点?

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 为什么不直接创建和使用这个 cms 目录呢?
  • 其实是不允许的。我们需要用 .htaccess rewrite 来做到这一点

标签: .htaccess mod-rewrite url-rewriting


【解决方案1】:

因为您没有提供有关您要使用的 CMS 的任何详细信息,所以我只能提供一个通用的 .htaccess 文件。如果您的 CMS 带有一些特殊的重写规则,您可能需要对其进行调整。

RewriteEngine on
RewriteBase /

# redirect to /cms if not rewritten internal
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !^/?cms(/.*)?$
RewriteRule ^/?(.*)$ /cms/$1 [R=301,L]

# rewrite to root directory if URL starts with /cms
RewriteCond %{REQUEST_URI} ^/?cms(/.*)?$
RewriteRule ^.*$ %1 [L]

只需将此 .htaccess 文件放在安装 CMS 的 public_html 文件夹中即可。如果您调用您的网站http://example.com,您将被重定向 (301) 到 http://example.com/cms,并且任何以 /cms 开头的 URL 都将在内部重定向到您的根文件夹。您不必创建 /cms 子文件夹。

我确实在带有 Apache 2.4.27 的 Ubuntu 16.04.03 LTS 服务器上测试了这个结果。

【讨论】:

  • 我用过这段代码,如果我打开 url example.com/cms,它总是显示 404
  • 您是否启用了 mod_rewrite 并有权使用 .htaccess?
  • 是的,我启用了 mode_rewrite 也使用了正确的 .htaccess
猜你喜欢
  • 2011-10-31
  • 1970-01-01
  • 2020-05-11
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 2012-10-03
相关资源
最近更新 更多