I have my Drupal installation in a subfolder on the server, so all my links are of the form: http://example.com/drupal/admin/etc.
In Drupal, you'd normally change the $base_url variable in the settings.php file to reflect this and hide the subfolder from the URL, but because I'm also using Domain Access, I can't change it.

I therefore went looking for a way of hiding the subfolder from the URL, and finally came across this post: http://drupal.org/node/217710#comment-718517
By adding the provided code to my settings.php file, I was able to hide the subfolder from the URL, and without breaking Domain Access!

I'd like to recommend that the code be added somewhere (install.txt?) so people in similar situations don't spend hours pulling their hair out like I did ;)

 * Set dynamic $base_url to remove subfolder from URL
 * See: http://drupal.org/node/217710#comment-718517
$base_root = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
$base_url = $base_root .'://'. preg_replace('/[^a-z0-9-:._]/i', '', $_SERVER['HTTP_HOST']);
#3 633726-install.patch1007 bytesagentrickard
Members fund testing for the Drupal project. Drupal Association Learn more


agentrickard’s picture

Status: Active » Needs review

Nice explanation. Isn't this normally done in .htaccess?

Is there a "normal" Drupal documentation page that refers to this technique?

BWPanda’s picture

I was originally looking for a .htaccess solution, but couldn't find one...

I just did a search for 'dynamic $base_url', but couldn't find any book pages that explained this technique either...

I just think it's a very handy piece of code and thank you for posting it!

agentrickard’s picture

Status: Needs review » Fixed
1007 bytes

Yes, added to INSTALL.txt.

modctek’s picture

Wow, how coincidental! I was banging my head on this very issue. Thank you for this excellent snippet!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

xjm’s picture

This saved my life. Thank you.

espirates’s picture

Are you saying to accomplish this all we need to do is add that code and no need to touch the htaccess ?

barrabasah’s picture

thks soooo - #6 me too
but i have A problem with login to panel :/

imsovru’s picture

Issue summary: View changes

for those using ssl only sites, you can also set a dynamic base_url

$base_url = 'https://'.$_SERVER['SERVER_NAME'];

This will force HTTPS, should also add a rewrite to your .htaccess to always load https,