Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

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.

SOLVED Help with rewrite rule for subdirectories

Discussion in 'Security' started by Spork Schivago, Nov 30, 2016.

Tags:
  1. Spork Schivago

    Spork Schivago Well-Known Member

    Joined:
    Jan 21, 2016
    Messages:
    514
    Likes Received:
    54
    Trophy Points:
    28
    Location:
    corning, ny
    cPanel Access Level:
    Root Administrator
    Hello,

    I posted this under Security because people are able to access some directories I don't want them accessing, however, it might fit under a better topic, I just couldn't find one that seemed to match.

    Anyway, I have manually set up sub-domains, like cpcalendars.mydomain.com, and whenever someone visits cpcalendars.mydomain.com, they get redirected to https://www.mydomain.com:2080. If they visit cpcalendars.mydomain.com/somedata, they'll get redirected to https://www.mydomain.com:2080/somedata. This is good and what I want.

    When someone visits mydomain.com/cpcalendars though, they see the contents of the cpcalendars directory. I don't want that. I want it so when someone visits mydomain.com/cpcalendars, they get redirected to https://www.mydomain.com:2080. I'm having trouble with the mod_rewrite rule though.

    Here's the content of my /home/<username>/public_html/cpcalendars/.htaccess file:
    Code:
    <ifModule mod_headers.c>
      # Try to turn off caching for Google Chrome.
      Header set Expires "Thu, 19 Nov 1981 08:52:00 GM"
      Header set Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
      Header set Pragma "no-cache"
    </ifModule>
    
    RewriteEngine on
    
    # Allow .well-known through for Let's Encrypt.
    RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
    
    # Redirect http requests to https because we have free SSL certs.
    RewriteCond %{HTTP_HOST} ^cpcalendars.mydomain.com$
    RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
    RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
    RewriteRule ^(.*)$ "https\:\/\/www\.MyDomain\.com\:2080\/$1" [R=301,L]
    
    # php -- BEGIN cPanel-generated handler, do not edit
    # NOTE this account's php is controlled via FPM and the vhost, this is a place holder.
    # Do not edit. This next line is to support the cPanel php wrapper (php_cli).
    # AddType application/x-httpd-ea-php70 .php .phtml
    # php -- END cPanel-generated handler, do not edit
    
    
    Here's the contents of my /home/<username>/public_html/.htaccess file:
    Code:
    # Tell the browser to check for index.html and index.php, in that order.
    # if either exist, load that file by default.
    DirectoryIndex index.php index.html
    
    <IfModule mod_headers.c>
    # Turn off caching for Google Chrome.
      Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate, post-check=0, pre-check=0"
      Header set Pragma "no-cache"
      Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
    
    # Add P3P Privacy Headers to the site (this causes infinite redirects for some reason).
    #  Header set P3P "policyref="/w3c/p3p.xml""
    </IfModule>
    
    <IfModule mod_rewrite.c>
    #Turn RewriteMod on.
      RewriteEngine On
    
    # Redirect https://cpanel.mydomain.com to https://www.MyDomain.com:2083
      RewriteCond %{HTTP_HOST} ^cpanel.mydomain.com$
      RewriteRule ^(.*)$ "https\:\/\/www\.MyDomain\.com\:2083\/$1" [R=307,L]
    
    # Redirect https://cpcalendars.mydomain.com to https://www.MyDomain.com:2080
      RewriteCond %{HTTP_HOST} ^cpcalendars.mydomain.com$
    # I thought this RewriteCond below would do the trick, but it doesn't.
      RewriteCond %{HTTP_HOST} ^mydomain.com/cpcalendars$
      RewriteRule ^(.*)$ "https\:\/\/www\.MyDomain\.com\:2080\/$1" [R=307,L]
    
    # Redirect https://cpcontacts.mydomain.com to https://www.MyDomain.com:2080
      RewriteCond %{HTTP_HOST} ^cpcontacts.mydomain.com$
      RewriteRule ^(.*)$ "https\:\/\/www\.MyDomain\.com\:2080\/$1" [R=307,L]
    
    # Redirect https://webdisk.mydomain.com to https://www.MyDomain.com:2078
      RewriteCond %{HTTP_HOST} ^webdisk.mydomain.com$
      RewriteRule ^(.*)$ "https\:\/\/www\.MyDomain\.com\:2078\/$1" [R=307,L]
    
    # Redirect https://webmail.mydomain.com to https://www.MyDomain.com:2096
      RewriteCond %{HTTP_HOST} ^webmail.mydomain.com$
      RewriteRule ^(.*)$ "https\:\/\/www\.MyDomain\.com\:2096\/$1" [R=307,L]
    
    # Redirect https://whm.mydomain.com to https://www.MyDomain.com:2087
      RewriteCond %{HTTP_HOST} ^whm.mydomain.com$
      RewriteRule ^(.*)$ "https\:\/\/www\.MyDomain\.com\:2087\/$1" [R=307,L]
    
    # Redirect all other users to the https version of our website,
    # because we have SSL certs now.
      RewriteCond %{HTTPS} !=on
      RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=307,L]
    </IfModule>
    
    # php -- BEGIN cPanel-generated handler, do not edit
    # NOTE this account's php is controlled via FPM and the vhost, this is a place holder.
    # Do not edit. This next line is to support the cPanel php wrapper (php_cli).
    # AddType application/x-httpd-ea-php70 .php .phtml
    # php -- END cPanel-generated handler, do not edit
    
    
    Any idea what I'm doing wrong and how to fix this? I do not want to enable the proxy subdomains. Thanks.
     
  2. Spork Schivago

    Spork Schivago Well-Known Member

    Joined:
    Jan 21, 2016
    Messages:
    514
    Likes Received:
    54
    Trophy Points:
    28
    Location:
    corning, ny
    cPanel Access Level:
    Root Administrator
    I think I might have gotten it. This is what I added to the end of the Rewrite rules in the /home/<username>/public_html/cpcalendars/.htaccess file:
    Code:
    RewriteCond %{REQUEST_URI} ^(.*)$
    RewriteRule ^(.*)$ "https\:\/\/www\.MyDomain\.com\:2080\/$1" [R=301,L]
    
     
    cPanelMichael likes this.
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    I'm happy to see you were able to find a suitable rewrite rule. Thank you for updating this thread with the outcome.
     
Loading...

Share This Page