How to add a slash at the end of category's url and remove it at the end of post's on wordpress?












0















How to make wordpress category's URL with slash at the end and without it in post's url. Like this:



"mysite.com/mycategory/"
"mysite.com/mycategory/mypost"



The problem is that by default, you can either do everything with a slash or all without a slash.
('category' prefix already removed via htaccess).










share|improve this question





























    0















    How to make wordpress category's URL with slash at the end and without it in post's url. Like this:



    "mysite.com/mycategory/"
    "mysite.com/mycategory/mypost"



    The problem is that by default, you can either do everything with a slash or all without a slash.
    ('category' prefix already removed via htaccess).










    share|improve this question



























      0












      0








      0


      0






      How to make wordpress category's URL with slash at the end and without it in post's url. Like this:



      "mysite.com/mycategory/"
      "mysite.com/mycategory/mypost"



      The problem is that by default, you can either do everything with a slash or all without a slash.
      ('category' prefix already removed via htaccess).










      share|improve this question
















      How to make wordpress category's URL with slash at the end and without it in post's url. Like this:



      "mysite.com/mycategory/"
      "mysite.com/mycategory/mypost"



      The problem is that by default, you can either do everything with a slash or all without a slash.
      ('category' prefix already removed via htaccess).







      php wordpress url category permalinks






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday







      Samat Zhanbekov

















      asked 2 days ago









      Samat ZhanbekovSamat Zhanbekov

      263416




      263416
























          2 Answers
          2






          active

          oldest

          votes


















          1














          There are two good solutions for this:



          The WP_Rewrite class has a var named $use_trailing_slashes that is set dynamically based upon whether or not your custom permalink structure ends in a '/'.



          $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );


          This means that all WP generated links (the_permalink, category_link, the_permalink_rss, etc.) will not end in a '/'. So for category pages WP will show '/category/category' instead of '/category/category/'.



          You can solve it by either using a filter or modifying your .htaccess or both:



          Sample user_trailingslashit Filter



          The user_trailingslashit function applies the 'user_trailingslashit' filter to the result prior to returning it. It provides the url and the type of url to the filter.



          $string = apply_filters('user_trailingslashit', $string, $type_of_url);


          So to hook into this and add a trailing slash to all urls other than single posts add this code to a plugin file or your functions.php theme file.



          function fix_trailingsss($s='',$t='single')
          {
          if($t!='single')$s=rtrim($s,'/').'/';
          return preg_replace('/^(.*)([^l/])$/i', '12/',$s);
          }
          add_filter('user_trailingslashit', 'fix_trailingsss', 9999,2);


          Htaccess RedirectMatch



          You can setup an .htaccess redirect to force category urls to always use a trailing slash like this:



          RedirectMatch 301 ^/category/([^/]+)$ /category/$1/


          Source of information: https://www.askapache.com/wordpress/adding-trailing-permalinks/



          For any further questions consult the codex:



          https://codex.wordpress.org/wp_rewrite



          https://codex.wordpress.org/Using_Permalinks






          share|improve this answer































            0














            I solved it like this:



            function no_page_slash( $string, $type ){
            if($type == 'single')
            $string = untrailingslashit($string);
            return $string;
            }
            add_filter('user_trailingslashit', 'no_page_slash', 70, 2);


            Your permalinks must be set on like /%category%/%postname%/ .






            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%2f54240232%2fhow-to-add-a-slash-at-the-end-of-categorys-url-and-remove-it-at-the-end-of-post%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














              There are two good solutions for this:



              The WP_Rewrite class has a var named $use_trailing_slashes that is set dynamically based upon whether or not your custom permalink structure ends in a '/'.



              $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );


              This means that all WP generated links (the_permalink, category_link, the_permalink_rss, etc.) will not end in a '/'. So for category pages WP will show '/category/category' instead of '/category/category/'.



              You can solve it by either using a filter or modifying your .htaccess or both:



              Sample user_trailingslashit Filter



              The user_trailingslashit function applies the 'user_trailingslashit' filter to the result prior to returning it. It provides the url and the type of url to the filter.



              $string = apply_filters('user_trailingslashit', $string, $type_of_url);


              So to hook into this and add a trailing slash to all urls other than single posts add this code to a plugin file or your functions.php theme file.



              function fix_trailingsss($s='',$t='single')
              {
              if($t!='single')$s=rtrim($s,'/').'/';
              return preg_replace('/^(.*)([^l/])$/i', '12/',$s);
              }
              add_filter('user_trailingslashit', 'fix_trailingsss', 9999,2);


              Htaccess RedirectMatch



              You can setup an .htaccess redirect to force category urls to always use a trailing slash like this:



              RedirectMatch 301 ^/category/([^/]+)$ /category/$1/


              Source of information: https://www.askapache.com/wordpress/adding-trailing-permalinks/



              For any further questions consult the codex:



              https://codex.wordpress.org/wp_rewrite



              https://codex.wordpress.org/Using_Permalinks






              share|improve this answer




























                1














                There are two good solutions for this:



                The WP_Rewrite class has a var named $use_trailing_slashes that is set dynamically based upon whether or not your custom permalink structure ends in a '/'.



                $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );


                This means that all WP generated links (the_permalink, category_link, the_permalink_rss, etc.) will not end in a '/'. So for category pages WP will show '/category/category' instead of '/category/category/'.



                You can solve it by either using a filter or modifying your .htaccess or both:



                Sample user_trailingslashit Filter



                The user_trailingslashit function applies the 'user_trailingslashit' filter to the result prior to returning it. It provides the url and the type of url to the filter.



                $string = apply_filters('user_trailingslashit', $string, $type_of_url);


                So to hook into this and add a trailing slash to all urls other than single posts add this code to a plugin file or your functions.php theme file.



                function fix_trailingsss($s='',$t='single')
                {
                if($t!='single')$s=rtrim($s,'/').'/';
                return preg_replace('/^(.*)([^l/])$/i', '12/',$s);
                }
                add_filter('user_trailingslashit', 'fix_trailingsss', 9999,2);


                Htaccess RedirectMatch



                You can setup an .htaccess redirect to force category urls to always use a trailing slash like this:



                RedirectMatch 301 ^/category/([^/]+)$ /category/$1/


                Source of information: https://www.askapache.com/wordpress/adding-trailing-permalinks/



                For any further questions consult the codex:



                https://codex.wordpress.org/wp_rewrite



                https://codex.wordpress.org/Using_Permalinks






                share|improve this answer


























                  1












                  1








                  1







                  There are two good solutions for this:



                  The WP_Rewrite class has a var named $use_trailing_slashes that is set dynamically based upon whether or not your custom permalink structure ends in a '/'.



                  $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );


                  This means that all WP generated links (the_permalink, category_link, the_permalink_rss, etc.) will not end in a '/'. So for category pages WP will show '/category/category' instead of '/category/category/'.



                  You can solve it by either using a filter or modifying your .htaccess or both:



                  Sample user_trailingslashit Filter



                  The user_trailingslashit function applies the 'user_trailingslashit' filter to the result prior to returning it. It provides the url and the type of url to the filter.



                  $string = apply_filters('user_trailingslashit', $string, $type_of_url);


                  So to hook into this and add a trailing slash to all urls other than single posts add this code to a plugin file or your functions.php theme file.



                  function fix_trailingsss($s='',$t='single')
                  {
                  if($t!='single')$s=rtrim($s,'/').'/';
                  return preg_replace('/^(.*)([^l/])$/i', '12/',$s);
                  }
                  add_filter('user_trailingslashit', 'fix_trailingsss', 9999,2);


                  Htaccess RedirectMatch



                  You can setup an .htaccess redirect to force category urls to always use a trailing slash like this:



                  RedirectMatch 301 ^/category/([^/]+)$ /category/$1/


                  Source of information: https://www.askapache.com/wordpress/adding-trailing-permalinks/



                  For any further questions consult the codex:



                  https://codex.wordpress.org/wp_rewrite



                  https://codex.wordpress.org/Using_Permalinks






                  share|improve this answer













                  There are two good solutions for this:



                  The WP_Rewrite class has a var named $use_trailing_slashes that is set dynamically based upon whether or not your custom permalink structure ends in a '/'.



                  $this->use_trailing_slashes = ( '/' == substr($this->permalink_structure, -1, 1) );


                  This means that all WP generated links (the_permalink, category_link, the_permalink_rss, etc.) will not end in a '/'. So for category pages WP will show '/category/category' instead of '/category/category/'.



                  You can solve it by either using a filter or modifying your .htaccess or both:



                  Sample user_trailingslashit Filter



                  The user_trailingslashit function applies the 'user_trailingslashit' filter to the result prior to returning it. It provides the url and the type of url to the filter.



                  $string = apply_filters('user_trailingslashit', $string, $type_of_url);


                  So to hook into this and add a trailing slash to all urls other than single posts add this code to a plugin file or your functions.php theme file.



                  function fix_trailingsss($s='',$t='single')
                  {
                  if($t!='single')$s=rtrim($s,'/').'/';
                  return preg_replace('/^(.*)([^l/])$/i', '12/',$s);
                  }
                  add_filter('user_trailingslashit', 'fix_trailingsss', 9999,2);


                  Htaccess RedirectMatch



                  You can setup an .htaccess redirect to force category urls to always use a trailing slash like this:



                  RedirectMatch 301 ^/category/([^/]+)$ /category/$1/


                  Source of information: https://www.askapache.com/wordpress/adding-trailing-permalinks/



                  For any further questions consult the codex:



                  https://codex.wordpress.org/wp_rewrite



                  https://codex.wordpress.org/Using_Permalinks







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  BlackbamBlackbam

                  5,056124375




                  5,056124375

























                      0














                      I solved it like this:



                      function no_page_slash( $string, $type ){
                      if($type == 'single')
                      $string = untrailingslashit($string);
                      return $string;
                      }
                      add_filter('user_trailingslashit', 'no_page_slash', 70, 2);


                      Your permalinks must be set on like /%category%/%postname%/ .






                      share|improve this answer




























                        0














                        I solved it like this:



                        function no_page_slash( $string, $type ){
                        if($type == 'single')
                        $string = untrailingslashit($string);
                        return $string;
                        }
                        add_filter('user_trailingslashit', 'no_page_slash', 70, 2);


                        Your permalinks must be set on like /%category%/%postname%/ .






                        share|improve this answer


























                          0












                          0








                          0







                          I solved it like this:



                          function no_page_slash( $string, $type ){
                          if($type == 'single')
                          $string = untrailingslashit($string);
                          return $string;
                          }
                          add_filter('user_trailingslashit', 'no_page_slash', 70, 2);


                          Your permalinks must be set on like /%category%/%postname%/ .






                          share|improve this answer













                          I solved it like this:



                          function no_page_slash( $string, $type ){
                          if($type == 'single')
                          $string = untrailingslashit($string);
                          return $string;
                          }
                          add_filter('user_trailingslashit', 'no_page_slash', 70, 2);


                          Your permalinks must be set on like /%category%/%postname%/ .







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered yesterday









                          Samat ZhanbekovSamat Zhanbekov

                          263416




                          263416






























                              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%2f54240232%2fhow-to-add-a-slash-at-the-end-of-categorys-url-and-remove-it-at-the-end-of-post%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

                              Homophylophilia

                              Updating UILabel text programmatically using a function

                              Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage