【问题标题】:.htaccess-The page isn't redirecting properly.htaccess-页面未正确重定向
【发布时间】:2018-06-08 00:29:13
【问题描述】:

我正在尝试将 index.html 重定向到 home.html 并通过 .htaccess 从 url 中删除该 home.html。但它显示页面没有正确重定向。 这意味着我需要将 mydomain.com/index.html 重定向到 mydomain.com/home.html 并且仅在访问索引页面时才需要显示 mydomain.com/。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Redirect 301 "/index.html" "/home.html"

RewriteCond %{THE_REQUEST} ^GET.*home\.html [NC]
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]

</IfModule>

【问题讨论】:

    标签: php html .htaccess


    【解决方案1】:

    //这是工作代码,我希望它有用..

    <IfModule mod_rewrite.c>
    RewriteEngine On
    Redirect 301 / http://newsite.com/
    </IfModule>
    

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www\.)?http://dbdenterprise\.in$
    RewriteRule ^(.*)$ http://dbdenterprise.com/$1 [R=301,QSA,L]
    

    【讨论】:

      【解决方案2】:

      您可以尝试将 DirectoryIndex 设置为所需页面(在您的情况下为 home.html),而不是使用 mod_rewrite:

      DirectoryIndex home.html
      

      所以,如果你打电话给domain.com/,它应该会给你domain.com/home.html的内容

      供参考,Apache 配置手册: https://httpd.apache.org/docs/2.4/mod/mod_dir.html “目录索引指令”


      引用下面的对话:

      也许这可行(抱歉,我现在无法测试)

      RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} =/index.html
      RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]
      

      一点解释:如果请求的主机匹配 example.com 并且调用的 URI 实际上不是真实的文件或文件夹加上 URI 等于 “index.html”它应该重定向到 home.html。

      编辑:由于您不想显示 home.html,我们可以通过在内部“代理”请求而不是强制外部重定向来逃避。尝试将最后一行(RewriteRule)中的“R=301”替换为“P”,使其显示为RewriteRule (.*?)home\.html/*(.*) /$1$2 [P,NE,L]

      【讨论】:

      • 我也尝试过 DirectoryIndex home.html 但它会更改目录中的所有 index.html。我的意思是子域。所以我只需要特定的域。
      • 如果我理解正确,您只希望这发生在特定域上而不是在该域的子域上?
      • 更新了我的答案
      • c1nema 它工作但显示 mydomain.com/home.html。但我只需要 URL 中的 mydomain.com
      猜你喜欢
      • 2013-06-28
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2017-01-18
      相关资源
      最近更新 更多