The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Rewrite Rules for all sites

Discussion in 'General Discussion' started by jimhermann, Jun 5, 2011.

  1. jimhermann

    jimhermann Active Member

    Joined:
    Jan 20, 2008
    Messages:
    42
    Likes Received:
    1
    Trophy Points:
    8
    cPanel Community,

    I want to have http://www.domain-name.tld/mailman/listinfo redirected to http://domain-name.tld/mailman/listinfo (without the www.)

    I added some Rewrite Rules to the Post VirtualHost Include (/usr/local/apache/conf/includes/post_virtualhost_global.conf) using the cPanel Include Editor for all Apache version.

    The Rewrite Rules did not work, so I tried a simple Rewrite Rule that works in /home/domain-username/public_html/.htaccess:

    RewriteEngine On

    RewriteLog "/var/log/rewrite.log"
    RewriteLogLevel 2

    RewriteCond %{HTTP_HOST} ^www\.domain-name\.tld$ [NC]
    RewriteRule ^(.*)$ http://domain-name.tld/$1 [R,L]


    The RewriteLog and RewriteLogLevel commands are effective, so I know that Apache is reading the file.

    However, the RewriteRule is not effective.

    What am I doing wrong? Is the HTTP_HOST not available at this point?

    The Rewrite Logs do not show that Apache evaluated the Rewrite Rule for domain-name.tld

    I tried putting the same commands in /usr/local/apache/conf/userdata/mailman.conf and got the same results.

    # /scripts/verify_vhost_includes
    Testing /usr/local/apache/conf/userdata/mailman.conf...ok

    Jim
     
  2. cPanelTristan

    cPanelTristan Quality Assurance Analyst
    Staff Member

    Joined:
    Oct 2, 2010
    Messages:
    7,623
    Likes Received:
    21
    Trophy Points:
    38
    Location:
    somewhere over the rainbow
    cPanel Access Level:
    Root Administrator
  3. jimhermann

    jimhermann Active Member

    Joined:
    Jan 20, 2008
    Messages:
    42
    Likes Received:
    1
    Trophy Points:
    8
    Rewriting the domain name is harder than switching from HTTP to HTTPS. Here is what I had to add to httpd.conf or .htaccess:

    RewriteEngine On

    RewriteMap strip_www prg:/usr/local/cpanel/3rdparty/mailman/bin/strip_www.pl

    RewriteCond %{REQUEST_URI} ^/mailman
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} !^$
    RewriteRule ^/(.*)$ http://${strip_www:%{HTTP_HOST}}/$1 [R,L]

    where /usr/local/cpanel/3rdparty/mailman/bin/strip_www.pl contains:

    #!/usr/bin/perl
    $| = 1;
    while (<STDIN>) {
    $_ =~ s/www\.//;
    print $_;
    }

    It works, but loads 423 perl strip_www.pl processes when I start httpd. It's the nature of Apache beast.

    I think that I would better off creating individual httpd.conf files for each domain name in:

    /usr/local/apache/conf/userdata/std/2/$username/$domainname

    RewriteEngine On

    RewriteCond %{REQUEST_URI} ^/mailman
    RewriteCond %{HTTP_HOST} ^www.$domainname$ [NC]
    RewriteRule ^/(.*)$ http://$domainname/$1 [R,L]

    where $username and $domainname have to be different for each site.

    I haven't tried this yet.

    Jim
     
  4. cPanelTristan

    cPanelTristan Quality Assurance Analyst
    Staff Member

    Joined:
    Oct 2, 2010
    Messages:
    7,623
    Likes Received:
    21
    Trophy Points:
    38
    Location:
    somewhere over the rainbow
    cPanel Access Level:
    Root Administrator
    I'm uncertain why an .htaccess at /usr/local/cpanel/3rdparty/mailman/cgi-bin wasn't used as my link suggested (of note, /usr/local/cpanel/3rdparty/mailman/bin is not the same as cgi-bin). Now, you cannot use httpd.conf files for each domain because, as I already indicated, the rewrite needs to be in mailman's cgi-bin. This is why it is recommended in all the posts where discussion of mailman rewrites are discussed to put the rewrites into the .htaccess file for mailman itself.

    This would work for every domain if you put it in /usr/local/cpanel/3rdparty/mailman/cgi-bin location:

    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/mailman/$1 [R=301,L]
    I tested it, and it redirects mailman's list no matter what domain from www.domain.com/mailman/whatever to domain.com/mailman/whatever and it only does it for mailman b/c it's in mailman's cgi-bin

    Thanks!
     
  5. jimhermann

    jimhermann Active Member

    Joined:
    Jan 20, 2008
    Messages:
    42
    Likes Received:
    1
    Trophy Points:
    8
    Very nice. I liked it and replaced my RewriteMap with your code.

    BTW, both codes work in /usr/local/apache/conf/userdata/mailman.conf also. You're right. They won't work in /home/username/public_html/.htaccess because it is never loaded for URI /mailman. However, /usr/local/apache/conf/userdata/*.conf is included in the httpd.conf, so it is always loaded with every virtual domain. I found this solution before you posted your message.

    Thanks again.

    Jim
     
  6. jimhermann

    jimhermann Active Member

    Joined:
    Jan 20, 2008
    Messages:
    42
    Likes Received:
    1
    Trophy Points:
    8
Loading...

Share This Page