How to redirect to subdomain in .htaccess file with multiple domains












0















I have an .htaccess file that sends example1.com, example2.com, and example3.com to different files on my server



I want to make it so example3.com temporarily redirects to a subdomain that has been given its own A record: forums.example3.com. Would like to do it in .htaccess instead of DNS settings. It should show forums.example3.com in the URL bar too. I want to also make it so I can remove the redirect in the future without too much re-coding. So far i cant even figure out the redirect as every attempt at rewriting this working code for the redirect always fails:





Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.
)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+.)example2.com [NC]
RewriteCond %{HTTP_HOST} !^example2.com$
RewriteRule ^(.
)$ http://example2.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([^.]+.)example3.com [NC]
RewriteCond %{HTTP_HOST} !^example3.com$
RewriteRule ^(.
)$ http://example3.com/$1 [R=301,L]
#othersites in othersites folder
RewriteCond $1 !^othersites/
RewriteCond %{HTTP_HOST} ^example2.com$
RewriteRule ^(.)$ /othersites/example2.com/$1 [L]
RewriteCond $1 !^othersites/
RewriteCond %{HTTP_HOST} ^example3.com$
RewriteRule ^(.
)$ /othersites/example3.com/$1 [L]


what code should I add/change to redirect example3.com to forums.example3.com? Preferably in the most SEO friendly way.










