apache rewriterule: rewrite url to hash

After building a new website for a client, i needed to redirect their old URLs to new ones, normally a trivial task. But in this case the new site is driven by hash urls, so we need to rewrite url to hash url, such as: from: /apage to domain.co.uk/#!/area/section/bit and variations on that. By default Apache will encode any characters such as hashes (#) and percentages (%), so i needed to tell Apache not to do this…

No Escape

To achieve this, we need to make changes to the main httpd.conf file, mine is located in /etc/httpd/conf/httpd.conf

The line we need to add is:

RewriteMap escape int:escape

…and then restart Apache, after first checking the syntax:

/etc/init.d/httpd configtest

You should see:

Syntax OK
/etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

The RewriteRule the need

We need to add the NE flag to our rewrite rules too:

RewriteRule ^wills$ /#!/private-client/life-planning/wills [NE,R]

We can now redirect old URLs to these shiny new hash ones (whether hash URLs are good or bad is a different matter!)

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *