customize url link javascript

Customize URLs with JavaScript: Redirect, Pre-pend, and More

By Nicole Hernandez
AD

Regular URLs often include parameters and session IDs, making them unreadable and non-shareable. To circumvent this, we employ a method using JavaScript to customize URL links, ensuring they're clean, shareable, and semantic. This technique, called Permalinking, is an essential SEO practice that improves user experience, boosts site visibility, and enhances click-through rates.

By customizing URLs, you can create readable, memorable, and keyword-rich links that accurately represent the content they lead to. This practice not only aids users in understanding the content but also signals to search engines what the page is about, thereby enhancing your site's SEO.

Understanding Custom URL Linking

Custom URL linking, or pretty URL generation, is the process of rewriting URLs to make them more user-friendly. This involves removing parameters and replacing them with text that describes the resource in a human-readable way. JavaScript plays a crucial role in this process, assisting in the reading and rewriting of URLs.

AD
an image of a computer screen with headphones on it and another screenshot of the web page
an image of a computer screen with headphones on it and another screenshot of the web page
Display the URL on a Print Page
Display the URL on a Print Page
Upload Image using JS 🤳| HTML CSS JavaScript
Upload Image using JS 🤳| HTML CSS JavaScript

Here's a simple example demonstrating the concept. Suppose you have a URL like this:

https://example.com/?id=12345¶m=test

Using JavaScript, you can transform it into something more readable like:

Internal vs external links (HTML basics) 👇
Internal vs external links (HTML basics) 👇
a person typing on a laptop with the text how to code a link in html
a person typing on a laptop with the text how to code a link in html
an animated electric border for the source code - borderer, with text below it
an animated electric border for the source code - borderer, with text below it
https://example.com/ products/12345-test

Setting Up HTML Forms for URL Customization

To start customizing URLs, you'll need to use HTML forms. Forms allow users to enter data, which you can then use to create custom URLs. Here's a simple form example:

<form id="custom-url-form" action="/submit">
  <input type="text" id="user-input">
  <input type="submit" value="Submit">
</form>

In this form, the user's input will be the basis for the customized URL.

AD

Using JavaScript to Customize URLs

After setting up your HTML form, you can use JavaScript to read the user's input and transform it into a custom URL. Here's a simple script to do that:

the back side of a computer screen with an image of a black and purple background
the back side of a computer screen with an image of a black and purple background
an animated image slider is shown with the text, and it appears to be being displayed
an animated image slider is shown with the text, and it appears to be being displayed
Input with Action Button - Code Snippet
Input with Action Button - Code Snippet
URL Expander: See Where a Short Link Goes
URL Expander: See Where a Short Link Goes
an image of a screen with text on it that says, simple like button animation
an image of a screen with text on it that says, simple like button animation
Create a digital clock through HTML & Javascript Amazing
Create a digital clock through HTML & Javascript Amazing
document.getElementById('custom-url-form').addEventListener('submit', function(e) {
  e.preventDefault();
  var input = document.getElementById('user-input').value;
  var url = '/products/' + input.replace(/ /g, '-'); // replaces spaces with hyphens
  window.location.href = url;
});

In this script, the user's input is transformed into a URL-friendly string (replacing spaces with hyphens) and then navigates to the new URL.

Implementing Custom URL Linking in Your Application

Implementing custom URL linking in your application involves several steps. First, you need to set up server-side routing to handle these URL requests. After that, you will need to retrieve the data based on the URL parameter and display it.

AD

Here's an example using Node.js and Express.js:

How to Get #Youtube Custom URL for #SEO | by Asad - Growth4EveryOne | हिन्दी | اردو
How to Get #Youtube Custom URL for #SEO | by Asad - Growth4EveryOne | हिन्दी | اردو
Create 360 View Image Rotation Slider Using HTML CSS and JavaScript
Create 360 View Image Rotation Slider Using HTML CSS and JavaScript
Javascript Loops & Iterations
Javascript Loops & Iterations
Interactive Submit Button Using HTML CSS and JavaScript
Interactive Submit Button Using HTML CSS and JavaScript
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
JavaScript Template Literals: Write Cleaner & Dynamic Strings 📝
🔗 HTML <link> Tag — Linking External Resources
🔗 HTML <link> Tag — Linking External Resources
a screen shot of a web page with the words'html input types '
a screen shot of a web page with the words'html input types '
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
Simple Javascript Tagging
Simple Javascript Tagging
JavaScript One-Liners
JavaScript One-Liners
CSS button animation
CSS button animation
an image of some type of webpage
an image of some type of webpage
change body background with js every second
change body background with js every second
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM

Setting Up Server-Side Routing

In your Express.js app, set up a route to handle the custom URLs:

app.get('/products/:productId', function(req, res) {
  // fetch product data based on req.params.productId and render it
});

Fetching and Displaying Data Based on URL Parameters

Within this route, fetch the product data based on the 'productId' parameter and display it in your view:

AD
Product.find({ where: { id: req.params.productId } })
  .then((product) => {
    res.render('product', { product: product.toJSON() })
  })
  .catch(() => {
    res.sendStatus(500)
  })

In yourProduct view, HTML, you can display the product information:

<h1>{{product.name}}</h1>
<p>{{product.description}}</p>

By following these steps, you can create dynamic, user-friendly URLs that not only improve user experience but also enhance your site's SEO.

Remember, customizing URL links with JavaScript is a powerful tool, but it's just one part of your SEO strategy. You must continue to create high-quality content, optimize your site speed, and build quality backlinks to see the full benefits.

So, why not start today and turn your sites' URLs into powerful, shareable assets? Happy coding!

AD