Hi there. I'm setting up a new install of Drupal 7 and have run into a problem with Drupal trying to get request URLs from Apache. I want to keep the drupal files together in their own directory, but I don't want the URLs generated by Drupal to have '/drupal7' on the front.

public_html/.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /drupal7/$0 [last]

With this setup, going to http://example.com/taxonomy/3 correctly serves me a page as if I had gone to http://example.com/drupal7/taxonomy/3; so far so good. However, the links on that page all have /drupal7 on the beginning. In order to prevent that, I did the following:

public_html/drupal7/sites/default/settings.php:

$base_url = 'http://example.com';

Now the generated links look correct: http://example.com/taxonomy/3, but there is a problem when I actually follow one of them: I get a 404 error! When I look at the 'Recent log messages' report, I see a 'page not found' log entry for the following message: 'term/3'. It seems that the first 9 characters of the request path have been stripped.

I solved this by monkey-patching the request_path function like so:

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 2d68c81..d2382b4 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -2513,10 +2513,13 @@ function request_path() {
   }
   elseif (isset($_SERVER['REQUEST_URI'])) {
     // This is a request using a clean URL. Extract the path from REQUEST_URI.
+    //error_log ('REQUEST_URI: ' . $_SERVER['REQUEST_URI']);
     $request_path = strtok($_SERVER['REQUEST_URI'], '?');
-    $base_path_len = strlen(rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/'));
+    //error_log ('SCRIPT_NAME: ' . $_SERVER['SCRIPT_NAME']);
+    $base_path_len = 0; // strlen(rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/'));
     // Unescape and strip $base_path prefix, leaving q without a leading slash.
     $path = substr(urldecode($request_path), $base_path_len + 1);
+    //error_log ('path: ' . $path);
   }
   else {
     // This is the front page.

Those log statements output the following messages, respectively:

REQUEST_URI: /taxonomy/term/3
SCRIPT_NAME: /drupal7/index.php
path: /term/3

So, I've worked around the problem. My question is: is it is a bug in Drupal? I don't see why it is trying to remove the leading portion of the REQUEST_URI server variable. But then again, it may be my configuration that is the problem, and if I had set things up in a different way, I wouldn't have run into the problem. Anyone?

Comments

jscoble’s picture

Have you tried just setting the DocumentRoot in your Apache configuration for that site to /webroot/drupal7? Where /webroot is the full path to drupal7 directory?

No rewrite rules should be necessary, unless I'm misunderstanding the issue.

yrro’s picture

There are other things in the public_html directory though; I still want them to exist independently from the drupal files.

David4514’s picture

Following instructions found on drupal.org, I had a test site running under a subdirectory just fine. The issue was that the subdirectory name appeared in all of the urls exposed to the users. This was ok for testing, but as I near production, I wanted to leave drupal installed in its subdirectory, but make it appear to the user that it was in the root directory.

I do not have control over the document root on my host server, so I've been attempting to use mod_rewrite to allow me to hide the subdirectory in which drupal is installed from the users. Nothing has worked. I was coming to the conclusion that the problem was within bootstrap.inc when I encountered this post.

I made the same change. In addition, I had to change in settings.php
[Edit] Just noticed that the original post mentioned this as one of the things that had to be changed. [/Edit]

FROM: $base_url = "http://www.mydomain.com/drupal";
TO: $base_url = "http://www.mydomain.com";

All works great with this change. Is there some way better way that does not require hacking?

Dave

mat8iou’s picture

I'm having exactly the same problem with Drupal 7.

I had it set up in this way with Drupal 6, with no problems at all, for 6 different sites.

With Drupal 7, it works if Clean urls are turned off when I upgrade, but I can not enable them, as the clean url dialog box does not give the option to enable it.

If I upgrade with clean urls turned on, then I just get 404 errors with nearly every page - I can bring up some pages if I alter the url to show the version without clean urls, but any links on the pages still point to clean urls & give an error.

I haven't installed the patch given above yet, as I'm assuming that I can somehow fix this error within .htaccess or something like that, although as I exhaust more & more options, I'm getting less convinced that the issue can be solved easily.

I'm still unclear exactly what changed in Drupal 7 that broke this feature. I want to run it in a sub-directory, as I still have links to various other pages etc in the root directory & if everything sits there together, it becomes tricky to work out which files are drupal related & which are not.

rjlyders’s picture

yrro, thanks for much for your instructions. It saved me a lot of debugging time dealing with this in Drupal 7.2 as I also have other sites under public_html so I can't just change the doc root. Hoping that this issue is fixed soon.

regards

boonep’s picture

I had the same problem. Solved the issue by editing the Drupal 7 .htaccess file:

#Replaced (line 114):
RewriteRule ^index.php [L]

#with the Drupal 6 version of the same line:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

mat8iou’s picture

Tried that - doesn't seem to work for me. I still can't get clean urls to work if I do this.

lucyconnuk’s picture

For anyone trying to get this working, this worked for me (D7.10). I have the following .htaccess in the root directory:

RewriteEngine on

# Redirect "" and "/" to "drupal-7.10/index.php"
RewriteRule ^$ drupal-7.10/index.php [L]
RewriteRule ^/$ drupal-7.10/index.php [L]

# Allow through any files or directories which exist...
# ...But redirect everything else to drupal-7.10/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) drupal-7.10/$1 [L]

I have $base_url uncommented in sites/default/settings.php.
And I have edited the D7 .htaccess file as per boonep's comment above.

All now working fine, including clean URLs.

jenniferamcd’s picture

I've got the same issue and I'm trying to use your instructions, but you lost me at "I solved this by monkey-patching the request_path function like so:" - what is done with this code? New at this...

desoi’s picture

Thanks for identifying the issue. I added to this bug report -- hopefully it will get some attention.

http://drupal.org/node/885846