GoDaddy .htaccess是一个子域名

时间:2013-11-17 02:59:46

标签: php apache .htaccess mod-rewrite redirect

我一直在开发一个网站,我的子域名.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

My Root看起来像是:

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

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

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

3 个答案:

答案 0 :(得分:1)

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]

答案 1 :(得分:1)

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

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

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

在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 :(得分:1)

在@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]

工作不正常,因为我的服务器转到了网址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]

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