How can I load content from two different folders (based on the subdomain) and keep the old URLs?












0















My website is example.com.



In my public_html folder, I have two folders. One is called main, and the other is called report.



When somebody navigates to example.com, I want to serve content from the main folder, but I want their URL to remain the same.



So, if they navigate to example.com/file.html, I want them to see the file.html file that's in the main folder, but I don't want their URL to change to example.com/main/file.html.



Similarly, if they navigate to report.example.com/report1.pdf, I want them to see the report1.pdf file that's in the report folder, but I don't want their URL to change to report.example.com/report/report1.pdf.



The closest thing I've been able to achieve is this:



DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^((?!main).*)$ /main/$1

RewriteCond %{HTTP_HOST} ^report.example.com$ [NC]
RewriteRule ^((?!report).*)$ /report/$1 [L]


That seems to work to some extent, but:




  • If somebody navigates to http://example.com/report/index.html, the server incorrectly loads the index.html file from the public_html/report folder rather than the public_html/main/report folder. Normally that path would make sense, but in this case I'm trying to change that behavior.


  • If somebody navigates to http://report.example.com/test, the server correctly displays the public_html/test/index.html file, but redirects them to http://report.example.com/report/test/.



How can I load the correct files and keep the URLs correct?










share|improve this question























  • Do you have access to apache configuration files, or .htaccess is the only option?

    – Dusan Bajic
    Jan 1 at 11:22











  • @DusanBajic - We're using Cloudways, so we have some options but not total control.

    – Pikamander2
    Jan 2 at 2:32
















0















My website is example.com.



In my public_html folder, I have two folders. One is called main, and the other is called report.



When somebody navigates to example.com, I want to serve content from the main folder, but I want their URL to remain the same.



So, if they navigate to example.com/file.html, I want them to see the file.html file that's in the main folder, but I don't want their URL to change to example.com/main/file.html.



Similarly, if they navigate to report.example.com/report1.pdf, I want them to see the report1.pdf file that's in the report folder, but I don't want their URL to change to report.example.com/report/report1.pdf.



The closest thing I've been able to achieve is this:



DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^((?!main).*)$ /main/$1

RewriteCond %{HTTP_HOST} ^report.example.com$ [NC]
RewriteRule ^((?!report).*)$ /report/$1 [L]


That seems to work to some extent, but:




  • If somebody navigates to http://example.com/report/index.html, the server incorrectly loads the index.html file from the public_html/report folder rather than the public_html/main/report folder. Normally that path would make sense, but in this case I'm trying to change that behavior.


  • If somebody navigates to http://report.example.com/test, the server correctly displays the public_html/test/index.html file, but redirects them to http://report.example.com/report/test/.



How can I load the correct files and keep the URLs correct?










share|improve this question























  • Do you have access to apache configuration files, or .htaccess is the only option?

    – Dusan Bajic
    Jan 1 at 11:22











  • @DusanBajic - We're using Cloudways, so we have some options but not total control.

    – Pikamander2
    Jan 2 at 2:32














0












0








0








My website is example.com.



In my public_html folder, I have two folders. One is called main, and the other is called report.



When somebody navigates to example.com, I want to serve content from the main folder, but I want their URL to remain the same.



So, if they navigate to example.com/file.html, I want them to see the file.html file that's in the main folder, but I don't want their URL to change to example.com/main/file.html.



Similarly, if they navigate to report.example.com/report1.pdf, I want them to see the report1.pdf file that's in the report folder, but I don't want their URL to change to report.example.com/report/report1.pdf.



The closest thing I've been able to achieve is this:



DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^((?!main).*)$ /main/$1

RewriteCond %{HTTP_HOST} ^report.example.com$ [NC]
RewriteRule ^((?!report).*)$ /report/$1 [L]


That seems to work to some extent, but:




  • If somebody navigates to http://example.com/report/index.html, the server incorrectly loads the index.html file from the public_html/report folder rather than the public_html/main/report folder. Normally that path would make sense, but in this case I'm trying to change that behavior.


  • If somebody navigates to http://report.example.com/test, the server correctly displays the public_html/test/index.html file, but redirects them to http://report.example.com/report/test/.



How can I load the correct files and keep the URLs correct?










share|improve this question














My website is example.com.



In my public_html folder, I have two folders. One is called main, and the other is called report.



When somebody navigates to example.com, I want to serve content from the main folder, but I want their URL to remain the same.