share|improve this question



























    0















    I have an .htaccess file that sends example1.com, example2.com, and example3.com to different files on my server



    I want to make it so example3.com temporarily redirects to a subdomain that has been given its own A record: forums.example3.com. Would like to do it in .htaccess instead of DNS settings. It should show forums.example3.com in the URL bar too. I want to also make it so I can remove the redirect in the future without too much re-coding. So far i cant even figure out the redirect as every attempt at rewriting this working code for the redirect always fails:





    Options +FollowSymLinks -MultiViews -Indexes
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]
    RewriteCond %{HTTP_HOST} !^example.com$
    RewriteRule ^(.
    )$ http://example.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^([^.]+.)example2.com [NC]
    RewriteCond %{HTTP_HOST} !^example2.com$
    RewriteRule ^(.
    )$ http://example2.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^([^.]+.)example3.com [NC]
    RewriteCond %{HTTP_HOST} !^example3.com$
    RewriteRule ^(.
    )$ http://example3.com/$1 [R=301,L]
    #othersites in othersites folder
    RewriteCond $1 !^othersites/
    RewriteCond %{HTTP_HOST} ^example2.com$
    RewriteRule ^(.)$ /othersites/example2.com/$1 [L]
    RewriteCond $1 !^othersites/
    RewriteCond %{HTTP_HOST} ^example3.com$
    RewriteRule ^(.
    )$ /othersites/example3.com/$1 [L]


    what code should I add/change to redirect example3.com to forums.example3.com? Preferably in the most SEO friendly way.










    share|improve this question

























      0












      0








      0








      I have an .htaccess file that sends example1.com, example2.com, and example3.com to different files on my server



      I want to make it so example3.com temporarily redirects to a subdomain that has been given its own A record: forums.example3.com. Would like to do it in .htaccess instead of DNS settings. It should show forums.example3.com in the URL bar too. I want to also make it so I can remove the redirect in the future without too much re-coding. So far i cant even figure out the redirect as every attempt at rewriting this working code for the redirect always fails:





      Options +FollowSymLinks -MultiViews -Indexes
      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]
      RewriteCond %{HTTP_HOST} !^example.com$
      RewriteRule ^(.
      )$ http://example.com/$1 [R=301,L]
      RewriteCond %{HTTP_HOST} ^([^.]+.)example2.com [NC]
      RewriteCond %{HTTP_HOST} !^example2.com$
      RewriteRule ^(.
      )$ http://example2.com/$1 [R=301,L]
      RewriteCond %{HTTP_HOST} ^([^.]+.)example3.com [NC]
      RewriteCond %{HTTP_HOST} !^example3.com$
      RewriteRule ^(.
      )$ http://example3.com/$1 [R=301,L]
      #othersites in othersites folder
      RewriteCond $1 !^othersites/
      RewriteCond %{HTTP_HOST} ^example2.com$
      RewriteRule ^(.)$ /othersites/example2.com/$1 [L]
      RewriteCond $1 !^othersites/
      RewriteCond %{HTTP_HOST} ^example3.com$
      RewriteRule ^(.
      )$ /othersites/example3.com/$1 [L]


      what code should I add/change to redirect example3.com to forums.example3.com? Preferably in the most SEO friendly way.










      share|improve this question














      I have an .htaccess file that sends example1.com, example2.com, and example3.com to different files on my server



      I want to make it so example3.com temporarily redirects to a subdomain that has been given its own A record: forums.example3.com. Would like to do it in .htaccess instead of DNS settings. It should show forums.example3.com in the URL bar too. I want to also make it so I can remove the redirect in the future without too much re-coding. So far i cant even figure out the redirect as every attempt at rewriting this working code for the redirect always fails:





      Options +FollowSymLinks -MultiViews -Indexes
      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]
      RewriteCond %{HTTP_HOST} !^example.com$
      RewriteRule ^(.
      )$ http://example.com/$1 [R=301,L]
      RewriteCond %{HTTP_HOST} ^([^.]+.)example2.com [NC]
      RewriteCond %{HTTP_HOST} !^example2.com$
      RewriteRule ^(.
      )$ http://example2.com/$1 [R=301,L]
      RewriteCond %{HTTP_HOST} ^([^.]+.)example3.com [NC]
      RewriteCond %{HTTP_HOST} !^example3.com$
      RewriteRule ^(.
      )$ http://example3.com/$1 [R=301,L]
      #othersites in othersites folder
      RewriteCond $1 !^othersites/
      RewriteCond %{HTTP_HOST} ^example2.com$
      RewriteRule ^(.)$ /othersites/example2.com/$1 [L]
      RewriteCond $1 !^othersites/
      RewriteCond %{HTTP_HOST} ^example3.com$
      RewriteRule ^(.
      )$ /othersites/example3.com/$1 [L]


      what code should I add/change to redirect example3.com to forums.example3.com? Preferably in the most SEO friendly way.







      .htaccess redirect






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 17:14









      Rose AlexanderRose Alexander

      61




      61
























          1 Answer
          1






          active

          oldest

          votes


















          0














          First of all, this rule is most likely wrong: RewriteRule ^(.)$



          One single dot represents one single character, whereas you usually want to allow zero or more.



          What you want instead should be RewriteRule ^(.*)$



          The exclamation mark specifies a negative match. So if the host being requested is example3.com, then this rule will be met:



          RewriteCond %{HTTP_HOST} !^example.com$



          But the previous one would fail anyway:



          RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]



          However I would write more explicit rules and avoid negative matches if unnecessary, as they can easily lead to unwanted results.



          So, the following rule should be enough to redirect example3.com (with or without subdomain) to http://forums.example3.com:



          RewriteEngine On
          RewriteCond %{HTTP_HOST} (.+.)?example3.com$ [NC]
          RewriteRule ^(.)$ http://forums.example3.com/$1 [R=302,L]


          Since you are saying you want to set up a temporary redirect, you may instead use HTTP code 302 for the redirect.






          share|improve this answer
























          • thanks, now how do I tell it to use the files in /othersites/example3.com/forums as well as update the URL to forums.example3.com? appending RewriteCond %{HTTP_HOST} ^forums.example3.com$ RewriteRule ^(.*)$ /addons/example3.com/forums/$1 Doesnt seem to keep forums.example3.com to keep from pulling files from my main html folder, which is example1.com

            – Rose Alexander
            Jan 1 at 20:22













          • Something like this: RewriteCond %{REQUEST_URI} ^/othersites/example3.com/forums$ and you could also use [OR] to regroup your rules, see: httpd.apache.org/docs/current/mod/mod_rewrite.html

            – Anonymous
            Jan 1 at 20:34











          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%2f53997397%2fhow-to-redirect-to-subdomain-in-htaccess-file-with-multiple-domains%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














          First of all, this rule is most likely wrong: RewriteRule ^(.)$



          One single dot represents one single character, whereas you usually want to allow zero or more.



          What you want instead should be RewriteRule ^(.*)$



          The exclamation mark specifies a negative match. So if the host being requested is example3.com, then this rule will be met:



          RewriteCond %{HTTP_HOST} !^example.com$



          But the previous one would fail anyway:



          RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]



          However I would write more explicit rules and avoid negative matches if unnecessary, as they can easily lead to unwanted results.



          So, the following rule should be enough to redirect example3.com (with or without subdomain) to http://forums.example3.com:



          RewriteEngine On
          RewriteCond %{HTTP_HOST} (.+.)?example3.com$ [NC]
          RewriteRule ^(.)$ http://forums.example3.com/$1 [R=302,L]


          Since you are saying you want to set up a temporary redirect, you may instead use HTTP code 302 for the redirect.






          share|improve this answer
























          • thanks, now how do I tell it to use the files in /othersites/example3.com/forums as well as update the URL to forums.example3.com? appending RewriteCond %{HTTP_HOST} ^forums.example3.com$ RewriteRule ^(.*)$ /addons/example3.com/forums/$1 Doesnt seem to keep forums.example3.com to keep from pulling files from my main html folder, which is example1.com

            – Rose Alexander
            Jan 1 at 20:22













          • Something like this: RewriteCond %{REQUEST_URI} ^/othersites/example3.com/forums$ and you could also use [OR] to regroup your rules, see: httpd.apache.org/docs/current/mod/mod_rewrite.html

            – Anonymous
            Jan 1 at 20:34
















          0














          First of all, this rule is most likely wrong: RewriteRule ^(.)$



          One single dot represents one single character, whereas you usually want to allow zero or more.



          What you want instead should be RewriteRule ^(.*)$



          The exclamation mark specifies a negative match. So if the host being requested is example3.com, then this rule will be met:



          RewriteCond %{HTTP_HOST} !^example.com$



          But the previous one would fail anyway:



          RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]



          However I would write more explicit rules and avoid negative matches if unnecessary, as they can easily lead to unwanted results.



          So, the following rule should be enough to redirect example3.com (with or without subdomain) to http://forums.example3.com:



          RewriteEngine On
          RewriteCond %{HTTP_HOST} (.+.)?example3.com$ [NC]
          RewriteRule ^(.)$ http://forums.example3.com/$1 [R=302,L]


          Since you are saying you want to set up a temporary redirect, you may instead use HTTP code 302 for the redirect.






          share|improve this answer
























          • thanks, now how do I tell it to use the files in /othersites/example3.com/forums as well as update the URL to forums.example3.com? appending RewriteCond %{HTTP_HOST} ^forums.example3.com$ RewriteRule ^(.*)$ /addons/example3.com/forums/$1 Doesnt seem to keep forums.example3.com to keep from pulling files from my main html folder, which is example1.com

            – Rose Alexander
            Jan 1 at 20:22













          • Something like this: RewriteCond %{REQUEST_URI} ^/othersites/example3.com/forums$ and you could also use [OR] to regroup your rules, see: httpd.apache.org/docs/current/mod/mod_rewrite.html

            – Anonymous
            Jan 1 at 20:34














          0












          0








          0







          First of all, this rule is most likely wrong: RewriteRule ^(.)$



          One single dot represents one single character, whereas you usually want to allow zero or more.



          What you want instead should be RewriteRule ^(.*)$



          The exclamation mark specifies a negative match. So if the host being requested is example3.com, then this rule will be met:



          RewriteCond %{HTTP_HOST} !^example.com$



          But the previous one would fail anyway:



          RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]



          However I would write more explicit rules and avoid negative matches if unnecessary, as they can easily lead to unwanted results.



          So, the following rule should be enough to redirect example3.com (with or without subdomain) to http://forums.example3.com:



          RewriteEngine On
          RewriteCond %{HTTP_HOST} (.+.)?example3.com$ [NC]
          RewriteRule ^(.)$ http://forums.example3.com/$1 [R=302,L]


          Since you are saying you want to set up a temporary redirect, you may instead use HTTP code 302 for the redirect.






          share|improve this answer













          First of all, this rule is most likely wrong: RewriteRule ^(.)$



          One single dot represents one single character, whereas you usually want to allow zero or more.



          What you want instead should be RewriteRule ^(.*)$



          The exclamation mark specifies a negative match. So if the host being requested is example3.com, then this rule will be met:



          RewriteCond %{HTTP_HOST} !^example.com$



          But the previous one would fail anyway:



          RewriteCond %{HTTP_HOST} ^([^.]+.)example.com [NC]



          However I would write more explicit rules and avoid negative matches if unnecessary, as they can easily lead to unwanted results.



          So, the following rule should be enough to redirect example3.com (with or without subdomain) to http://forums.example3.com:



          RewriteEngine On
          RewriteCond %{HTTP_HOST} (.+.)?example3.com$ [NC]
          RewriteRule ^(.)$ http://forums.example3.com/$1 [R=302,L]


          Since you are saying you want to set up a temporary redirect, you may instead use HTTP code 302 for the redirect.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 19:27









          AnonymousAnonymous

          1973




          1973













          • thanks, now how do I tell it to use the files in /othersites/example3.com/forums as well as update the URL to forums.example3.com? appending RewriteCond %{HTTP_HOST} ^forums.example3.com$ RewriteRule ^(.*)$ /addons/example3.com/forums/$1 Doesnt seem to keep forums.example3.com to keep from pulling files from my main html folder, which is example1.com

            – Rose Alexander
            Jan 1 at 20:22













          • Something like this: RewriteCond %{REQUEST_URI} ^/othersites/example3.com/forums$ and you could also use [OR] to regroup your rules, see: httpd.apache.org/docs/current/mod/mod_rewrite.html

            – Anonymous
            Jan 1 at 20:34



















          • thanks, now how do I tell it to use the files in /othersites/example3.com/forums as well as update the URL to forums.example3.com? appending RewriteCond %{HTTP_HOST} ^forums.example3.com$ RewriteRule ^(.*)$ /addons/example3.com/forums/$1 Doesnt seem to keep forums.example3.com to keep from pulling files from my main html folder, which is example1.com

            – Rose Alexander
            Jan 1 at 20:22













          • Something like this: RewriteCond %{REQUEST_URI} ^/othersites/example3.com/forums$ and you could also use [OR] to regroup your rules, see: httpd.apache.org/docs/current/mod/mod_rewrite.html

            – Anonymous
            Jan 1 at 20:34

















          thanks, now how do I tell it to use the files in /othersites/example3.com/forums as well as update the URL to forums.example3.com? appending RewriteCond %{HTTP_HOST} ^forums.example3.com$ RewriteRule ^(.*)$ /addons/example3.com/forums/$1 Doesnt seem to keep forums.example3.com to keep from pulling files from my main html folder, which is example1.com

          – Rose Alexander
          Jan 1 at 20:22







          thanks, now how do I tell it to use the files in /othersites/example3.com/forums as well as update the URL to forums.example3.com? appending RewriteCond %{HTTP_HOST} ^forums.example3.com$ RewriteRule ^(.*)$ /addons/example3.com/forums/$1 Doesnt seem to keep forums.example3.com to keep from pulling files from my main html folder, which is example1.com

          – Rose Alexander
          Jan 1 at 20:22















          Something like this: RewriteCond %{REQUEST_URI} ^/othersites/example3.com/forums$ and you could also use [OR] to regroup your rules, see: httpd.apache.org/docs/current/mod/mod_rewrite.html

          – Anonymous
          Jan 1 at 20:34





          Something like this: RewriteCond %{REQUEST_URI} ^/othersites/example3.com/forums$ and you could also use [OR] to regroup your rules, see: httpd.apache.org/docs/current/mod/mod_rewrite.html

          – Anonymous
          Jan 1 at 20:34




















          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%2f53997397%2fhow-to-redirect-to-subdomain-in-htaccess-file-with-multiple-domains%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

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          Npm cannot find a required file even through it is in the searched directory