Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Drupal custom URL rewriting - Change the admin url to enhance security

DZone's Guide to

Drupal custom URL rewriting - Change the admin url to enhance security

· Web Dev Zone
Free Resource

Get the senior executive’s handbook of important trends, tips, and strategies to compete and win in the digital economy.

The following tip can be used in multiple scenarios (being anywhere you need custom URL rewriting and want to do this without .htaccess), but I'll illustrate it for two specific purposes.
  1. At our company all urls beginning with /admin are blocked from outside by a firewall for content security reasons. This sucks, because Drupal administration is done on pages with a /admin url. So we need to find a way to rewrite all of the urls to something like /config (or something else).
  2. If someone knows your site is on Drupal, this gives him some knowledge on how the site is structured. For example does he know that all administration is done on /admin . To make it harder to guess this url, we want to rename it.
Both of these cases can be tackled by one hook ( custom_url_rewrite) in Drupal that has to be specified in the default.php file. You can find a descent explanation of how this hook works in the Drupal API. In the following example I rewrite all admin urls to config (and vice versa).
function custom_url_rewrite($op, $result, $path) {

if ($op == 'alias') {
if (preg_match('|^admin(/{0,1}.*)|', $path, $matches)) {
return 'config'. $matches[1];
}
}

if ($op == 'source') {
if (preg_match('|^config(/{0,1}.*)|', $path, $matches)) {
return 'admin'. $matches[1];
}
}

return $result;

}

Read this guide to learn everything you need to know about RPA, and how it can help you manage and automate your processes.

Topics:

Published at DZone with permission of Davy Van Den Bremt. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}