How do I redirect an old style post to a new URL format?












2















I have a website that is going to be converted to a new cms. The reason for doing this is to not lose inbound links and end up with dead pages in search engines.



In the old website the URLs are structured like:



website.com/4212/write-a-google-review


and in the new:



website.com/blog?write-a-google-review


although I have some flexibility in that I can add the ID into the URL string as in any of:



website.com/blog?4212&write-a-google-review
website.com/blog?4212-write-a-google-review
website.com/blog?4212/write-a-google-review
website.com/blog?write-a-google-review&4212


I have tried a number of rewrite variations some just matching the ID, others matching the text string, but haven't gotten the right formula yet.



RewriteRule ^4212/?$ blog?4212/write-a-google-review [NC,L] ## 
RewriteRule ^[4212]?$ blog?4212/write-a-google-review [NC,L] ##




Comment added testing the 1st answer (by Croises).



As a singular redirect I found that this works but only if I gave the full url with https://www.mywebsite.com/:



RewriteRule write-a-google-review/?$ https://www.mywebsite.com/blog?write-a-google-review [NC,L]


When I added the following two lines it resulted in a broken page, no styling, no post content output:



RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [L]


So curious I changed it to include the full url like the following and it worked as a regex solution for all entries:



RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(d+(?:/.+?)?)/?$ http://www.mywebsite.com/blog?$1 [L]









