How to remove characters from a redirected URL in .htaccess?











up vote
0
down vote

favorite












I recently noticed a minor problem in a .htaccess redirect that I can't find a fix for.
I'm redirecting thousands of links of the form



"example.com/tinc?key={mass of random characters, etc}"



and repointing them at:



"example.com/search.php"



The following .htaccess command works fine:



"RedirectMatch 301 ^/tinc.*$ example.com/search.php"


...except that I just realised what it actually does is send the user to



example.com/search.php?key={mass of random characters, etc}



I thought that the .htaccess script directed the user to the new specific URL, but it's actually just replacing the first part of the original URL with the new one.



Because search.php just responds with the valid response that it can't find a match to the string, it's not disastrous, but it's not pretty in the address bar and I'd like it to work properly just for my own satisfaction.



So - can anyone advise how redirect in a way that loses the random string of characters at end, please? Thanks!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I recently noticed a minor problem in a .htaccess redirect that I can't find a fix for.
    I'm redirecting thousands of links of the form



    "example.com/tinc?key={mass of random characters, etc}"



    and repointing them at:



    "example.com/search.php"



    The following .htaccess command works fine:



    "RedirectMatch 301 ^/tinc.*$ example.com/search.php"


    ...except that I just realised what it actually does is send the user to



    example.com/search.php?key={mass of random characters, etc}



    I thought that the .htaccess script directed the user to the new specific URL, but it's actually just replacing the first part of the original URL with the new one.



    Because search.php just responds with the valid response that it can't find a match to the string, it's not disastrous, but it's not pretty in the address bar and I'd like it to work properly just for my own satisfaction.



    So - can anyone advise how redirect in a way that loses the random string of characters at end, please? Thanks!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I recently noticed a minor problem in a .htaccess redirect that I can't find a fix for.
      I'm redirecting thousands of links of the form



      "example.com/tinc?key={mass of random characters, etc}"



      and repointing them at:



      "example.com/search.php"



      The following .htaccess command works fine:



      "RedirectMatch 301 ^/tinc.*$ example.com/search.php"


      ...except that I just realised what it actually does is send the user to



      example.com/search.php?key={mass of random characters, etc}



      I thought that the .htaccess script directed the user to the new specific URL, but it's actually just replacing the first part of the original URL with the new one.



      Because search.php just responds with the valid response that it can't find a match to the string, it's not disastrous, but it's not pretty in the address bar and I'd like it to work properly just for my own satisfaction.



      So - can anyone advise how redirect in a way that loses the random string of characters at end, please? Thanks!










      share|improve this question















      I recently noticed a minor problem in a .htaccess redirect that I can't find a fix for.
      I'm redirecting thousands of links of the form



      "example.com/tinc?key={mass of random characters, etc}"



      and repointing them at:



      "example.com/search.php"



      The following .htaccess command works fine:



      "RedirectMatch 301 ^/tinc.*$ example.com/search.php"


      ...except that I just realised what it actually does is send the user to



      example.com/search.php?key={mass of random characters, etc}



      I thought that the .htaccess script directed the user to the new specific URL, but it's actually just replacing the first part of the original URL with the new one.



      Because search.php just responds with the valid response that it can't find a match to the string, it's not disastrous, but it's not pretty in the address bar and I'd like it to work properly just for my own satisfaction.



      So - can anyone advise how redirect in a way that loses the random string of characters at end, please? Thanks!







      regex .htaccess redirect






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 11:55

























      asked Nov 10 at 22:51









      Gavin W

      32




      32
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I can't see where the problem is, but being more explicit, and defining multiple rules might help:



          RedirectMatch 301 ^example.com/tinc$ example.com/search.php
          RedirectMatch 301 ^example.com/tinc?key=.*$ example.com/search.php


          If that doesn't work, then assuming that your only GET parameter is key, you could try this RewriteCond.



          RewriteCond %{QUERY_STRING} ^key=.*$
          RewriteRule ^example.com/tinc$ example.com/search.php [L,R=301]


          Good luck!






          share|improve this answer





















          • Thanks Addison, much appreciated, and at least I don't feel like I've missed something obvious now! Unfortunately, they result in the same problem; the string of characters persists in the form .../search.php?={original string}. As I say, it's as if the rules are simply resulting in replacement of everything up to the searched text, and leaving everything else in place. Thanks anyway!
            – Gavin W
            Nov 14 at 17:40












          • Do you think maybe something is happening to the URL after the replacement occurs? It's not normal that this is happening.
            – Addison
            Nov 14 at 23:30






          • 1




            I don't really know - I tried different strings and had the same result....until just now. I made up a link that should have failed and found that it now works as intended. The problem seems to lie in not trapping the "?" symbol. If I try "miramodus.com/tinc_add_any_string" it works as intended, if I try "miramodus.com/tinc?_add_any_string" it includes _add_any_string to the URL. Not clear why it does this, but trapping the ? mark in .htaccess should fix things. Many thanks for your help - you've inadvertently solved this for me!
            – Gavin W
            Nov 15 at 10:55













          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',
          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%2f53244200%2fhow-to-remove-characters-from-a-redirected-url-in-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








          up vote
          0
          down vote













          I can't see where the problem is, but being more explicit, and defining multiple rules might help:



          RedirectMatch 301 ^example.com/tinc$ example.com/search.php
          RedirectMatch 301 ^example.com/tinc?key=.*$ example.com/search.php


          If that doesn't work, then assuming that your only GET parameter is key, you could try this RewriteCond.



          RewriteCond %{QUERY_STRING} ^key=.*$
          RewriteRule ^example.com/tinc$ example.com/search.php [L,R=301]


          Good luck!






          share|improve this answer





















          • Thanks Addison, much appreciated, and at least I don't feel like I've missed something obvious now! Unfortunately, they result in the same problem; the string of characters persists in the form .../search.php?={original string}. As I say, it's as if the rules are simply resulting in replacement of everything up to the searched text, and leaving everything else in place. Thanks anyway!
            – Gavin W
            Nov 14 at 17:40












          • Do you think maybe something is happening to the URL after the replacement occurs? It's not normal that this is happening.
            – Addison
            Nov 14 at 23:30






          • 1




            I don't really know - I tried different strings and had the same result....until just now. I made up a link that should have failed and found that it now works as intended. The problem seems to lie in not trapping the "?" symbol. If I try "miramodus.com/tinc_add_any_string" it works as intended, if I try "miramodus.com/tinc?_add_any_string" it includes _add_any_string to the URL. Not clear why it does this, but trapping the ? mark in .htaccess should fix things. Many thanks for your help - you've inadvertently solved this for me!
            – Gavin W
            Nov 15 at 10:55

















          up vote
          0
          down vote













          I can't see where the problem is, but being more explicit, and defining multiple rules might help:



          RedirectMatch 301 ^example.com/tinc$ example.com/search.php
          RedirectMatch 301 ^example.com/tinc?key=.*$ example.com/search.php


          If that doesn't work, then assuming that your only GET parameter is key, you could try this RewriteCond.



          RewriteCond %{QUERY_STRING} ^key=.*$
          RewriteRule ^example.com/tinc$ example.com/search.php [L,R=301]


          Good luck!






          share|improve this answer





















          • Thanks Addison, much appreciated, and at least I don't feel like I've missed something obvious now! Unfortunately, they result in the same problem; the string of characters persists in the form .../search.php?={original string}. As I say, it's as if the rules are simply resulting in replacement of everything up to the searched text, and leaving everything else in place. Thanks anyway!
            – Gavin W
            Nov 14 at 17:40












          • Do you think maybe something is happening to the URL after the replacement occurs? It's not normal that this is happening.
            – Addison
            Nov 14 at 23:30






          • 1




            I don't really know - I tried different strings and had the same result....until just now. I made up a link that should have failed and found that it now works as intended. The problem seems to lie in not trapping the "?" symbol. If I try "miramodus.com/tinc_add_any_string" it works as intended, if I try "miramodus.com/tinc?_add_any_string" it includes _add_any_string to the URL. Not clear why it does this, but trapping the ? mark in .htaccess should fix things. Many thanks for your help - you've inadvertently solved this for me!
            – Gavin W
            Nov 15 at 10:55















          up vote
          0
          down vote










          up vote
          0
          down vote









          I can't see where the problem is, but being more explicit, and defining multiple rules might help:



          RedirectMatch 301 ^example.com/tinc$ example.com/search.php
          RedirectMatch 301 ^example.com/tinc?key=.*$ example.com/search.php


          If that doesn't work, then assuming that your only GET parameter is key, you could try this RewriteCond.



          RewriteCond %{QUERY_STRING} ^key=.*$
          RewriteRule ^example.com/tinc$ example.com/search.php [L,R=301]


          Good luck!






          share|improve this answer












          I can't see where the problem is, but being more explicit, and defining multiple rules might help:



          RedirectMatch 301 ^example.com/tinc$ example.com/search.php
          RedirectMatch 301 ^example.com/tinc?key=.*$ example.com/search.php


          If that doesn't work, then assuming that your only GET parameter is key, you could try this RewriteCond.



          RewriteCond %{QUERY_STRING} ^key=.*$
          RewriteRule ^example.com/tinc$ example.com/search.php [L,R=301]


          Good luck!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 at 2:06









          Addison

          1,4761734




          1,4761734












          • Thanks Addison, much appreciated, and at least I don't feel like I've missed something obvious now! Unfortunately, they result in the same problem; the string of characters persists in the form .../search.php?={original string}. As I say, it's as if the rules are simply resulting in replacement of everything up to the searched text, and leaving everything else in place. Thanks anyway!
            – Gavin W
            Nov 14 at 17:40












          • Do you think maybe something is happening to the URL after the replacement occurs? It's not normal that this is happening.
            – Addison
            Nov 14 at 23:30






          • 1




            I don't really know - I tried different strings and had the same result....until just now. I made up a link that should have failed and found that it now works as intended. The problem seems to lie in not trapping the "?" symbol. If I try "miramodus.com/tinc_add_any_string" it works as intended, if I try "miramodus.com/tinc?_add_any_string" it includes _add_any_string to the URL. Not clear why it does this, but trapping the ? mark in .htaccess should fix things. Many thanks for your help - you've inadvertently solved this for me!
            – Gavin W
            Nov 15 at 10:55




















          • Thanks Addison, much appreciated, and at least I don't feel like I've missed something obvious now! Unfortunately, they result in the same problem; the string of characters persists in the form .../search.php?={original string}. As I say, it's as if the rules are simply resulting in replacement of everything up to the searched text, and leaving everything else in place. Thanks anyway!
            – Gavin W
            Nov 14 at 17:40












          • Do you think maybe something is happening to the URL after the replacement occurs? It's not normal that this is happening.
            – Addison
            Nov 14 at 23:30






          • 1




            I don't really know - I tried different strings and had the same result....until just now. I made up a link that should have failed and found that it now works as intended. The problem seems to lie in not trapping the "?" symbol. If I try "miramodus.com/tinc_add_any_string" it works as intended, if I try "miramodus.com/tinc?_add_any_string" it includes _add_any_string to the URL. Not clear why it does this, but trapping the ? mark in .htaccess should fix things. Many thanks for your help - you've inadvertently solved this for me!
            – Gavin W
            Nov 15 at 10:55


















          Thanks Addison, much appreciated, and at least I don't feel like I've missed something obvious now! Unfortunately, they result in the same problem; the string of characters persists in the form .../search.php?={original string}. As I say, it's as if the rules are simply resulting in replacement of everything up to the searched text, and leaving everything else in place. Thanks anyway!
          – Gavin W
          Nov 14 at 17:40






          Thanks Addison, much appreciated, and at least I don't feel like I've missed something obvious now! Unfortunately, they result in the same problem; the string of characters persists in the form .../search.php?={original string}. As I say, it's as if the rules are simply resulting in replacement of everything up to the searched text, and leaving everything else in place. Thanks anyway!
          – Gavin W
          Nov 14 at 17:40














          Do you think maybe something is happening to the URL after the replacement occurs? It's not normal that this is happening.
          – Addison
          Nov 14 at 23:30




          Do you think maybe something is happening to the URL after the replacement occurs? It's not normal that this is happening.
          – Addison
          Nov 14 at 23:30




          1




          1




          I don't really know - I tried different strings and had the same result....until just now. I made up a link that should have failed and found that it now works as intended. The problem seems to lie in not trapping the "?" symbol. If I try "miramodus.com/tinc_add_any_string" it works as intended, if I try "miramodus.com/tinc?_add_any_string" it includes _add_any_string to the URL. Not clear why it does this, but trapping the ? mark in .htaccess should fix things. Many thanks for your help - you've inadvertently solved this for me!
          – Gavin W
          Nov 15 at 10:55






          I don't really know - I tried different strings and had the same result....until just now. I made up a link that should have failed and found that it now works as intended. The problem seems to lie in not trapping the "?" symbol. If I try "miramodus.com/tinc_add_any_string" it works as intended, if I try "miramodus.com/tinc?_add_any_string" it includes _add_any_string to the URL. Not clear why it does this, but trapping the ? mark in .htaccess should fix things. Many thanks for your help - you've inadvertently solved this for me!
          – Gavin W
          Nov 15 at 10:55




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244200%2fhow-to-remove-characters-from-a-redirected-url-in-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







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues