Replacing space from special urls using htaccess










1














I'm able to replace all %20 with - from url by using :



.htaccess :



RewriteRule "^(S*)s+(S*)$" /$1-$2 [L,NE,R=302]
RewriteRule "^(S*)s+(S*s+.*)$" $1-$2 [L]
# remove multiple hyphens
RewriteRule ^(.*)-2,(.*)$ /$1-$2 [L,R=302]


Now i want to do this work for only desired urls Not all , url's like like :



Example.com/blog/example%title => example.com/blog/example-title



Example.com/product/example%product => example.com/product/example-product



How i can do this using .htaccess ?



EDIT :



Main Problem is that when i upload images to server with % in name like image 1.jpg url will redirect me to a name like image-1.jpg and server can't find image to show



EDIT 2



my .htaccess have this codes beforehand:



RewriteEngine On
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %HTTPS off
RewriteRule .* https://%HTTP_HOST%REQUEST_URI [L,R=301]









share|improve this question




























    1














    I'm able to replace all %20 with - from url by using :



    .htaccess :



    RewriteRule "^(S*)s+(S*)$" /$1-$2 [L,NE,R=302]
    RewriteRule "^(S*)s+(S*s+.*)$" $1-$2 [L]
    # remove multiple hyphens
    RewriteRule ^(.*)-2,(.*)$ /$1-$2 [L,R=302]


    Now i want to do this work for only desired urls Not all , url's like like :



    Example.com/blog/example%title => example.com/blog/example-title



    Example.com/product/example%product => example.com/product/example-product



    How i can do this using .htaccess ?



    EDIT :



    Main Problem is that when i upload images to server with % in name like image 1.jpg url will redirect me to a name like image-1.jpg and server can't find image to show



    EDIT 2



    my .htaccess have this codes beforehand:



    RewriteEngine On
    RewriteCond %REQUEST_FILENAME !-f
    RewriteCond %REQUEST_FILENAME !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    RewriteCond %HTTPS off
    RewriteRule .* https://%HTTP_HOST%REQUEST_URI [L,R=301]









    share|improve this question


























      1












      1








      1







      I'm able to replace all %20 with - from url by using :



      .htaccess :



      RewriteRule "^(S*)s+(S*)$" /$1-$2 [L,NE,R=302]
      RewriteRule "^(S*)s+(S*s+.*)$" $1-$2 [L]
      # remove multiple hyphens
      RewriteRule ^(.*)-2,(.*)$ /$1-$2 [L,R=302]


      Now i want to do this work for only desired urls Not all , url's like like :



      Example.com/blog/example%title => example.com/blog/example-title



      Example.com/product/example%product => example.com/product/example-product



      How i can do this using .htaccess ?



      EDIT :



      Main Problem is that when i upload images to server with % in name like image 1.jpg url will redirect me to a name like image-1.jpg and server can't find image to show



      EDIT 2



      my .htaccess have this codes beforehand:



      RewriteEngine On
      RewriteCond %REQUEST_FILENAME !-f
      RewriteCond %REQUEST_FILENAME !-d
      RewriteRule ^(.*)$ index.php/$1 [L]
      RewriteCond %HTTPS off
      RewriteRule .* https://%HTTP_HOST%REQUEST_URI [L,R=301]









      share|improve this question















      I'm able to replace all %20 with - from url by using :



      .htaccess :



      RewriteRule "^(S*)s+(S*)$" /$1-$2 [L,NE,R=302]
      RewriteRule "^(S*)s+(S*s+.*)$" $1-$2 [L]
      # remove multiple hyphens
      RewriteRule ^(.*)-2,(.*)$ /$1-$2 [L,R=302]


      Now i want to do this work for only desired urls Not all , url's like like :



      Example.com/blog/example%title => example.com/blog/example-title



      Example.com/product/example%product => example.com/product/example-product



      How i can do this using .htaccess ?



      EDIT :



      Main Problem is that when i upload images to server with % in name like image 1.jpg url will redirect me to a name like image-1.jpg and server can't find image to show



      EDIT 2



      my .htaccess have this codes beforehand:



      RewriteEngine On
      RewriteCond %REQUEST_FILENAME !-f
      RewriteCond %REQUEST_FILENAME !-d
      RewriteRule ^(.*)$ index.php/$1 [L]
      RewriteCond %HTTPS off
      RewriteRule .* https://%HTTP_HOST%REQUEST_URI [L,R=301]






      .htaccess mod-rewrite






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 18:44

























      asked Nov 12 '18 at 16:13









      SAM NZD

      69811228




      69811228






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can place known prefixes in your rules:



          RewriteRule "^((?:blog|product)/S*)s+(S*)$" /$1-$2 [L,NE,R=302,NC]

          RewriteRule "^((?:blog|product)S*)s+(S*s+.*)$" $1-$2 [L,NC]

          # remove multiple hyphens
          RewriteRule ^((?:blog|product)/.*)-2,(.*)$ /$1-$2 [L,R=302,NE,NC]





          share|improve this answer




















          • Not worked ! i tested for this url : site.com/blog/one/64/how%to%relax . i want to be /blog/one/64/how-to-relax .
            – SAM NZD
            Nov 12 '18 at 18:30











          • Space character is %20 not %. I have tested with http://localhost/blog/one/64/how%20to%20relax that got correctly redirected to http://localhost/blog/one/64/how-to-relax
            – anubhava
            Nov 12 '18 at 18:38







          • 1




            Finally i moved your codes top of everything i had beforehand in .htaccess and it worked ! i did not know reason
            – SAM NZD
            Nov 12 '18 at 19:03






          • 1




            Yes indeed all redirect rules should be placed before internal rewrite rules because RewriteRule ^(.*)$ index.php/$1 [L] changes all URIs to index.php/...
            – anubhava
            Nov 12 '18 at 19:12






          • 1




            thanks you are wizard
            – SAM NZD
            Nov 12 '18 at 19:54










          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%2f53266065%2freplacing-space-from-special-urls-using-htaccess%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









          1














          You can place known prefixes in your rules:



          RewriteRule "^((?:blog|product)/S*)s+(S*)$" /$1-$2 [L,NE,R=302,NC]

          RewriteRule "^((?:blog|product)S*)s+(S*s+.*)$" $1-$2 [L,NC]

          # remove multiple hyphens
          RewriteRule ^((?:blog|product)/.*)-2,(.*)$ /$1-$2 [L,R=302,NE,NC]





          share|improve this answer




















          • Not worked ! i tested for this url : site.com/blog/one/64/how%to%relax . i want to be /blog/one/64/how-to-relax .
            – SAM NZD
            Nov 12 '18 at 18:30











          • Space character is %20 not %. I have tested with http://localhost/blog/one/64/how%20to%20relax that got correctly redirected to http://localhost/blog/one/64/how-to-relax
            – anubhava
            Nov 12 '18 at 18:38







          • 1




            Finally i moved your codes top of everything i had beforehand in .htaccess and it worked ! i did not know reason
            – SAM NZD
            Nov 12 '18 at 19:03






          • 1




            Yes indeed all redirect rules should be placed before internal rewrite rules because RewriteRule ^(.*)$ index.php/$1 [L] changes all URIs to index.php/...
            – anubhava
            Nov 12 '18 at 19:12






          • 1




            thanks you are wizard
            – SAM NZD
            Nov 12 '18 at 19:54















          1














          You can place known prefixes in your rules:



          RewriteRule "^((?:blog|product)/S*)s+(S*)$" /$1-$2 [L,NE,R=302,NC]

          RewriteRule "^((?:blog|product)S*)s+(S*s+.*)$" $1-$2 [L,NC]

          # remove multiple hyphens
          RewriteRule ^((?:blog|product)/.*)-2,(.*)$ /$1-$2 [L,R=302,NE,NC]





          share|improve this answer




















          • Not worked ! i tested for this url : site.com/blog/one/64/how%to%relax . i want to be /blog/one/64/how-to-relax .
            – SAM NZD
            Nov 12 '18 at 18:30











          • Space character is %20 not %. I have tested with http://localhost/blog/one/64/how%20to%20relax that got correctly redirected to http://localhost/blog/one/64/how-to-relax
            – anubhava
            Nov 12 '18 at 18:38







          • 1




            Finally i moved your codes top of everything i had beforehand in .htaccess and it worked ! i did not know reason
            – SAM NZD
            Nov 12 '18 at 19:03






          • 1




            Yes indeed all redirect rules should be placed before internal rewrite rules because RewriteRule ^(.*)$ index.php/$1 [L] changes all URIs to index.php/...
            – anubhava
            Nov 12 '18 at 19:12






          • 1




            thanks you are wizard
            – SAM NZD
            Nov 12 '18 at 19:54













          1












          1








          1






          You can place known prefixes in your rules:



          RewriteRule "^((?:blog|product)/S*)s+(S*)$" /$1-$2 [L,NE,R=302,NC]

          RewriteRule "^((?:blog|product)S*)s+(S*s+.*)$" $1-$2 [L,NC]

          # remove multiple hyphens
          RewriteRule ^((?:blog|product)/.*)-2,(.*)$ /$1-$2 [L,R=302,NE,NC]





          share|improve this answer












          You can place known prefixes in your rules:



          RewriteRule "^((?:blog|product)/S*)s+(S*)$" /$1-$2 [L,NE,R=302,NC]

          RewriteRule "^((?:blog|product)S*)s+(S*s+.*)$" $1-$2 [L,NC]

          # remove multiple hyphens
          RewriteRule ^((?:blog|product)/.*)-2,(.*)$ /$1-$2 [L,R=302,NE,NC]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 16:49









          anubhava

          520k46316390




          520k46316390











          • Not worked ! i tested for this url : site.com/blog/one/64/how%to%relax . i want to be /blog/one/64/how-to-relax .
            – SAM NZD
            Nov 12 '18 at 18:30











          • Space character is %20 not %. I have tested with http://localhost/blog/one/64/how%20to%20relax that got correctly redirected to http://localhost/blog/one/64/how-to-relax
            – anubhava
            Nov 12 '18 at 18:38







          • 1




            Finally i moved your codes top of everything i had beforehand in .htaccess and it worked ! i did not know reason
            – SAM NZD
            Nov 12 '18 at 19:03






          • 1




            Yes indeed all redirect rules should be placed before internal rewrite rules because RewriteRule ^(.*)$ index.php/$1 [L] changes all URIs to index.php/...
            – anubhava
            Nov 12 '18 at 19:12






          • 1




            thanks you are wizard
            – SAM NZD
            Nov 12 '18 at 19:54
















          • Not worked ! i tested for this url : site.com/blog/one/64/how%to%relax . i want to be /blog/one/64/how-to-relax .
            – SAM NZD
            Nov 12 '18 at 18:30











          • Space character is %20 not %. I have tested with http://localhost/blog/one/64/how%20to%20relax that got correctly redirected to http://localhost/blog/one/64/how-to-relax
            – anubhava
            Nov 12 '18 at 18:38







          • 1




            Finally i moved your codes top of everything i had beforehand in .htaccess and it worked ! i did not know reason
            – SAM NZD
            Nov 12 '18 at 19:03






          • 1




            Yes indeed all redirect rules should be placed before internal rewrite rules because RewriteRule ^(.*)$ index.php/$1 [L] changes all URIs to index.php/...
            – anubhava
            Nov 12 '18 at 19:12






          • 1




            thanks you are wizard
            – SAM NZD
            Nov 12 '18 at 19:54















          Not worked ! i tested for this url : site.com/blog/one/64/how%to%relax . i want to be /blog/one/64/how-to-relax .
          – SAM NZD
          Nov 12 '18 at 18:30





          Not worked ! i tested for this url : site.com/blog/one/64/how%to%relax . i want to be /blog/one/64/how-to-relax .
          – SAM NZD
          Nov 12 '18 at 18:30













          Space character is %20 not %. I have tested with http://localhost/blog/one/64/how%20to%20relax that got correctly redirected to http://localhost/blog/one/64/how-to-relax
          – anubhava
          Nov 12 '18 at 18:38





          Space character is %20 not %. I have tested with http://localhost/blog/one/64/how%20to%20relax that got correctly redirected to http://localhost/blog/one/64/how-to-relax
          – anubhava
          Nov 12 '18 at 18:38





          1




          1




          Finally i moved your codes top of everything i had beforehand in .htaccess and it worked ! i did not know reason
          – SAM NZD
          Nov 12 '18 at 19:03




          Finally i moved your codes top of everything i had beforehand in .htaccess and it worked ! i did not know reason
          – SAM NZD
          Nov 12 '18 at 19:03




          1




          1




          Yes indeed all redirect rules should be placed before internal rewrite rules because RewriteRule ^(.*)$ index.php/$1 [L] changes all URIs to index.php/...
          – anubhava
          Nov 12 '18 at 19:12




          Yes indeed all redirect rules should be placed before internal rewrite rules because RewriteRule ^(.*)$ index.php/$1 [L] changes all URIs to index.php/...
          – anubhava
          Nov 12 '18 at 19:12




          1




          1




          thanks you are wizard
          – SAM NZD
          Nov 12 '18 at 19:54




          thanks you are wizard
          – SAM NZD
          Nov 12 '18 at 19:54

















          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%2f53266065%2freplacing-space-from-special-urls-using-htaccess%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto