WWW重定向到非WWW与通配符子域服务器 [英] Redirecting www to non www on server with wildcard subdomains

查看:156
本文介绍了WWW重定向到非WWW与通配符子域服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails +的Apache +乘客设置和我的应用程序供应通配符子域。我需要的所有WWW网址重定向到其非WWW等值。

I have a Rails+Apache+Passenger setup and my app serves wildcard subdomains. I need all www URLs to redirect to their non www equivalents.


  • www.example.net应该重定向到example.net站

  • www.subdomain.example.net应该重定向到subdomain.example.net

我目前的虚拟主机的配置是如下

My current vhost config is as below

<VirtualHost *:80>

  ServerName  example.net
  ServerAlias *.example.net

  DocumentRoot /home/public_html/example.net/current/public

  RailsEnv staging

</VirtualHost>

我试过的重写规则在不同的地点各种各样,但没有生效。我检查,以确保在Apache重写模块使能和RewriteEngine叙述上。不知道我错过了什么。所有帮助不大AP preciated!

I tried an assortment of rewrite rules in various locations but none took effect. I've checked to make sure that the apache rewrite module is enabled and RewriteEngine is on. Not sure what I'm missing. All help much appreciated!

推荐答案

我在我的应用程序解决了这个问题,因为我基于反正域有逻辑。把这个code在你的ApplicationController

I solved this issue in my app, as I have logic based on the domain anyway. Place this code in your ApplicationController

class ApplicationController < ActionController::Base
    before_filter :check_host

    def check_host
        if request.host.split('.')[0] == 'www'
            redirect_to "http://" + request.host.gsub('www.','')
        end
    end
end

可能有特殊情况,如果你的一些主机名包含WWW。为您不得不code表示。

Could have special cases if some of your hostnames contain "www." for any other reason that you'd have to code for.

这篇关于WWW重定向到非WWW与通配符子域服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