So, if they navigate to example.com/file.html, I want them to see the file.html file that's in the main folder, but I don't want their URL to change to example.com/main/file.html.



Similarly, if they navigate to report.example.com/report1.pdf, I want them to see the report1.pdf file that's in the report folder, but I don't want their URL to change to report.example.com/report/report1.pdf.



The closest thing I've been able to achieve is this:



DirectoryIndex index.php index.html index.htm
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^((?!main).*)$ /main/$1

RewriteCond %{HTTP_HOST} ^report.example.com$ [NC]
RewriteRule ^((?!report).*)$ /report/$1 [L]


That seems to work to some extent, but:




  • If somebody navigates to http://example.com/report/index.html, the server incorrectly loads the index.html file from the public_html/report folder rather than the public_html/main/report folder. Normally that path would make sense, but in this case I'm trying to change that behavior.


  • If somebody navigates to http://report.example.com/test, the server correctly displays the public_html/test/index.html file, but redirects them to http://report.example.com/report/test/.



How can I load the correct files and keep the URLs correct?







regex apache .htaccess url-rewriting subdomain






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 6:33









Pikamander2Pikamander2

2,13722134




2,13722134













  • Do you have access to apache configuration files, or .htaccess is the only option?

    – Dusan Bajic
    Jan 1 at 11:22











  • @DusanBajic - We're using Cloudways, so we have some options but not total control.

    – Pikamander2
    Jan 2 at 2:32



















  • Do you have access to apache configuration files, or .htaccess is the only option?

    – Dusan Bajic
    Jan 1 at 11:22











  • @DusanBajic - We're using Cloudways, so we have some options but not total control.

    – Pikamander2
    Jan 2 at 2:32

















Do you have access to apache configuration files, or .htaccess is the only option?

– Dusan Bajic
Jan 1 at 11:22





Do you have access to apache configuration files, or .htaccess is the only option?

– Dusan Bajic
Jan 1 at 11:22













@DusanBajic - We're using Cloudways, so we have some options but not total control.

– Pikamander2
Jan 2 at 2:32





@DusanBajic - We're using Cloudways, so we have some options but not total control.

– Pikamander2
Jan 2 at 2:32












1 Answer
1






active

oldest

votes


















0














This should do:



RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /main%{REQUEST_URI} [L]

RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^ /reports%{REQUEST_URI} [L]





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993488%2fhow-can-i-load-content-from-two-different-folders-based-on-the-subdomain-and-k%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This should do:



    RewriteCond %{HTTP_HOST} ^example.com$ [NC]
    RewriteCond %{ENV:REDIRECT_STATUS} !200
    RewriteRule ^ /main%{REQUEST_URI} [L]

    RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
    RewriteCond %{ENV:REDIRECT_STATUS} !200
    RewriteRule ^ /reports%{REQUEST_URI} [L]





    share|improve this answer




























      0














      This should do:



      RewriteCond %{HTTP_HOST} ^example.com$ [NC]
      RewriteCond %{ENV:REDIRECT_STATUS} !200
      RewriteRule ^ /main%{REQUEST_URI} [L]

      RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
      RewriteCond %{ENV:REDIRECT_STATUS} !200
      RewriteRule ^ /reports%{REQUEST_URI} [L]





      share|improve this answer


























        0












        0








        0







        This should do:



        RewriteCond %{HTTP_HOST} ^example.com$ [NC]
        RewriteCond %{ENV:REDIRECT_STATUS} !200
        RewriteRule ^ /main%{REQUEST_URI} [L]

        RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
        RewriteCond %{ENV:REDIRECT_STATUS} !200
        RewriteRule ^ /reports%{REQUEST_URI} [L]





        share|improve this answer













        This should do:



        RewriteCond %{HTTP_HOST} ^example.com$ [NC]
        RewriteCond %{ENV:REDIRECT_STATUS} !200
        RewriteRule ^ /main%{REQUEST_URI} [L]

        RewriteCond %{HTTP_HOST} ^reports.example.com$ [NC]
        RewriteCond %{ENV:REDIRECT_STATUS} !200
        RewriteRule ^ /reports%{REQUEST_URI} [L]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 9:25









        Dusan BajicDusan Bajic

        6,10831826




        6,10831826
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993488%2fhow-can-i-load-content-from-two-different-folders-based-on-the-subdomain-and-k%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'