Hi everyone,

I have drupal installed in a subdirectory on my server, let's say in localhost/subdir

When I write a link with a target like "/about", it goes to http://localhost/about, rather than http://localhost/subdir/about. This is the problem. Here are the steps I've taken:

I have uncommented the line in .htaccess to say:

RewriteBase /subdir

I have also uncommented the line in settings.php to say:

$base_url = 'http://localhost/subdir';

I've restarted my server, and cleared all the caches..... Not sure where to go from here.

This seems like something a lot of people probably do all the time. Help please?

Comments

goron’s picture

sorry, but anyone have this problem before? Thank you in advance!

pkiff’s picture

I could be wrong, but I don't think that RewriteBase in .htaccess and base_url in the Drupal settings.php are the issue here. The problem is that "/about" tells the web server to look for the root of the web server and then look for a file named "about" in the root. If your drupal files are not stored in root, then you need to configure some kind of alias or subdomain or virtual domain in Apache that will point from your root folder to your subdir folder under certain conditions.

THEN you can use the base_url and RewriteBase settings the way they are intended (I think!).

Phil.

goron’s picture

hmmm, as far as I understand, the RewriteBase line is the one that's supposed to be used for this purpose. The comments in .htaccess state:

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
pkiff’s picture

Yeah, you might be right. I was just thinking that if you use the link "/about" then the rewrite rules may be working fine, it's just that they don't rewrite that link the way you want. The link should be "/subdir/about" if it is in a subdirectory, right? The link "/about" shouldn't take you to your drupal install at all, but should take you to the folder above it, right? If you enable those rewrite lines, then internal Drupal links will be written like this: "/subdir/sites/..." not like this "/sites/".

As I say, though, I could be wrong.

There is, however, a cool module that can help with moving back and forth from relative to absolute URLs:
http://drupal.org/project/pathologic

But your issue shouldn't require a module, so perhaps I don't understand it.

Phil.

artatac’s picture

Sub

pkiff’s picture

Just to be clear, then, you can have a folder set up as a subdomain/virtual root, or as a regular subdirectory. If it is a subdomain, then you need to configure Apache to tell it to treat the folder as its own (sub)domain, and "/about" will go to "mysubdomain.example.com/about" when you start in the subdomain directory. But if it is just a regular subdirectory, then to get to the /about folder in the subdirectory, you need to include the subdirectory name, then "/mysubdir/about" will go to "example.com/mysubdir/about".

keva’s picture

I have this same issue when developing sites that share an IP address, and have been tempted to try Pathologic. Would love to find out how others deal with it.

pkiff’s picture

I'm still not clear on what the issue is and whether it is actually "by design" or a problem with configuration or something else.

But regardless, different developers have different ways of dealing with multiple installs at the same IP address. One common way of dealing with multiple installs is to assign a different subdomain for each site. So, for example, you would have draft1.example.com and draft2.example.com. By using subdomains, you avoid running into any potential incompatibilities in links relative to root between your test server and your production server -- I think that is possibly the real issue that is causing a problem for the original poster: if you use a subdirectory that is not configured as a subdomain, then you need to include the parent directory path in links that are relative to root.

If setting up a testing environment in the way I described, you typically store the drupal code base in one subfolder, you identify another subfolder as the subdomain address, and you use a multi-site install to allow you to have several different databases (sites) to play around with that all use the same drupal core base. If you are fancy, you can have multiple installs of drupal as well so that you can test Drupal versions of different sites at the same time. On your physical disk, the folders would be laid out like this:
locahost/drupal-6.17
locahost/drupal-6.16
locahost/drupal-7.01b
locahost/draft1
locahost/draft2
locahost/draft3

