duanluangua8850 2011-07-22 12:19
浏览 14
已采纳

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

I asked the same question Redirect to site if WWW part is missing but the answer did not helped in my case, as i was unable to get to change the CNAME and .htaccess also failed.
The only option left is to use PHP.
Can anyone help me ?

If someone tries to browse example.com or example.com/ or so the PHP will redirect it to 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>
  • 写回答

3条回答 默认 最新

  • dongxi2163 2011-07-22 12:22
    关注

    Try this in .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>
    

    Obviously you should change site.com with your domain.


    Update: incorporated your code with my suggestion.

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害