share|improve this question





























    2















    I have a website that is going to be converted to a new cms. The reason for doing this is to not lose inbound links and end up with dead pages in search engines.



    In the old website the URLs are structured like:



    website.com/4212/write-a-google-review


    and in the new:



    website.com/blog?write-a-google-review


    although I have some flexibility in that I can add the ID into the URL string as in any of:



    website.com/blog?4212&write-a-google-review
    website.com/blog?4212-write-a-google-review
    website.com/blog?4212/write-a-google-review
    website.com/blog?write-a-google-review&4212


    I have tried a number of rewrite variations some just matching the ID, others matching the text string, but haven't gotten the right formula yet.



    RewriteRule ^4212/?$ blog?4212/write-a-google-review [NC,L] ## 
    RewriteRule ^[4212]?$ blog?4212/write-a-google-review [NC,L] ##




    Comment added testing the 1st answer (by Croises).



    As a singular redirect I found that this works but only if I gave the full url with https://www.mywebsite.com/:



    RewriteRule write-a-google-review/?$ https://www.mywebsite.com/blog?write-a-google-review [NC,L]


    When I added the following two lines it resulted in a broken page, no styling, no post content output:



    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [L]


    So curious I changed it to include the full url like the following and it worked as a regex solution for all entries:



    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(d+(?:/.+?)?)/?$ http://www.mywebsite.com/blog?$1 [L]









    share|improve this question



























      2












      2








      2








      I have a website that is going to be converted to a new cms. The reason for doing this is to not lose inbound links and end up with dead pages in search engines.



      In the old website the URLs are structured like:



      website.com/4212/write-a-google-review


      and in the new:



      website.com/blog?write-a-google-review


      although I have some flexibility in that I can add the ID into the URL string as in any of:



      website.com/blog?4212&write-a-google-review
      website.com/blog?4212-write-a-google-review
      website.com/blog?4212/write-a-google-review
      website.com/blog?write-a-google-review&4212


      I have tried a number of rewrite variations some just matching the ID, others matching the text string, but haven't gotten the right formula yet.



      RewriteRule ^4212/?$ blog?4212/write-a-google-review [NC,L] ## 
      RewriteRule ^[4212]?$ blog?4212/write-a-google-review [NC,L] ##




      Comment added testing the 1st answer (by Croises).



      As a singular redirect I found that this works but only if I gave the full url with https://www.mywebsite.com/:



      RewriteRule write-a-google-review/?$ https://www.mywebsite.com/blog?write-a-google-review [NC,L]


      When I added the following two lines it resulted in a broken page, no styling, no post content output:



      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [L]


      So curious I changed it to include the full url like the following and it worked as a regex solution for all entries:



      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(d+(?:/.+?)?)/?$ http://www.mywebsite.com/blog?$1 [L]









      share|improve this question
















      I have a website that is going to be converted to a new cms. The reason for doing this is to not lose inbound links and end up with dead pages in search engines.



      In the old website the URLs are structured like:



      website.com/4212/write-a-google-review


      and in the new:



      website.com/blog?write-a-google-review


      although I have some flexibility in that I can add the ID into the URL string as in any of:



      website.com/blog?4212&write-a-google-review
      website.com/blog?4212-write-a-google-review
      website.com/blog?4212/write-a-google-review
      website.com/blog?write-a-google-review&4212


      I have tried a number of rewrite variations some just matching the ID, others matching the text string, but haven't gotten the right formula yet.



      RewriteRule ^4212/?$ blog?4212/write-a-google-review [NC,L] ## 
      RewriteRule ^[4212]?$ blog?4212/write-a-google-review [NC,L] ##




      Comment added testing the 1st answer (by Croises).



      As a singular redirect I found that this works but only if I gave the full url with https://www.mywebsite.com/:



      RewriteRule write-a-google-review/?$ https://www.mywebsite.com/blog?write-a-google-review [NC,L]


      When I added the following two lines it resulted in a broken page, no styling, no post content output:



      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [L]


      So curious I changed it to include the full url like the following and it worked as a regex solution for all entries:



      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(d+(?:/.+?)?)/?$ http://www.mywebsite.com/blog?$1 [L]






      .htaccess mod-rewrite






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 29 '18 at 9:37









      Abhishek Gurjar

      5,92772534




      5,92772534










      asked Nov 28 '18 at 15:23









      Bob MBob M

      811210




      811210
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can use:



          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [R=301,QSA,L]





          share|improve this answer


























          • Thx - it works after I proceed the word blog with the full URL including https. I added a comment to the original post.

            – Bob M
            Nov 28 '18 at 23:30











          • It's because you ask for redirection, not rewrite. I add the R=301 for that.

            – Croises
            Nov 29 '18 at 0:46











          • For the problem with css or pictures, add <base href="/"> or path to the folder, in your html head.

            – Croises
            Nov 29 '18 at 0:48



















          0














          RewriteCond %{QUERY_STRING} (^|&)blog?4212($|&)
          RewriteCond %{QUERY_STRING} (^|&)write-a-google-review($|&)
          RewriteRule ^website.com/index.php$ /website.com/blog?4212-write-a-google-review&%{QUERY_STRING}


          From



          website.com/index.php?blog?4212&write-a-google-review


          To



          website.com/blog?4212-write-a-google-review


          Regex



          RewriteRule ^([a-z]+)/([0-9]+)/([a-z0-9-]+)?$ index.php?alias=$1?$2-$3 [L]

          Array
          (
          [alias] => blog?8753-lowering-expectations-with-skype
          )





          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%2f53522790%2fhow-do-i-redirect-an-old-style-post-to-a-new-url-format%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









            1














            You can use:



            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [R=301,QSA,L]





            share|improve this answer


























            • Thx - it works after I proceed the word blog with the full URL including https. I added a comment to the original post.

              – Bob M
              Nov 28 '18 at 23:30











            • It's because you ask for redirection, not rewrite. I add the R=301 for that.

              – Croises
              Nov 29 '18 at 0:46











            • For the problem with css or pictures, add <base href="/"> or path to the folder, in your html head.

              – Croises
              Nov 29 '18 at 0:48
















            1














            You can use:



            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [R=301,QSA,L]





            share|improve this answer


























            • Thx - it works after I proceed the word blog with the full URL including https. I added a comment to the original post.

              – Bob M
              Nov 28 '18 at 23:30











            • It's because you ask for redirection, not rewrite. I add the R=301 for that.

              – Croises
              Nov 29 '18 at 0:46











            • For the problem with css or pictures, add <base href="/"> or path to the folder, in your html head.

              – Croises
              Nov 29 '18 at 0:48














            1












            1








            1







            You can use:



            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [R=301,QSA,L]





            share|improve this answer















            You can use:



            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(d+(?:/.+?)?)/?$ blog?$1 [R=301,QSA,L]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 29 '18 at 0:45

























            answered Nov 28 '18 at 15:49









            CroisesCroises

            17.3k42142




            17.3k42142













            • Thx - it works after I proceed the word blog with the full URL including https. I added a comment to the original post.

              – Bob M
              Nov 28 '18 at 23:30











            • It's because you ask for redirection, not rewrite. I add the R=301 for that.

              – Croises
              Nov 29 '18 at 0:46











            • For the problem with css or pictures, add <base href="/"> or path to the folder, in your html head.

              – Croises
              Nov 29 '18 at 0:48



















            • Thx - it works after I proceed the word blog with the full URL including https. I added a comment to the original post.

              – Bob M
              Nov 28 '18 at 23:30











            • It's because you ask for redirection, not rewrite. I add the R=301 for that.

              – Croises
              Nov 29 '18 at 0:46











            • For the problem with css or pictures, add <base href="/"> or path to the folder, in your html head.

              – Croises
              Nov 29 '18 at 0:48

















            Thx - it works after I proceed the word blog with the full URL including https. I added a comment to the original post.

            – Bob M
            Nov 28 '18 at 23:30





            Thx - it works after I proceed the word blog with the full URL including https. I added a comment to the original post.

            – Bob M
            Nov 28 '18 at 23:30













            It's because you ask for redirection, not rewrite. I add the R=301 for that.

            – Croises
            Nov 29 '18 at 0:46





            It's because you ask for redirection, not rewrite. I add the R=301 for that.

            – Croises
            Nov 29 '18 at 0:46













            For the problem with css or pictures, add <base href="/"> or path to the folder, in your html head.

            – Croises
            Nov 29 '18 at 0:48





            For the problem with css or pictures, add <base href="/"> or path to the folder, in your html head.

            – Croises
            Nov 29 '18 at 0:48













            0














            RewriteCond %{QUERY_STRING} (^|&)blog?4212($|&)
            RewriteCond %{QUERY_STRING} (^|&)write-a-google-review($|&)
            RewriteRule ^website.com/index.php$ /website.com/blog?4212-write-a-google-review&%{QUERY_STRING}


            From



            website.com/index.php?blog?4212&write-a-google-review


            To



            website.com/blog?4212-write-a-google-review


            Regex



            RewriteRule ^([a-z]+)/([0-9]+)/([a-z0-9-]+)?$ index.php?alias=$1?$2-$3 [L]

            Array
            (
            [alias] => blog?8753-lowering-expectations-with-skype
            )





            share|improve this answer






























              0














              RewriteCond %{QUERY_STRING} (^|&)blog?4212($|&)
              RewriteCond %{QUERY_STRING} (^|&)write-a-google-review($|&)
              RewriteRule ^website.com/index.php$ /website.com/blog?4212-write-a-google-review&%{QUERY_STRING}


              From



              website.com/index.php?blog?4212&write-a-google-review


              To



              website.com/blog?4212-write-a-google-review


              Regex



              RewriteRule ^([a-z]+)/([0-9]+)/([a-z0-9-]+)?$ index.php?alias=$1?$2-$3 [L]

              Array
              (
              [alias] => blog?8753-lowering-expectations-with-skype
              )





              share|improve this answer




























                0












                0








                0







                RewriteCond %{QUERY_STRING} (^|&)blog?4212($|&)
                RewriteCond %{QUERY_STRING} (^|&)write-a-google-review($|&)
                RewriteRule ^website.com/index.php$ /website.com/blog?4212-write-a-google-review&%{QUERY_STRING}


                From



                website.com/index.php?blog?4212&write-a-google-review


                To



                website.com/blog?4212-write-a-google-review


                Regex



                RewriteRule ^([a-z]+)/([0-9]+)/([a-z0-9-]+)?$ index.php?alias=$1?$2-$3 [L]

                Array
                (
                [alias] => blog?8753-lowering-expectations-with-skype
                )





                share|improve this answer















                RewriteCond %{QUERY_STRING} (^|&)blog?4212($|&)
                RewriteCond %{QUERY_STRING} (^|&)write-a-google-review($|&)
                RewriteRule ^website.com/index.php$ /website.com/blog?4212-write-a-google-review&%{QUERY_STRING}


                From



                website.com/index.php?blog?4212&write-a-google-review


                To



                website.com/blog?4212-write-a-google-review


                Regex



                RewriteRule ^([a-z]+)/([0-9]+)/([a-z0-9-]+)?$ index.php?alias=$1?$2-$3 [L]

                Array
                (
                [alias] => blog?8753-lowering-expectations-with-skype
                )






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 28 '18 at 17:57

























                answered Nov 28 '18 at 17:15









                Shailesh DwivediShailesh Dwivedi

                282310




                282310






























                    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%2f53522790%2fhow-do-i-redirect-an-old-style-post-to-a-new-url-format%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

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)