2011-07-22 38 views
0

我問了同樣的問題Redirect to site if WWW part is missing但我的情況沒有幫助,因爲我無法改變CNAME和.htaccess也失敗了。
剩下的唯一選擇是使用PHP。
任何人都可以幫助我嗎?重定向到網站如果www部分丟失使用PHP

如果有人試圖瀏覽example.com或example.com/左右PHP將重定向它www.example.com/ ..

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> 
+0

'www'在鏈接前面很糟糕...... :(不要! – daGrevis

+0

@daGrevis:是的確如此:/我看不到使用它的理由:/ – genesis

+2

我會建議你試着找出原因/關於.htaccess什麼'失敗',因爲這幾乎是最好的,最簡單的方法來正確地重定向到您的域名,同時包括www。 – Mac

回答

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> 
+0

我想知道爲什麼downvote?答案在這裏解決了一個問題,不管我們喜歡與否,都是一個完全不同的問題。 – Shef

+0

對不起shef。在第一分鐘O P在請求Php,「我不想依賴.htaccess」,但我沒有發現他的編輯。然而,這是標籤php的問題,而不是.htaccess – genesis

+0

確定它被標記爲PHP,但OP還聲明htaccess失敗。所以,我提出了一個htaccess解決方案,它可以工作,因爲PHP將是一個巨大的開銷。 – Shef

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

一個很小的事情,但我'd將==改爲===以及!= to!==。更嚴格的檢查會更快更正確。 – Sid

+0

@Sid:done ....... – genesis

+0

我可以聞到noobs的遞歸問題。xD – daGrevis

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; 
} 

我希望這有助於。

相關問題