【问题标题】:.htaccess redirect does not working well.htaccess 重定向无法正常工作
【发布时间】:2015-10-27 12:34:32
【问题描述】:

我想用 .htaccess 将主域“example.com”重定向到子目录“sub”。我有包含以下内容的 .htaccess 文件:

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteCond %{REQUEST_URI} !^/sub/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /sub/$1 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ sub/index.php [L]

如果我在 Web 浏览器的地址栏中输入 example.com 或 www.example.com,它可以正常工作,但如果我输入 example.com/index.php 或 www.example.com/index.php 则不起作用它进入主文件夹,而不是子文件夹 sub。

我做错了什么?

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    这很好用:

    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^(www.)?example\.org$ [NC]
    RewriteCond %{REQUEST_URI} !^/sub/
    RewriteRule ^(.*)$ /sub/$1 [L]
    

    另外,您可以使用this for testing your htaccess

    【讨论】:

      【解决方案2】:

      为什么不在 index.php 文件中设置重定向?您的 .htaccess 文件没有条件。因为你是直接点击页面。

      根据我在下面的评论,将其放入您的 index.php 页面:

      header("Location: http://example.com/sub/index.php");
      die();
      

      【讨论】:

      • 我不明白 :(
      • 您似乎正试图拦截到您的根目录的连接并将它们发送到“/sub/index.php”。但是当有人进入“index.php”时它不起作用。那是因为 .htaccess 在这种情况下不会做任何事情。您需要将重定向放在该文件中。
      • 我不想重定向在地址栏可见,如果我在带有 header() 函数的 php 文件中重定向它是可见的。但如果 durbnpoisn 悲伤是真的,那是不可能的:(
      猜你喜欢
      • 1970-01-01
      • 2013-01-08
      • 2013-11-04
      • 2023-04-02
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      相关资源
      最近更新 更多