【问题标题】:remove folder from url using .htaccess使用 .htaccess 从 url 中删除文件夹
【发布时间】:2015-09-18 21:36:11
【问题描述】:

我知道还有很多其他类似的问题,我尝试了其中一些,但没有运气。

我有一个名为 test.com 的网站,当有人搜索 test.com 以显示 test.com/home 中的内容时,我需要在 uerl 中没有主页。

目前我的 htaccess 文件如下所示:

RewriteEngine on
RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule    /(.*) home/$1    [L]
    RewriteRule    /home/(.*) /$1    [L,R=302]

RewriteOptions inherit

RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$

但是当我输入 test.com 时,它会显示文件夹列表,而不是 home 内的内容。

我做错了什么?

谢谢

【问题讨论】:

  • 这个 htaccess 在哪里?
  • 我正在使用 cpanel,它位于 public_html 文件夹中

标签: regex apache .htaccess mod-rewrite redirect


【解决方案1】:

您可以在 root .htaccess 中将您的规则简化为此

RewriteEngine on
RewriteBase /

RewriteRule ^$ home/ [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]

确保在测试之前清除浏览器缓存。

【讨论】:

    猜你喜欢
    • 2015-06-23
    • 2015-07-27
    • 2022-09-27
    • 2018-04-22
    • 1970-01-01
    • 2015-12-17
    • 2016-04-13
    • 2014-04-13
    相关资源
    最近更新 更多