【问题标题】:.htaccess redirect root to index.php.htaccess 将根目录重定向到 index.php
【发布时间】:2012-04-27 21:10:08
【问题描述】:

我需要从http://example.com/ 重定向到http://example.com/index.php

【问题讨论】:

    标签: apache .htaccess mod-rewrite root


    【解决方案1】:

    使用这个:

    DirectoryIndex index.php
    

    【讨论】:

    • 出于 SEO 原因,可以为两个不同的 URL 提供相同的内容。所以DirectoryIndex 对 SEO 场景有帮助。
    【解决方案2】:

    试试这个:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} ^$
    RewriteCond %{HTTP_HOST} ^example.com$
    RewriteRule ^$ http://example.com/index.php [L,R=301]
    

    【讨论】:

    • 为什么你需要mod_rewrite 呢? DirectoryIndex 是这里的正确答案。
    • 两者的工作方式有所不同。默认目录不会更改 uri,因此如果您需要对其进行一些操作,这将是一团糟。
    • 出于 SEO 原因,可以为两个不同的 URL 提供相同的内容。所以DirectoryIndex 对 SEO 场景有帮助。
    • 这个解决方案更灵活,因为目录索引不能按照 OP 请求重定向
    【解决方案3】:

    只是添加我自己的解决方案,因为 the answer of Michael 对我不起作用,我做了类似的事情:

    RewriteEngine on
    
    # These two lines redirect non-www to www.
    RewriteCond %{HTTP_HOST} ^example\.com$
    RewriteRule (.*) https://www.example.com$1 [R=301,L]
    
    # These two lines redirect the root to index.html.
    RewriteRule ^$ /index.html [R=301,L]
    RewriteRule ^/$ /index.html [R=301,L]
    

    这也保留了任何可能的现有查询字符串参数。

    【讨论】:

      【解决方案4】:

      除了上面的@Uwe Keim'sanswer,我在 Apache 虚拟主机上设置了非 www 到 www。

      .htaccess 对我有用的是:

      RewriteEngine on
      
      # Two lines to redirect from the root web directory to myfile.html.
      RewriteRule ^$ /index.html [R=301,L]
      RewriteRule ^/$ /index.html [R=301,L]
      

      【讨论】:

        猜你喜欢
        • 2017-01-12
        • 2013-03-06
        • 2016-11-22
        • 1970-01-01
        • 2013-04-10
        • 1970-01-01
        • 2016-01-21
        • 1970-01-01
        • 2013-12-31
        相关资源
        最近更新 更多