【问题标题】:GoDaddy .htaccess for a subdomain子域的 GoDaddy .htaccess
【发布时间】:2013-11-17 02:59:46
【问题描述】:

我一直在开发一个网站,但我的子域的 .htaccess 似乎无法正常工作。

我的子域中的 .htaccess 文件。

DirectoryIndex index.php
Options -MultiViews

RewriteEngine on

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+home\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ home.php?page=$1 [L,QSA]

当我转到我的子域时,我可以找到我的 index.php,但是当我转到我的 home.php 或 /home 等时,它似乎不起作用,并重定向到我的 404 页面(已在我的根目录。

我的根目录 .htaccess 文件:

Options -MultiViews

RewriteEngine on

ErrorDocument 404 http://example.ca/error?id=404
ErrorDocument 403 http://example.ca/error?id=403
ErrorDocument 500 http://example.ca/error?id=500

RewriteCond %{http_host} ^www\.james-emerson\.ca [NC]
RewriteRule ^(.*)$ http://james-emerson.ca/$1 [R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteRule ^home$ index.php

我的根看起来像:

/
 - content/
 - include/
 - sites/
   - external/
     - sbfg/ (subdomains subdirectory)
       - content/
       - include/
       - .htaccess
       - index.php
       - home.php
     - ...etc...

 - .htaccess
 - index.php
 - ...etc...

如果您能提供帮助,我将不胜感激。提前致谢。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    为什么不这样,处理php中必须排除的元素:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ home.php [QSA,L]
    

    这不是最简单的,但是这段代码应该可以工作并且可以完全控制何时重定向。您可以轻松创建一个存储漂亮 url 的表以获取控制器/操作/参数,更轻松地维护旧 url 等。

    在 php 中,这应该适用于每个目录,指向请求开始的文件 (home.php)

    setup_paths();
    $current_url = get_current_url(false);
    $explode_uri = explode('/', $current_url);
    $page_like_you_have_now = array_shift($explode_uri);
    
    function setup_paths()
    {
        define('BASE_DIR', dirname($_SERVER['PHP_SELF']));
        $base_url = BASE_DIR;
        if(substr($base_url, -1, 1) !== DS)
        {
            $base_url .= DS;
        }
        define('BASE_URL', str_replace('\\', '/', $base_url));
    }
    
    function get_current_url($with_base = true)
    {
        $current_uri_data = parse_url($_SERVER['REQUEST_URI']);
        $start = strlen(BASE_DIR);
        $uri = substr($current_uri_data['path'], $start);
    
       //Remove possible slashes on begin, or do other corrections
        while(substr($uri, 0, 1) == '/')
        {
            $uri = substr($uri, 1); //@should 301 to correct pages afterwards..
        }
    
        return ($with_base ? BASE_URL : '') . $uri;
    }
    

    【讨论】:

    • 我明白了为什么会出现问题。谢谢你的帮助。我投票赞成你的答案,尽管它并没有真正帮助我。
    【解决方案2】:

    在您的 sbfg/.htaccess 中试试这个:

    DirectoryIndex index.php
    Options -MultiViews
    
    RewriteEngine on
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /sbfg//home\.php\?page=([^\s&]*)[&\s] [NC]
    RewriteRule ^ /sbfg/%1? [R=302,L]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ /sbfg/home.php?page=$1 [L,QSA]
    

    【讨论】:

    • 不起作用,我可以加载 home.php 页面,但无法缩短链接。如果我使用我原来的 .htaccess 文件,我可以加载 home.php,但我不能将 ?page=contact 或简写形式添加到 /contact 中。
    • 确保将这些规则放在其他规则之前。
    • 还是不行。现在当我转到 home.php 时,它直接进入我的 404 错误文档。
    • 我仍然只是被重定向到错误页面...我想将这些添加到 sites/external/sbfg 文件夹中的 .htaccess 文件中还是我的根目录中?
    • OK 上次编辑。现在将这 2 条规则放在现有规则之上,看看这是否有效。并移开您的 sbfg/.htaccess 文件
    【解决方案3】:

    在@anubhava 的帮助下,我自己想出了如何解决问题。

    @anubhava 给我的 .htaccess 文件:

    DirectoryIndex index.php
    Options -MultiViews
    
    RewriteEngine on
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /sbfg//home\.php\?page=([^\s&]*)[&\s] [NC]
    RewriteRule ^ /sbfg/%1? [R=302,L]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ /sbfg/home.php?page=$1 [L,QSA]
    

    无法正常工作,因为我的服务器将访问 URL sites/external/sbfg/sbfg/home.php 而不是 sites/home/sbfg/home.php。直到我从根 .htaccess 文件中删除了 404 文档代码,我才注意到错误。

    我将我的代码更新为:

    DirectoryIndex index.php
    Options -MultiViews
    
    RewriteEngine on
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /home\.php\?page=([^\s&]*)[&\s] [NC]
    RewriteRule ^ /%1? [R=302,L]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ /home.php?page=$1 [L,QSA]
    

    然后将部分代码添加到我的根目录.htaccess 文件中:

    Options -MultiViews
    
    ErrorDocument 404 http://james-emerson.ca/error?id=404
    ErrorDocument 403 http://james-emerson.ca/error?id=403
    ErrorDocument 500 http://james-emerson.ca/error?id=500
    
    RewriteEngine on
    
    RewriteCond %{http_host} ^www\.james-emerson\.ca [NC]
    RewriteRule ^(.*)$ http://james-emerson.ca/$1 [R=301,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.php -f 
    RewriteRule ^(.*)$ $1.php
    RewriteRule ^home$ index.php
    
    # .htaccess file for the sbfg/home.php?page= code.
    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} /home\.php\?page=([^\s&]*)[&\s] [NC]
    RewriteRule ^ %1? [R=302,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/?$ home.php?page=$1 [L,QSA]
    

    现在它可以正常工作了。感谢大家帮助我,不知道以前为什么会出现这些问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-08
      • 2021-12-01
      • 2013-06-28
      • 1970-01-01
      • 2017-07-05
      • 2017-07-13
      • 2017-06-25
      • 1970-01-01
      相关资源
      最近更新 更多