How to Enable User-Friendly URLs for Websites Through Apache HTTPD Server

Enable user-friendly URLs for websites through Apache HTTPD Server.

Albin Issac
Nov 6 · 4 min read

A user-friendly URL is a Web address that is easy to read and includes words that describe the content of the webpage.

Defining the user-friendly URL is the best practice also the recommendation for SEO, the user-friendly URL’s helps to hide the complex internal URL’s from the end-users and search engines. These URLs help visitors to remember web addresses, which they can easily type to access the page.

For example, the long URL /test/en/pdp/book.html?id=123 should be changed to the user-friendly URL /en/book/123

The Apache URL Rewriting (mod_rewrite) along with PT(PassThrough) flag helps to define the user-friendly URL’s in Apache by hiding the complex and lengthy URL’s from end-users.

Image for post
Image for post

URL Rewriting enables Web administrators to create powerful rules to implement URLs that are easier for users to remember and easier for search engines to find.

The mod_rewrite module provides a way to modify incoming URL requests, dynamically, based on regular expression rules. This allows you to map arbitrary URLs onto your internal URL structure in any way you like.

The URL manipulations can depend on various tests: server variables, environment variables, HTTP headers, time stamps, external database lookups, and various other external programs or handlers, which can be used to achieve granular URL matching.

Rewrite rules can operate on the full URLs, including the path-info and query string portions, the rewritten result can lead to further rules, internal sub-processing, external request redirection, or proxy passthrough, depending on what flags you attach to the rules.

The target (or substitution string) in a RewriteRule is assumed to be a file path, by default. The use of the [PT] flag causes it to be treated as a URI instead. That is to say, the use of the [PT] flag causes the result of the RewriteRule to be passed back through URL mapping, so that location-based mappings, such as Alias, Redirect, or ScriptAlias

The [PT] flag implies the [L] flag, the rewriting will be stopped in order to pass the request to the next phase of processing. The modified URI is sent to the next module for processing but the actual is not modified in the browser, the end-user access the friendly URL but the Rewrite rule along with the PT flag internally rewrites to the actual backend URL.

Image for post
Image for post

For the demo, I am integrating the Apache server with Tomcat to enable access to content through myexample.com (can be integrated with any other backend servers to the server the content), Apache server internally directs all the traffic to Tomcat to fetch the required content.

Add the below configurations for enabling the integrations

Enable the mod_rewrite, mod_jk modules in httpd.conf — the mod_jk module can be downloaded from https://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/windows/

LoadModule rewrite_module modules/mod_rewrite.so
LoadModule jk_module modules/mod_jk.so

Create workers.properties file with below configurations(change the ajp port number based on your tomcat server configurations)

worker.list=worker1,jkstatus#Set properties for worker19 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.ping_timeout=1000
worker.worker1.connect_timeout=10000
worker.worker1.prepost_timeout=10000
worker.worker1.socket_timeout=10
worker.worker1.connection_pool_timeout=60
worker.worker1.connection_pool_size=90
worker.worker1.retries=2
worker.worker1.reply_timeout=300000
# status worker
worker.jkstatus.type=status

Enable the below configuration in httpd.conf

JkWorkersFile conf/workers.properties
JkShmFile logs/mod_jk.shm
JkLogFile logs/mod_jk.log
JkLogLevel info

Enable the below configurations in the virtual host file

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "c:/Apache24/docs/myexample.com"
ServerName myexample.com
ErrorLog "logs/myexample.com-error.log"
CustomLog "logs/myexample.com-access.log" common

JkMount /* worker1

</VirtualHost>

Now the content from the Tomcat server is accessible through myexample.com e.g http://myexample.com/test1/index.html (test1 is context root)

Image for post
Image for post

The URL is not userfriendly, I want to change to a user-friendly URL e.g. http://myexample.com/en/index.html

Let me now enable the required RewriteRules along with PT flags to the virtual host.

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "c:/Apache24/docs/myexample.com"
ServerName myexample.com
ErrorLog "logs/myexample.com-error.log"
CustomLog "logs/myexample.com-access.log" common

JkMount /* worker1


RewriteEngine on
RewriteRule ^/en/(.*).html /test1/$1.html [PT]
</VirtualHost>

Here I am mapping any incoming request to /en/<page name>.html to the back end specific internal URL /test1/<pagename>.html

Now the user can access myexample.com/en/index.html, the request will be internally rewritten to /test/index.html to the backend tomcat server, the user always sees /en/index.html in the browser.

Image for post
Image for post

Now both user-friendly URL and internal URLs are accessible, if required you can enable the redirect from the internal URL to the user-friendly URL.

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "c:/Apache24/docs/myexample.com"
ServerName myexample.com
ErrorLog "logs/myexample.com-error.log"
CustomLog "logs/myexample.com-access.log" common

JkMount /* worker1


RewriteEngine on
RewriteRule ^/en/(.*).html /test1/$1.html [PT]

RewriteRule ^/test1/(.*).html /en/$1.html [R=301,L]
</VirtualHost>

Even you should be able to configure complex URLs like product URLs e.g.

The internal product URL is — /en/test/pdp/book.html?id=123, this URL is not SEO/User friendly as the title of the product is not part of the URL

The URL should be rewritten to /en/test/Sample_Book/book/pdp/123 internally without changing the browser URL

RewriteRule ^/en/test/(.*)/(.*)/pdp/(.*)$ /en/test/pdp/$2.html?id=$3 [PT,L]

URL rewriting along with the PT flag is a relatively quick and easy way to improve your website’s URL structure to end users and search engines, the RewriteRules along with the PT flag enables user-friendly URL’s and internally rewrite the incoming URL’s to the internal backend URL.

Medium's largest active publication, followed by +734K people. Follow to join our community.

Albin Issac

Written by

Working as a Software Architect on Marketing Technologies. Reach out to me on Linkedin: https://www.linkedin.com/in/albin-issac-56917523/

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Albin Issac

Written by

Working as a Software Architect on Marketing Technologies. Reach out to me on Linkedin: https://www.linkedin.com/in/albin-issac-56917523/

The Startup

Medium's largest active publication, followed by +734K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store