We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15871
    • 105 Posts
    We’ve developed a site under a subdirectory and have pointed beta.domain.com CNAME to it for dev purposes. My question is this, to launch, would you just point DNS to the beta (making that the new root), or would you copy all the files from /beta to the root and launch the site that way.

    Thoughts? Issues with either strategy?

    Tnx.
    Chuck
      • 5689
      • 289 Posts
      A much easier way is to use the power of mod_rewrite via a .htaccess file. It rewrites the URL on the server end to seamlessly "pretend" the subfolder is the root. It’s power is a lot more than doing just that, but it’s a common thing to use it for.

      This code should work for you. Put a text file named .htaccess in the root folder, and make the contents of it to be this. Note that your server needs to have mod_rewrite configured/installed for Apache although it probably already is.

      RewriteEngine On
      RewriteBase /
      
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*/)?[^/.]+$ %{REQUEST_URI}/ [R=301,L]
      
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ beta/$1 [L]


      I don’t know if you know how to use mod_rewrite or not, but here’s a rundown of what this all does. There’s actually some extra code in here that isn’t completely necessary for just making the root of the site point to the beta folder, but my experience with mod_rewrite has led me to figure out that this "extra" code I’ve written/found makes things work better.

      The first line simply tells Apache that mod_rewrite is going to be used. The second line really doesn’t have any effect with your setup, but it doesn’t hurt to include it. It simply tells mod_rewrite that the base path on the site that points to the current folder is the root. For example, if I had an alias or something set up to tell my server that www.example.com/test should really point to a folder called test2 instead of test, my .htaccess file in the test2 folder would need "RewriteBase /test" to tell mod_rewrite that the relative URL path for this folder is /test instead of the actual folder /test2. If that’s confusing, feel free to look up RewriteBase, but in most cases you won’t need it anyway. I just use it for consistency. The block of 5 lines that follows adds a trailing slash to the URL if necessary (if it doesn’t have one and if it’s a folder) to make things easier to work with. This is actually a more complicated way of adding a trailing slash than most people would do, but I wrote this code to be more accurate. It only adds a slash if A) it’s guaranteed to be a folder or B) we know for sure it’s not a file. The reason we can’t just check if it’s a folder and add a slash if there isn’t one, is because when you’re using mod_rewrite in other subfolders, a lot of your URLs won’t point to real files or folders because mod_rewrite in those subfolders will tell the server where to actually look for those files and folders. So we make our best guess and say that if the URL doesn’t end with what looks like a filename (so it doesn’t have a dot), then it is most likely is a folder and it’s safe to add the slash. The last three lines are the important ones. They take any requests to the root folder and reroute them to the beta subfolder without the user knowing. The URL will still be www.example.com, but the server will be retrieving the files from www.example.com/beta.

      I hope that all helps. laugh I’d be glad to explain anything more about that code if you have questions. If you already know how to use mod_rewrite, then you probably didn’t need that explanation at all, but I figured I’d be thorough. smiley

      Edit: I added two more lines to the code near the end that check if the requested file/folder exists before rerouting to the beta subfolder. That way if you have another subfolder of root, test, you can still get to www.example.com/test. However, if we go to, say, www.example.com/why and why does not really exist, the server will be looking for it at beta/why. Again, if you need any more explanation, I’d be glad to give it.

      Edit 2: By the way, using the above code, you will still be able to go to www.example.com/beta. If you want that to redirect to www.example.com just so it looks nicer in case somebody is still using that URL after the switch, I can give you the code to do that as well. Just let me know! laugh
        I'm learning more about MODx all the time and loving it.
        • 15871
        • 105 Posts
        I actually have an htaccess file in the beta directory that is handling the friendly URL aspect for the site (the standard one that comes with the MODx install). So If I also place this new htaccess in the root, would that screw anything up? OR will the htaccess in the root, just redirect all traffic to the beta directory, and then that htaccess file will take over?

        OR

        Should it all just be handled by a single htaccess in the root?

        Thanks so much!
          • 5689
          • 289 Posts
          Glad to help. laugh

          I think you should be able to use both with no problems. I’ve done this, and most of the time it’s fine, but in some cases it seems that the one in the subfolder overrides the one in the parent folder, and only the subfolder one applies. Try with both and see what happens. Otherwise, yeah, you’re going to need to combine them, which shouldn’t be much trouble. I’m pretty sure it’ll work just fine with both, though.
            I'm learning more about MODx all the time and loving it.
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            .htaccess settings are inherited. Settings in a child directory will override those in a parent directory. Some settings cannot be duplicated. For example, RewriteEngine ON in both a parent directory and one of its children can cause 500 server errors; the setting in the child directory is unnecessary since it inherits the setting from its parent.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 33337
              • 3,975 Posts
              I would move the files to the root and modify the necessary paths.
                Zaigham R - MODX Professional | Skype | Email | Twitter

                Digging the interwebs for #MODX gems and bringing it to you. modx.link
                • 15871
                • 105 Posts
                Good comments, excellent!

                @zi any particular reason you wold move the files? Would it be just to keep everything clean?

                @Compeek & @sottwell, thanks for the htaccess directions. I do have one concern about the htaccess in that the client has a load of subfolders in the root (besides their old html based site) that I am concerned would match up to naming structures in the new MODx site.

                Thanks again smiley
                  • 5689
                  • 289 Posts
                  sottwell, thanks for that information. laugh I’ve never been clear on how it works or why I sometimes get 500 errors for no apparent reason.

                  killorbytes, if the client has a lot of other subfolders, I think it may be a lot easier to just move all of the files and update any necessary MODx settings. You’ll save yourself some trouble in the future if the client complains about not being able to access the other folders. If some of these existing subfolders do match up to some of the MODx ones, you’re going to have a problem whether you use .htaccess or just move everything. However, it’s sounding more like it’d be easier just to work it out and move everything now. smiley
                    I'm learning more about MODx all the time and loving it.
                    • 15871
                    • 105 Posts
                    Thank you everyone fro your feedback! I have decided to physically move the files. Thanks again!
                      • 5689
                      • 289 Posts
                      Glad to help. laugh I hope it all works out well!
                        I'm learning more about MODx all the time and loving it.