【问题标题】:.htaccess retrieve from folder if subdomain, not 301 redirecting?.htaccess 如果子域从文件夹中检索,而不是 301 重定向?
【发布时间】:2012-12-01 18:54:22
【问题描述】:

如果请求是子域,我希望我的网站只指向 /user 文件夹。

如果请求是 subdomain.site.com/admin,则站点应显示 subdomain.site.com/user/admin 的页面。

我的代码的问题在于它会进行 301 重定向,而不仅仅是保留 url 地址。

我的代码如下所示:

<IfModule mod_rewrite.c>

DirectoryIndex index.php

RewriteEngine on
RewriteBase / 

RewriteCond %{HTTP_HOST} !^www.bildsida.se(.*)
RewriteCond %{HTTP_HOST} ^[^.]+.bildsida.se(.*)

RewriteRule ^$ user/$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*) user/$1 [L]

</IfModule>

你也可以自己试试,去http://mickes.bildsida.se/admin看看地址怎么变成/user/admin了。很烦人……

【问题讨论】:

    标签: .htaccess mod-rewrite subdomain


    【解决方案1】:

    你只需要你展示的几行代码就可以了。

    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} !^www.bildsida.se
    RewriteCond %{HTTP_HOST} ^.+\.bildsida\.se
    
    RewriteRule ^(.*)$ user/$1 [L]
    

    我调整了 HTTP_HOST 检查,因为该参数只查看域名,因此您不需要末尾的 (.*)。我还删除了查看文件是否存在或是否为目录的检查,因为您希望所有内容都被重定向(例如,没有理由可以访问其他子域的文件)

    【讨论】:

    • 我现在收到“内部服务器错误”。可能是什么问题?
    【解决方案2】:

    您的代码可以简化为以下几行:(改为尝试)

    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^(www\.)?bildsida\.se [NC]
    RewriteRule ^(.*)$ /user/$1 [QSA,L]
    

    【讨论】:

    • 仍然得到“内部服务器错误”:/
    • 如果你直接访问文件(在用户中)你会得到内部服务器错误还是它工作正常?
    • 不,我仍然收到“内部服务器错误”
    • 那你应该检查你的代码,内部服务器错误可能不是来自htaccess
    • 但是,除了重定向之外,我在问题中的代码工作正常。值得注意的是,它实际上可以输入确切的路径,例如“mickes.bildsida.se/admin/index.php”。该地址获取 /user/admin/index.php,即使没有 /index.php,它也应该这样做。
    【解决方案3】:

    终于!经过日日夜夜阅读论坛和文档,并测试所有可能的方法,我得到了它的工作!

    Solotion:

    <IfModule mod_rewrite.c>
    
        DirectoryIndex index.php
    
        RewriteEngine on
        RewriteBase /
    
        RewriteCond %{HTTP_HOST} !^www.bildsida.se
        RewriteCond %{HTTP_HOST} !^bildsida.se
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^$ user/index.php [L]
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)? user/$1/ [QSA]
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule /(.+)/ user/$1 [QSA]
    
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2011-12-15
      • 1970-01-01
      • 2011-03-11
      • 2014-12-03
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多