What does URL rewrite mean?

With a web-server rewrite, existing URLs, or URL structures, are rewritten by rules configured into a web server, without the visitor noticing much. It can be used to map internal architecture into a different client-facing architecture. The re-write process can include scripts, and the passing of an http status-code. For example, a 301 redirect.

What is a rewrite?

All web servers, whether Apache, Nginx, Microsoft’s IIS or others, have the ability to change URLs before they are delivered.

This usually happens because a requested document is located somewhere else, for example after a website relaunch, and the visitor should be taken to the new location instead.

In addition to these external rewrites, where a visitor requests a URL and the server checks whether certain redirects apply to the requested URL, there are also internal rewrites.

The server knows which document or resource is to be made available within a URL, regardless of where it is stored in the internal folder structure.

Let’s look at the WordPress content management system as an example. Each blog post is stored in a database and given an ID. The individual pages can always be called up via this ID.

This post, for example, can be accessed via the outward-facing URL – https://www.sistrix.de/frag-sistrix/rewrite/ – but also via https://www.sistrix.de/?p=67170.

If we were to change the CMS, this internal file structure would probably also change and ?p=67170 would either no longer be available or could deliver a different resource. To ensure that https://www.sistrix.de/frag-sistrix/rewrite/ can still be accessed, the internal rewrite system checks which document is actually to be served there.

Applications and examples of rewrites

Rewrites always come into play when the existing URL needs to be adapted or completely changed. Some examples are:

Change www-website to non-www, or vice versa

It is up to you to decide whether your website should start with www. or not. Once this decision has been made, only the desired version should be used.

To ensure this, a 301 redirect from the unwanted version to the preferred one needs to be set up.

Switch from HTTP to HTTPS

The pages of a website that are available via the HTTP protocol are considered as separate pages from those that are delivered via the secure HTTPS protocol.

To prevent the occurrence of duplicate content, pages should always be redirected to the secure version.

URL changeover during a relaunch

If the website is fundamentally changed, the URL structures are often also rebuilt.

In order to ensure that Google can pass on the trust previously gained through documents to the new storage location of the document without any problems, 301 redirects are also necessary here.

Mapping URL parameters into readable URLs

If your website is configured to deliver product pages via a URL parameter, or example:

https://www.topshop.com/products?p=124 

rewrite code could help to match and map these into readable URLs. using a rewrite, and a php script, the id can be used to extract product names, let’s say it’s a Laura Ashley summer dress, from a database and to construct a URL. For example:

https://www.topshop.com/laura-ashley/dress/summer

Remember that you need to consider whether Google may have already indexed the original URL with parameters and in this case a 301 redirect would be recommended. For new content, there’s no need to add a redirect.

Example of a rewrite in .htaccess

The rewrite function in the Apache we server is part of an optional module that must be enabled. The common way to trigger the re-write is to add some code to the .htaccess file. The following example calls a script. The script can be used to check for valid products and to redirect to other URLs (a ‘product not found page, for example) or generate HTML.


RewriteCond %{HTTP_HOST} ^www\.topshop\.com$
RewriteCond %{REQUEST_URI} ^/products.*$
RewriteCond %{QUERY_STRING} ^id=*
RewriteRule .* ./lookup_prods.php [L]

The second example is using a virtual URL path (that doesn’t exist on the server.) The rewrite code captures the call to the virtual path and calls php code that generates the correct HTML for that path.

For example, https://www.topshop.com/brands/laura-ashley/dress/11234 will be captured by the rewrite code below and php will be executed that will return the correct HTML.


RewriteCond %{REQUEST_URI} ^/brands.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* ./lookup_prods.php [L]

Conclusion on the topic of rewrite

Rewrites can be used to avoid duplicate content, redirect broken internal links and create search engine friendly URLs. Redirecting URLs via the Rewrite module offers the possibility to improve the usability and search-friendliness of your own website.

Steve Paine