Apache redirect to https/ssl using httpd.conf

Discussion in 'Server Operation' started by madsere, Mar 26, 2012.

  1. madsere

    madsere New Member

    I want to ensure that several variations of http://*.mydomain.*/* gets redirected to https://mydomain.com/*.

    No problem, the following part of httpd.conf takes care of that:

    So far so good - but I also want to make sure that any traffic to https://www.mydomain.com/* gets redirected to https://mydomain.com/* (partly to prevent certificate errors, partly to ensure Google only sees one website).

    I can't add another Virtualhost container for this, Apache won't have that.

    I can add a .htaccess file in the website, but that won't catch any links to subdirectories (i.e. https://www.mydomain.com/blah/ will not get caught unless I drop a .htaccess file in each and every directory, not an optimal solution).

    How can I ensure traffic to https://www.mydomain.com/* gets redirected to https://mydomain.com/* ? I'm sure there must be a way to do this?
  2. TiTex

    TiTex Member

    this should work with .htaccess

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^domainname.tld$
    RewriteRule ^(.*)$ https://domainname.tld/$1 [R=301]
    and you can remove this line from the VirtualHost directive
    Last edited: Mar 27, 2012
  3. TiTex

    TiTex Member

  4. madsere

    madsere New Member

    No, not unless you stick the .htaccess file into the /blah/ directory as well. Anyway, that functionality is already accomplished with the httpd.conf entry in my OP.

    The problem is redirecting from https://www.mydomain.com/blah/ to https://mydomain.com/blah/
    Last edited: Mar 27, 2012
  5. TiTex

    TiTex Member

    i looks like you don't wanna understand that what i've posted above does exactly that
    it will redirect everything that's NOT https://mydomain.com/... to https://mydomain.com/...

    and if you put a .htaccess file in you htdocs root folder all subfolders will inherit the settings in it if not otherwise specified in subfolders .htaccess as long as AllowOverride is enabled for the root folder.

    do as you wish , i've just tested it on centos 5.6 with apache 2.4 and mod_rewrite enable... it does exactly what you asked for
    Last edited: Mar 27, 2012

Share This Page