Apache: Realative Link Problem using Rewrite Engine (mod_rewrite)

Using the Apache Rewrite Engine there might occour a problem with relative links if you are e.g. are jumping between directories or if you are using the the Rewrite Engine for an internal proxy. In the follwing example realtive links in the page will not work properly.

  <Directory "/documentroot/de">
  RewriteEngine on
  RewriteBase    /de/
  RewriteRule    ^/?  http://localhost/?language=de  [P]

There is a very easy solution to avoid this problem. You can define a base url tag in the header section of your HTML pages.

In the example above you have to include something like follows to the header section of all pages that do not work properly:

   <base href="http://localhost/">

That’s ist!

Wt Web Toolkit: Get URL parameter string values

If you want to access an url parameter string in Witty C++ HTML Web Toolkit there is an easy way to do so. Imagine you want to get the content of the parameter “param” in a URL like http://www.myurl.com/?param=test. To do so you can use the following code in your Wt Application class:

#include &lt;Wt/WApplication&gt;
#include &lt;Wt/WEnvironment&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;

using namespace Wt;

class myApplication: public WApplication
  myApplication(const WEnvironment&amp; env)
    : WApplication(env)
    setTitle(&quot;This is an application&quot;);

    addMetaHeader (&quot;description&quot;,&quot;a description&quot;);

    const std::string* param =    this-&gt;environment().getParameter(&quot;param&quot;); 

    if (param!=0)
        std::cout &lt;&lt; (*param) &lt;&lt; std::endl;



Also have a look of the handling of internal paths in Wt Web Toolkit:


Apache: Convert URL parameter strings to directories for search engine friendly URLS

Imagine you have a URL like http://www.whatever.com/?language=en which looks pretty ugly. For Search Engine Optimization it would be nice to have a more search engine friendly URL like http://www.whatever.com/en.

Apache offers an easy way to hide the parameter string by the second URL format. However, internally the page still is treated like the first version of the URL but users and search engines will not notice the old URL format.

To implement the example from above you simply have to add the following lines to your httpd.conf .

In this example the directory en is forwarded internally to page.php?language=en. If the user enters http://www.whatever.com/en he will see the content of http://www.whatever.com/page.php?language=en without noticing the parameter string.

<Directory "/documentroot/en">

  Options Indexes FollowSymLinks

  AllowOverride All

   Order allow,deny
   Allow from all

  Options +FollowSymlinks
  RewriteEngine on

  RewriteCond %{REQUEST_URI} =/en/

  RewriteRule   ^/? page.php\?language\=en [PT]


In the line /documentroot/en you define the directory for which you want to apply the hack. It has to be the internal location at your server.

The line “RewriteCond %{REQUEST_URI} =/en/” tells Apache to apply the hack only if http://www.whatever.com/en is requested. Other URLs in the directory like http://www.whatever.com/en/anypage.html will not be touched.

How to use Apache as a proxy server

The following configuration allows you to make Apache forward the request for your domain to a second domain at port 8081. If the user enters “www.domain.com” in his browser he well see the content provided by the server listening at http://www.otherdomain.com:8081/. 

<VirtualHost *:80>
    ServerName www.testdomain.com
    ServerAlias testdomain.com *.testdomain.com
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    ProxyPass / http://www.testdomain.com:8081/
    ProxyPassReverse / http://www.testdomain.com:8081/
    <Location />
        Order allow,deny
        Allow from all