0

I have create small webpage, I need change original URL for example now I redirected http://localhost/single-pages/?id=10 working fine, But I want to change URL structure of http://localhost/single-pages/10. How do I hide or mask query string value of "?id=". Could you please help me any one.

<html>
   <head>
      <title></title>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script>
      
   </head>
   <body>
    <div class="sampleLink">Link here</div>
   </div> 
   
   <script type="text/javascript">
     $(document).ready(function(){

        var link="http://localhost/single-pages/?id=10";
        $('.sampleLink').append("<a href="+link+">New Link Here</a>");
        console.log(window.location);
        
     });
   </script> 
   </body>
</html>
siva
  • 69
  • 1
  • 1
  • 4

1 Answers1

0

You could replace the ?id=10 by

const locationSearch = window.location.search;
const locationWithoutSearch = window.location.replace(locationSearch, '');

Now you can append the parameter of the search like

const params = new URLSearchParams(locationSearch);
const pageId = params.get('id');

And you have different possibilities to set the URL, one is (without reloading)

window.history.pushState({}, '', `/single-pages/${pageId}`);
pacman
  • 156
  • 1
  • 4