The relationships between all these folders would be primarily configured in Apache, using your hosts file or whatever. The site1,site2,and site3 folders are actually empty and may be set up virtually without creating physical folders. They are just pointers to make your subdomains work. The actual files for those sites should be stored inside the appropriate drupal/sites/ folder. All the sites would share the same single IP address -- or in this case they are all located at /localhost/. Then you would use the $base_url setting (if necessary) within the appropriate settings.php file inside the right sites folder to help get everything working. It is simplest if you just use a single Drupal code base and get that working first.

There are descriptions of how to do this kind of thing in the Drupal Documentation pages:
Configuring a basic multisite development environment in Linux
and
Enable hosting of multiple sites from a single installation

Do these things address the kind of issue you are having?

keva’s picture

Your comments absolutely address the issues. I also don't know if the underlying issue is by design or a problem with configuration, but I've been dealing with the same issue as OP for ~2 years. Thanks for all the input.

I've thought about using subdomains and multisite, but what stops me is that, once live, these sites' physical files DO need to reside in directories off the root, such as 123.45.67.89/~username. I've transferred many Drupal sites to different servers ... but not ever from a multisite to a free-standing, and am not sure how the files directory, file paths, etc. would be handled.

Do you have experience with this scenario?
- Set up the development site as a subdomain
- Use a freestanding Drupal installation
- Then transfer to a subdirectory before going live
It may be an option, but I haven't had time to try it out.

Otherwise, it's looking like dedicated IP addresses for each client is the answer, so that the whole subdirectory issue can be avoided.

goron’s picture

Thanks for all your comments pkiff.

I think you're right that subdomains are the best way to handle this kind of situation. I'm working locally and didn't even consider it. I don't think that I need a multisite setup, because I don't want to share the core code with other sites, I only want to run a single site by itself in a subfolder in root. I'm not sure how to go about setting up subdomains on localhost, but I'll check out the links you posted, do some research, and see if I can get it working.

I'm still confused about the purpose of RewriteBase in .htaccess... Given the comments above it in the file, and given pages like:
http://drupal.org/node/110438
http://drupal.org/node/152752
http://drupal.org/node/836188

They all make it seem like RewriteBase in .htaccess is the solution to this problem... If it's not meant for this, do you know what its use is?

Anyways, thanks for your help, and I'll report back on how it worked out

goron’s picture

Ok. Here's a summary of my problem and solution:

I am developing a site in a development environment on my local machine. The site is in a subfolder "/drupal" in the root of my local server. When I was developing the site under all the default settings, the hompage was at http://localhost/drupal. When I created a relative link from that page to "/about", the link went to "http://localhost/about" rather than "http://localhost/drupal/about". In order to not have to change all my links when moving to the live environment, I needed a way to make these relative links work correctly with Drupal installed in a subfolder.

I thought the way to do it was using RewriteBase in .htaccess. This didn't work.

The best solution, as was proposed above, seems to be to create a subdomain under localhost. This way root-relative links will be relative to the subdomain, which will point to the subfolder. So now the homepage will be at "drupal.localhost", and the about page link will correctly go to "drupal.localhost/about". Make sure also to then set the $base_url in settings.php to "http://drupal.localhost".

I set this up and it works perfectly.

I used the following blog post to learn how to create subdomains. It assumes a Mac OS X, XAMPP environment, but would be useful for anyone, especially on a mac: http://blog.noutash.com/2010/01/10/xampp-virtualhosts-domain/

Thanks a lot to everyone for the help!

SergFromSD’s picture

Thanks goron. I had the exact same issue and your summary and explanation helped me. Just a couple notes:

To create subdomains on MacOS and MAMP I used this document: http://jeremybuff.com/articles/how-to-create-subdomains-with-mamp-mac/
Pay attention to the first comment about the need to uncomment the line that calls httpd-vhosts.conf at httpd.conf. Also make sure to comment back the RewriteBase in .htaccess.

alex.skrypnyk’s picture

I've tried to find the solution for this in many different places, but it looks like that all advices are given just to contribute "something", but not to address the issue itself.

This particular topic has nothing to do with "multisite" module installation OR setup access to dirs as sub-domains. So please refrain from solutions that only confuses people.

