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!

mod_rewrite, in a cPanel context

Discussion in 'General Discussion' started by curioususer, May 8, 2005.

  1. curioususer

    curioususer Member

    Apr 30, 2005
    Likes Received:
    Trophy Points:
    I've read the official Apache documentation for mod_rewrite, but was extremely surprised that one cannot put all the rules in the top level .htaccess file. An example of this is if you have two parked domains, and rewrite them to subdirectories...

    For example and, both parked domains (the main cPanel domain is irrelevant here).

    So you create subdirectories pd1 and pd2 and write rewrite rules

    RewriteCond %{HTTP_HOST} pd1[.]com$ [nocase]
    RewriteRule ^(.*)$ /pd1/$1

    RewriteCond %{HTTP_HOST} pd2[.]com$ [nocase]
    RewriteRule ^(.*)$ /pd2/$1

    Now someone that accesses will be redirected to /pd1/ and someone that writes will be redirected to /pd2/ and that is all well and good, but doesn't handle the error cases. As long as people only follow links on the site, things will work as you expect, but unfortunately, if someone accesses it will "work", in the sense of serving the default page from the pd2 subdirectory.

    How to prevent that? I played with rules in the /.htaccess for ages until I was thoroughly frustrated, and then I finally had the idea of writing

    RewriteCond %{HTTP_HOST} !pd1[.]com$ [nocase]
    RewriteRule ^(.*)$ /cgi-bin/errdoc.cgi?err=404

    and putting that in /pd1/.htaccess and similar in /pd2/.htaccess, and that worked, redirecting folks to an appropriate error page. To me, this was not at all clear from reading the mod_rewrite documentation and examples.

    I know that .htaccess files are, in general, somewhat hierachical, but it seems that general rule doesn't apply to mod_rewrite... that mod_rewrite walks down to the directory tree

    If anyone can explain the exact relationship of .htaccess files and directory structures, and mod_rewrite rules that take effect, or point me at documentation of same, I'd appreciate it.

    Of course, being in shared hosting on cPanel, there seems to be no way to turn on or view the RewriteLog, so I have to just experiment to see what is going on, instead of being able to see the actual processing that happens, which could be enlightening.

    I had the thought that I could "dump" all the variables to environment variables, and and display them via an ErrorDocument .cgi script... so I wrote stuff like:

    RewriteRule ^(.*)$ $1 [env=MPATH:$1,env=MHH:%{HTTP_HOST},env=MRF:%{REQUEST_FILENAME},env=MURL:%{REQUEST_URI},env=MQS:%{QUERY_STRING},env=MHR:%{HTTP_REFERER}]

    (the above is all on one line in my .htaccess file, but commented out because it doesn't work... The RewriteRule is _not_ an identity, it breaks the redirections when I enable it.

    Maybe I need a RewriteBase other than / ?? I am at the root of my cPanel primary domain (this rule is in /public_html/.htaccess in other words).

    Is there an "identity" function for RewriteRule ? Some syntax that is acceptable, doesn't change anything, and can be used to obtain the side-effect behavior of setting environment variables?

    And/or how do people on shared hosting debug RewriteRule stuff without being able to turn on the RewriteLog ?

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice