如果使用PHP缺少www部分,则重定向到站点

时间:2011-07-22 12:19:02

标签: php redirect

我问了同样的问题Redirect to site if WWW part is missing,但答案在我的情况下没有帮助,因为我无法更改CNAME,而.htaccess也失败了。
剩下的唯一选择是使用PHP 谁能帮我 ?

如果有人试图浏览example.com或example.com/左右,PHP会将其重定向到www.example.com /.../ p>

edit

SetEnv TZ Asia/Calcutta 

ErrorDocument 400 /error.html
ErrorDocument 401 /error.html
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html

<ifModule mod_php4.c>
 php_value zlib.output_compression 16386
</ifModule>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteRule ^home$ home.php [L]

RewriteRule ^credit$ credits.php [L]
RewriteRule ^credits$ credits.php [L]

RewriteRule ^profile/(\d+)/?$ profile.php?id=$1 [L]
RewriteRule ^user/(\d+)/?$ view_profile.php?id=$1 [L]

RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^blog/(\d+)/?$ blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/?$ blog.php?id=$1 [L]
RewriteRule ^blog/(\d+)/([A-Za-z0-9-_\[\]]+)/?$ blog.php?id=$1&title=$2 [L]
RewriteRule ^blog.php/(\d+)/([A-Za-z0-9-_\[\]]+)/?$ blog.php?id=$1&title=$2 [L]

RewriteRule ^blogbyauthor.php/(\d+)/?$ blogbyauthor.php?uid=$1 [L]
RewriteRule ^blogbyauthor/(\d+)/?$ blogbyauthor.php?uid=$1 [L]
RewriteRule ^blogbyauthor/(\d+)/?$ blogbyauthor.php?uid=$1 [L]

RewriteRule ^blog_by_category.php/([A-Za-z0-9-_\[\]]+)/?$ blog_by_category.php?category=$1 [L]
RewriteRule ^blog_by_category/([A-Za-z0-9-_\[\]]+)/?$ blog_by_category.php?category=$1 [L]
</IfModule>

3 个答案:

答案 0 :(得分:3)

在.htaccess:

中试试
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine on

    # site.com -> www.site.com
    RewriteCond %{HTTP_HOST} ^site.com
    RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

</IfModule>

显然,您应该使用您的域名更改site.com


更新:根据我的建议合并您的代码。

SetEnv TZ Asia/Calcutta 

ErrorDocument 400 /error.html
ErrorDocument 401 /error.html
ErrorDocument 403 /error.html
ErrorDocument 404 /error.html

<ifModule mod_php4.c>
 php_value zlib.output_compression 16386
</ifModule>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

# site.com -> www.site.com
RewriteCond %{HTTP_HOST} ^site.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

RewriteRule ^home$ home.php [L]

RewriteRule ^credit$ credits.php [L]
RewriteRule ^credits$ credits.php [L]

RewriteRule ^profile/(\d+)/?$ profile.php?id=$1 [L]
RewriteRule ^user/(\d+)/?$ view_profile.php?id=$1 [L]

RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^blog/(\d+)/?$ blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/?$ blog.php?id=$1 [L]
RewriteRule ^blog/(\d+)/([A-Za-z0-9-_\[\]]+)/?$ blog.php?id=$1&title=$2 [L]
RewriteRule ^blog.php/(\d+)/([A-Za-z0-9-_\[\]]+)/?$ blog.php?id=$1&title=$2 [L]

RewriteRule ^blogbyauthor.php/(\d+)/?$ blogbyauthor.php?uid=$1 [L]
RewriteRule ^blogbyauthor/(\d+)/?$ blogbyauthor.php?uid=$1 [L]
RewriteRule ^blogbyauthor/(\d+)/?$ blogbyauthor.php?uid=$1 [L]

RewriteRule ^blog_by_category.php/([A-Za-z0-9-_\[\]]+)/?$ blog_by_category.php?category=$1 [L]
RewriteRule ^blog_by_category/([A-Za-z0-9-_\[\]]+)/?$ blog_by_category.php?category=$1 [L]
</IfModule>

答案 1 :(得分:2)

$protocol = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";

if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
    header('Location: '.$protocol.'www.'.$_SERVER['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
    exit;
}

答案 2 :(得分:0)

另一个(非常类似于起源)php解决方案是..

$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos($_SERVER["SERVER_PROTOCOL"], "/")).$s.'://';
$domain = $_SERVER['SERVER_NAME'];

$mypath  = $protocol.$domain;

if($mypath == 'http://yourdomain.com') {
    $uri = '';
    if(isset($_SERVER['REQUEST_URI'])) {
        $uri = $_SERVER['REQUEST_URI'];        
    }
    header ('HTTP/1.1 301 Moved Permanently');
    header ('Location: http://www.yourdomain.com'.$uri);
    exit;
}

我希望这会有所帮助。