If anyone know how to solve described problem (running totally separate websites, installed in subdirectories of one host), please share your knowledge here.

Founder, Software Engineer | www.drevops.com

pkiff’s picture

I'm not sure which part of the problem described in the subject and in the original post isn't addressed above.

Are you having a problem getting relative links to work? I don't think that links relative to root will work in a subdirectory unless the name of the subdirectory is included in the link. The thread suggests several ways of getting around that problem. Does that describe your problem, or is there something else?

Phil.

keva’s picture

Pathologic does seem to solve the problem.
- uncomment the line in .htaccess to say: RewriteBase /subdir
- Install Pathologic
- Check it as a filter in the chosen input format
- In the Configure tab, add "/" (without quotes) under "Also considered local:"

Then links written as "/about" will automatically have the base/subdir added in front.

This works on a site with the address in this format: http://IP-address/~username

Using WYSIWYG module and TinyMCE.

jhood’s picture

Thanks keva for posting the configuration process!

Link to Pathologic http://drupal.org/project/pathologic

jct’s picture

Excellent description - this worked for me as well. The only other thing to note is that the process must be repeated for each input format to which you want the path changes to apply.

brian.gora@gmail.com’s picture

From what I see Pathologic is just an input filter so it applies to content.

But, how can I manage URLs that are part of the code inside templates? ie. On node.tpl.php I have some code like this:

<a href="/whetever"></a>
or
<?php echo '<a href="/whatever"></a>'; ?>

So what I need is those links to go /mysubdir/whatever because my site is published under /mysubdir.

Thanks in advance!

pkiff’s picture

The quick answer to this is not to use hard-coded absolute URLs in your templates. You can use drupal domain or site variables to replace hard-coded absolute URLs, and those will be replaced with the correct domains when you move the template from one environment to another. Or just use relative URLs.

Also, I only saw this post because I'm following this thread, but generally very few people will see posts in an old thread like this, and it is much better to start your own fresh question as a new post in the support forums, or in the issue queues of whatever module you need help with.

brian.gora@gmail.com’s picture

Thanks for your suggestion pkiff, so you think there's no other way to solve this but updating those URLs in templates? The thing is it's a project with already too much code using hard-coded absolute URLs so changing this everywhere will be a difficult and time demanding task.

I was wondering if there's no other way to configure that "/whatever" URLs should always point to "/subdir/whatever" via htaccess or any different method.

I'll be following your advice and open a fresh question too.

Thanks again!

vm’s picture

if the urls are in the tpl.php files , a search and replace can't be too difficult regardless of how many urls there are. Otherwise, if you want to work with a redirect in your .htaccess you can certainly test without too much effort. If they are hardcoded as absolute urls , no idea how well redirects will work.

brian.gora@gmail.com’s picture

Thanks for your suggestion VM.

Since URLs are sometimes being constructed inside variables and then echoed/printed there's no quick search and replace that can be done. I'll be forced to go thru code in many files searching for missing matches that the replace pattern has not considered. Can you think of another approach of performing this replacement safely?

The URLs are always in the form of "/somethingA/somethingB", they don't have any subdomain and domain hardcoded. I need these URLs to get the drupal folder prepended so they become "/drupalsubdir/somethingA/somethingB".

Thanks again!

georgir’s picture

When you are outputting urls or links from code, you should always run them through the url or l functions.

kimble’s picture

Old as this thread is, I hope this helps someone as I recently ran across this need in an old D6 site.

If both $base_url in settings.php and RewriteBase in .htaccess are changed, I think they somehow they cancel each other out.

If I edit $base_url = 'http://localhost/subdir' then http://localhost/subdir/about works fine. You would do this to get the result goron, the original poster, was looking for.

You would use RewriteBase to change you web root to a different directory. I.E. have http://example.com point to public_html/subdir instead of public_html/ without showing the directory structure.