How to redirect root folder as subfolder to site?












0














I am trying to implement the Wordpress REST API with React. Modern convention seems to be to set an API as a subfolder to a domain, i.e.



https://sitename.com/api/v1

I want to be able to go to
https://sitename.com/api/v1/wp-admin

And be able to sign into wordpress and have it so the REST api is accessed like:

https://sitename.com/api/v1/wp-json/wp/v2/pages


I have this file structure:





Inside api/ is the wordpress install. If I include the api inside the public folder and point towards the build folder with the virtual host, it works as desired, except for the fact that when I build the React app it clears the Wordpress install and restarts that whole process.



I tried setting the virtual host to the subfolder, but it is still returning a 404.



Edit: To get a simple instance of this running:



yarn create-react-app dir_name
cd dir_name
mkdir api
cd api
wp core download
# Add virtual host
# Generate SSL Cert









share|improve this question



























    0














    I am trying to implement the Wordpress REST API with React. Modern convention seems to be to set an API as a subfolder to a domain, i.e.



    https://sitename.com/api/v1

    I want to be able to go to
    https://sitename.com/api/v1/wp-admin

    And be able to sign into wordpress and have it so the REST api is accessed like:

    https://sitename.com/api/v1/wp-json/wp/v2/pages


    I have this file structure:





    Inside api/ is the wordpress install. If I include the api inside the public folder and point towards the build folder with the virtual host, it works as desired, except for the fact that when I build the React app it clears the Wordpress install and restarts that whole process.



    I tried setting the virtual host to the subfolder, but it is still returning a 404.



    Edit: To get a simple instance of this running:



    yarn create-react-app dir_name
    cd dir_name
    mkdir api
    cd api
    wp core download
    # Add virtual host
    # Generate SSL Cert









    share|improve this question

























      0












      0








      0







      I am trying to implement the Wordpress REST API with React. Modern convention seems to be to set an API as a subfolder to a domain, i.e.



      https://sitename.com/api/v1

      I want to be able to go to
      https://sitename.com/api/v1/wp-admin

      And be able to sign into wordpress and have it so the REST api is accessed like:

      https://sitename.com/api/v1/wp-json/wp/v2/pages


      I have this file structure:





      Inside api/ is the wordpress install. If I include the api inside the public folder and point towards the build folder with the virtual host, it works as desired, except for the fact that when I build the React app it clears the Wordpress install and restarts that whole process.



      I tried setting the virtual host to the subfolder, but it is still returning a 404.



      Edit: To get a simple instance of this running:



      yarn create-react-app dir_name
      cd dir_name
      mkdir api
      cd api
      wp core download
      # Add virtual host
      # Generate SSL Cert









      share|improve this question













      I am trying to implement the Wordpress REST API with React. Modern convention seems to be to set an API as a subfolder to a domain, i.e.



      https://sitename.com/api/v1

      I want to be able to go to
      https://sitename.com/api/v1/wp-admin

      And be able to sign into wordpress and have it so the REST api is accessed like:

      https://sitename.com/api/v1/wp-json/wp/v2/pages


      I have this file structure:





      Inside api/ is the wordpress install. If I include the api inside the public folder and point towards the build folder with the virtual host, it works as desired, except for the fact that when I build the React app it clears the Wordpress install and restarts that whole process.



      I tried setting the virtual host to the subfolder, but it is still returning a 404.



      Edit: To get a simple instance of this running:



      yarn create-react-app dir_name
      cd dir_name
      mkdir api
      cd api
      wp core download
      # Add virtual host
      # Generate SSL Cert






      wordpress reactjs rest api virtualhost






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 24 '18 at 14:31









      Daniel Foust

      1241213




      1241213
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I believe if you install WordPress in the /api subfolder, that is considered root for the WP instance so no prefix will be added to the REST API endpoint.



          To achieve that, you should use the "rest_url_prefix" filter. Something like:



          add_filter( 'rest_url_prefix', 'change_api_slug');
          function change_api_slug( $slug ) {

          return 'api';

          }


          Here is a pretty good article: https://buddydev.com/modifying-api-base-url-prefix-for-wp-rest-api-plugin/



          Hope it helps :)






          share|improve this answer





















          • That doesn't really address my question. My virtual host is returning 404 for the wordpress install inside the api/ folder. I feel this is because the React App is already using the domain and so there is a conflict with the domain. I've solved it currently by adding a symbolic link to the build folder, but i feel that isn't the correct way to solve it.
            – Daniel Foust
            Dec 26 '18 at 0:50



















          0














          The best I've came up with is attaching a symbolic link to the api/ directory and linking it to the build/ directory.



          # Source->Destination
          ln -s /var/www/site.com/public/api /var/www/site.com/build/


          Depending on your configuration you may have to change api/ group once you do a yarn build for the server to have access for Wordpress (to add plugins for example).



          Quick run through:



          ln -s /var/www/site.com/api /var/www/site.com/public/
          yarn build/
          sudo chown -R :www-data build/





          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%2f53914705%2fhow-to-redirect-root-folder-as-subfolder-to-site%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I believe if you install WordPress in the /api subfolder, that is considered root for the WP instance so no prefix will be added to the REST API endpoint.



            To achieve that, you should use the "rest_url_prefix" filter. Something like:



            add_filter( 'rest_url_prefix', 'change_api_slug');
            function change_api_slug( $slug ) {

            return 'api';

            }


            Here is a pretty good article: https://buddydev.com/modifying-api-base-url-prefix-for-wp-rest-api-plugin/



            Hope it helps :)






            share|improve this answer





















            • That doesn't really address my question. My virtual host is returning 404 for the wordpress install inside the api/ folder. I feel this is because the React App is already using the domain and so there is a conflict with the domain. I've solved it currently by adding a symbolic link to the build folder, but i feel that isn't the correct way to solve it.
              – Daniel Foust
              Dec 26 '18 at 0:50
















            0














            I believe if you install WordPress in the /api subfolder, that is considered root for the WP instance so no prefix will be added to the REST API endpoint.



            To achieve that, you should use the "rest_url_prefix" filter. Something like:



            add_filter( 'rest_url_prefix', 'change_api_slug');
            function change_api_slug( $slug ) {

            return 'api';

            }


            Here is a pretty good article: https://buddydev.com/modifying-api-base-url-prefix-for-wp-rest-api-plugin/



            Hope it helps :)






            share|improve this answer





















            • That doesn't really address my question. My virtual host is returning 404 for the wordpress install inside the api/ folder. I feel this is because the React App is already using the domain and so there is a conflict with the domain. I've solved it currently by adding a symbolic link to the build folder, but i feel that isn't the correct way to solve it.
              – Daniel Foust
              Dec 26 '18 at 0:50














            0












            0








            0






            I believe if you install WordPress in the /api subfolder, that is considered root for the WP instance so no prefix will be added to the REST API endpoint.



            To achieve that, you should use the "rest_url_prefix" filter. Something like:



            add_filter( 'rest_url_prefix', 'change_api_slug');
            function change_api_slug( $slug ) {

            return 'api';

            }


            Here is a pretty good article: https://buddydev.com/modifying-api-base-url-prefix-for-wp-rest-api-plugin/



            Hope it helps :)






            share|improve this answer












            I believe if you install WordPress in the /api subfolder, that is considered root for the WP instance so no prefix will be added to the REST API endpoint.



            To achieve that, you should use the "rest_url_prefix" filter. Something like:



            add_filter( 'rest_url_prefix', 'change_api_slug');
            function change_api_slug( $slug ) {

            return 'api';

            }


            Here is a pretty good article: https://buddydev.com/modifying-api-base-url-prefix-for-wp-rest-api-plugin/



            Hope it helps :)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 24 '18 at 17:51









            FrancescoCarlucci

            726610




            726610












            • That doesn't really address my question. My virtual host is returning 404 for the wordpress install inside the api/ folder. I feel this is because the React App is already using the domain and so there is a conflict with the domain. I've solved it currently by adding a symbolic link to the build folder, but i feel that isn't the correct way to solve it.
              – Daniel Foust
              Dec 26 '18 at 0:50


















            • That doesn't really address my question. My virtual host is returning 404 for the wordpress install inside the api/ folder. I feel this is because the React App is already using the domain and so there is a conflict with the domain. I've solved it currently by adding a symbolic link to the build folder, but i feel that isn't the correct way to solve it.
              – Daniel Foust
              Dec 26 '18 at 0:50
















            That doesn't really address my question. My virtual host is returning 404 for the wordpress install inside the api/ folder. I feel this is because the React App is already using the domain and so there is a conflict with the domain. I've solved it currently by adding a symbolic link to the build folder, but i feel that isn't the correct way to solve it.
            – Daniel Foust
            Dec 26 '18 at 0:50




            That doesn't really address my question. My virtual host is returning 404 for the wordpress install inside the api/ folder. I feel this is because the React App is already using the domain and so there is a conflict with the domain. I've solved it currently by adding a symbolic link to the build folder, but i feel that isn't the correct way to solve it.
            – Daniel Foust
            Dec 26 '18 at 0:50













            0














            The best I've came up with is attaching a symbolic link to the api/ directory and linking it to the build/ directory.



            # Source->Destination
            ln -s /var/www/site.com/public/api /var/www/site.com/build/


            Depending on your configuration you may have to change api/ group once you do a yarn build for the server to have access for Wordpress (to add plugins for example).



            Quick run through:



            ln -s /var/www/site.com/api /var/www/site.com/public/
            yarn build/
            sudo chown -R :www-data build/





            share|improve this answer




























              0














              The best I've came up with is attaching a symbolic link to the api/ directory and linking it to the build/ directory.



              # Source->Destination
              ln -s /var/www/site.com/public/api /var/www/site.com/build/


              Depending on your configuration you may have to change api/ group once you do a yarn build for the server to have access for Wordpress (to add plugins for example).



              Quick run through:



              ln -s /var/www/site.com/api /var/www/site.com/public/
              yarn build/
              sudo chown -R :www-data build/





              share|improve this answer


























                0












                0








                0






                The best I've came up with is attaching a symbolic link to the api/ directory and linking it to the build/ directory.



                # Source->Destination
                ln -s /var/www/site.com/public/api /var/www/site.com/build/


                Depending on your configuration you may have to change api/ group once you do a yarn build for the server to have access for Wordpress (to add plugins for example).



                Quick run through:



                ln -s /var/www/site.com/api /var/www/site.com/public/
                yarn build/
                sudo chown -R :www-data build/





                share|improve this answer














                The best I've came up with is attaching a symbolic link to the api/ directory and linking it to the build/ directory.



                # Source->Destination
                ln -s /var/www/site.com/public/api /var/www/site.com/build/


                Depending on your configuration you may have to change api/ group once you do a yarn build for the server to have access for Wordpress (to add plugins for example).



                Quick run through:



                ln -s /var/www/site.com/api /var/www/site.com/public/
                yarn build/
                sudo chown -R :www-data build/






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Dec 27 '18 at 18:13

























                answered Dec 27 '18 at 17:57









                Daniel Foust

                1241213




                1241213






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53914705%2fhow-to-redirect-root-folder-as-subfolder-to-site%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

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas

                    Can't read property showImagePicker of undefined in react native iOS