Using Mod Rewrite Properly

Discussion in 'Linux VPS/Dedicated - General' started by eJM, Nov 23, 2008.

  1. eJM

    eJM enterpriseJM

    Like many, I have a several domains in my VPS account. The main domain the host account was set up with is, which is in the html_public root, and the rest of my domains are set up in folders in html_public. The directory structure would look like this (click to enlarge):


    My mod rewrite routine doesn't seem to work properly. I do have the redirect to non-www status for, but when it comes to redirecting, for instance, to, it either doesn't work at all, or it redirects to the 1foot domain, but to a 404 error page instead of the home page.

    I have this redirect in the .htaccess file that was automatically put there by the cPanel redirect feature:
    RewriteCond %{HTTP_HOST} ^.*$
    RewriteRule ^1foot/?$ "http\:\/\/1footinthegrave\.com" [R=301,L]
    When I type in the browser's address bar, I get, which results in a 404 page at 1foot.

    When I type in the address bar, I get the 1foot home page, just as it is s'posed to do.

    However, I haven't developed the site yet, so when I type in, I get the directory listing of that folder, which includes only a link to the parent directory and the folder cgi-bin. When I type in, I get and a directory list without the link to the parent directory (as it should be, I think).

    What I want is when someone (or a search spider) follows a link that refers to any subdirectory on that belongs to another domain, I want it to be directed to that domain's home page.

    I hope I haven't confused anyone. To summarize: should redirect to should redirect to

    As a side note, I have some domains set up for [noparse][/noparse] and some as just I have the rewrite in each of the domain folders to redirect from one to the other, but I don't know if that is necessary to set up in the .htaccess file too.

    Thanks for any help with this.

  2. magic

    magic New Member

    Could you please post the entire contents of the .htaccess file? Provided there is nothing security-related inside.

  3. eJM

    eJM enterpriseJM

    Here it is:
    Options +FollowSymLinks
    DirectoryIndex index.php index.html index.htm
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.enterprisejm\.net$
    RewriteRule ^(.*)$$1 [R=permanent,L]
    RewriteCond %{HTTP_HOST} ^$ [OR]
    RewriteCond %{HTTP_HOST} ^$
    RewriteRule ^(.*)$ [R=301,L]
    RewriteCond %{HTTP_HOST} ^.*$
    RewriteRule ^1foot/?$ "http\:\/\/1footinthegrave\.com" [R=301,L]
    That's not all of it. There are similar entries as the last one for each of the domains I have. The rewrite rules I use for the folder ejmcom have the same result as that of 1foot. Typing the domain folder in as a subdomain of gets the user to the home page of the domain located in that folder. But typing the domain folder in as a subfolder of results in a 404 error on the domain located in that folder and the strange address I showed you.


  4. magic

    magic New Member

    Try changing the last line to:
    RewriteRule ^1foot/?$ [R=301,L]
  5. eJM

    eJM enterpriseJM

    I made the change and typed in the address [noparse][/noparse], but it still led to a 404 error page at [noparse][/noparse]

  6. magic

    magic New Member

    In your httpd.conf add the following lines:
    RewriteLog logs/rewrite.log
    RewriteLogLevel 2
    then restart apache. do the page request again in your browser. then immediately go back into httpd.conf and comment out the above section (prefix each line with #) and then restart apache again. find the rewrite.log file in the logs directory of the apache installation and post its contents here
  7. eJM

    eJM enterpriseJM

    Sorry, the only editing of the httpd.conf I am familiar with is the Edit Apache Include Files available in the WHM. That doesn't appear to allow any commenting out or deleting of entries.

    I do have SSH access, but don't know the commands to do any editing of that file. If you could give me the commands, I'll try to follow your directions.

    Then again, isn't there something I can add to the httpd.conf file that would take care of all this for me, like in the <VirtualHost> section or something?

    Thanks for trying to help, I appreciate it.

  8. magic

    magic New Member

    You should file a support ticket. One of the techs can figure it out a lot faster if they can get into your machine than if we try to post back and forth about it here.
  9. eJM

    eJM enterpriseJM

    Okay. I was hoping this was something I could handle myself (learning how here), but you're prob'ly right. Thanks for your efforts.

  10. Dan

    Dan Moderator

    Hello ejm

    Can I ask why you're using addon domains rather than creating separate accounts for each one?
  11. eJM

    eJM enterpriseJM

    Hi Dan,

    Well, the only answer I can give you is, because I don't know any better. My previous experience with hosting has been that all the domains are added in a similar fashion to the way I have shown here (see my attachment). The hosting I have used in the past has always been shared hosting accounts. I didn't know there were other options to setting up domains on the VPS. I would like to do it in the most efficient way and in a way that is also secure and easily managed.

    I would appreciate any advice and direction in doing it better. It's especially important I get it right because a friend wants me to build him a website, but to arrange his own VPS here. He also has several domains he wants to develop over time.

    Thank you,

  12. Dan

    Dan Moderator

    Hi Jim,

    I've just done a write up in the cPanel how to forum on how to create domains/accounts. Unfortunately at this point there's no way for you to move those existing domains over to their own accounts without disrupting their service at least minimally.

    The benefit to having a domain in it's own account is that you determine disk space for each domain, all of their files (including email) are located in their own area, and the users can manage their own email accounts etc. And there will be no messing around with rewrite rules either ;)

    At this point, moving a domain to an account would not be too difficult unless we are also talking about emails.

    How I would go about it:

    Log into the server via SSH.

    Copy all of the domain's files to a temporary directory:

    cd /home/<myacct>/public_html
    mkdir /home/temp
    mkdir /home/temp/<acct-to-move>
    cp -R <acct-to-move>/* /home/temp

    Now with email, if they use POP then there is nothing to worry about as POP downloads all of their email to their client. But if they use IMAP then they will have email in their accounts and I am unsure of the path for add on domains. But if I were to guess I would think it's something like this:

    cd /home/<myacct>/mail/<>
    mkdir /home/temp/<acct-to-move-mail>
    cp -R * /home/temp/<acct-to-move-mail>

    At this point all files and emails have been copied to /home/temp so you can now delete the add on domain and their email accounts. Then create a new account for it.

    We can then copy the domain's files to the new account:

    cd /home/temp/<acct-to-move>
    cp -R * /home/<new-acct>/public_html

    Again, email, a sticky wicket. Recreate the email accounts in their cPanel. Copying the files cannot be done by wildcard I do not think though and each inbox will most likely have a minimum of 4 directorys (Inbox, Inbox/.Drafts, Inbox/.Sent, and Inbox/.Trash) and there are 2 directories in each we're concerned with (cur and new). So while there will be a lot of steps they will be repetitive. If you look at these directories and the format does not look similar then either adjust the paths or let me know and we can try to figure it out from there. Here goes:

    cd /home/temp/<acct-to-move-mail>/<inbox1>
    cp cur/* /home/<new-acct>/mail/<>/<inbox1>/cur
    cp new/* /home/<new-acct>/mail/<>/<inbox1>/new
    cp .Drafts/cur/* /home/<new-acct>/mail/<>/<inbox1>/.Drafts/cur
    cp .Drafts/new/* /home/<new-acct>/mail/<>/<inbox1>/.Drafts/new
    cp .Sent/cur/* /home/<new-acct>/mail/<>/<inbox1>/.Sent/cur
    cp .Sent/new/* /home/<new-acct>/mail/<>/<inbox1>/.Sent/new
    cp .Trash/cur/* /home/<new-acct>/mail/<>/<inbox1>/.Trash/cur
    cp .Trash/new/* /home/<new-acct>/mail/<>/<inbox1>/.Trash/new

    If they have created custom folders then repeat this for each of them. And then repeat for each inbox.

    At this point all of their files and emails have been copied into their new account. The files, however, are still owned by the previous account so you need to change ownership on them:

    chown -R <new-acct>:<new-acct> /home/<new-acct>/*

    Then do a bit of housecleaning (deleting the files from the temp folder):

    rm -rf /home/temp/*

    Repeat for all accounts you want to move and then when all accounts are moved delete the temp directory:

    rm -rf /home/temp
  13. eJM

    eJM enterpriseJM

    Hi Dan,

    I'da replied sooner, but I spent most of the day pouting over how I blundered into setting up my VPS accounts and domains without first checking with others that had been through this before. Okay, I'm through pouting and I'd really like to do something about it.

    I had some domains that were completely or practically dormant, so I re-parked them at my registrar and deleted them from my VPS account. I deleted their associated email accounts as well. Then I contacted the owners of some of the sites I've built and asked about their email accounts. Surprisingly (or not), most have not even set up their accounts on their computers and had not used the WebMail feature - and none had used any disk space. So I deleted those email accounts for now. That still leaves a few email accounts, but it won't be as much work as it could have been.

    That was well done, thank you. I understand that moving domains to their own accounts will cause some down-time, but if I get it right, it shouldn't be too much trouble for me or the site owners. They all have given me a free hand. I think this may turn out to be far more advantageous for the site owners because it will make it much easier for them to control when it comes time for them to arrange other hosting.

    My "customers" are mostly older flooring professionals that have asked me to manage everything for them because they either can't or don't want to. So, I still have to do a lot of that.

    Would "<acct-to-move>" equal the folder name I have those domains in?

    So, if the email account uses POP, then I can delete the account and then recreate it after the move? Only one client uses strictly WebMail. I don't know what protocol that is or if I follow your directions for that.

  14. Dan

    Dan Moderator

    Hi Jim!

    It sounds to me like you're in a very good position if you only have 1 email account to worry about. Webmail uses imap as well as the email still resides on the server.

    First, you mention that you're the webmaster for all these domains as well? If that's the case and you already have all the files for their content you do not have to copy them to a temp folder you can simply re-upload them using FTP to the new account once you've created it. If you're not or you don't have them then you can always download/upload them using FTP if you would rather do it that way.

    That is correct, yes.

    If you want to look at the paths I've given you and we can figure out what they would actually be we can do that or I am sure that support would be more than willing to help you out with it.


    Not knowing where you created the database (WHM or cPanel) I'll say log into whichever it was.

    Go to phpMyAdmin and then click on the database in the lefthand menu.

    Click on the Export tab and leave everything at the default except at the bottom click to save it as a file.

    Once you click Go it will prompt you for where to save it and then it will do so.

    Once you have created the domain's account then you can create the database the same way you did before. Create the user for it with the same name and password.

    Go to phpMyAdmin and click on the new database.

    Click the Import tab then browse to the file and click go.

    There are a couple of additional things you will probably have to do though. The old database will either have a name of <your-acct>_dbname or simply dbname and once you create it in the new account it will become <new-acct>_dbname. If that is kept in the database you will need to find it and change it. This will also be the case with the user (it will need to be changed from <your-acct>_username to <new-acct>_username).

    After importing the database click the Search tab and put the database name into the search box. Select all tables and click Go.

    If results come back then click on Browse and that will show you the data. Beneath that box will be a small table click on the symbol of a pencil to edit it then put the new database name in where applicable which will be of the format <new-acct>_dbname. Repeat this for the database user you have configured.

    The software's config file (config.php or configure.php or some such) will most likely be configured for the database name and user. Search the config file for the old names and fix them. There may also be an absolute path which will need to be corrected. The url to access the site should stay the same but you may want to check it to be sure.

    I think that's about it...

    You should be able to upgrade your existing VPS to a larger package without a server move at all. When you're ready just shoot off the request and they will fix you right up!

Share This Page