FYI, a Drupal installation tip.

In order to keep all the Drupal files together, I wanted to install it into the ~pageroot/drupal/ subdirectory of my web server, but I didn't want to have to access it via http://www.mysite.com/drupal/ -- I wanted to access it as if it lived in the root, at http://www.mysite.com/.

Here's my solution:

In the root, I placed an .htaccess file that contained:

Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule .* http://www.mysite.com/	[L,R=301]
RewriteRule ^$ drupal/index.php	[L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

Then in drupal/.htaccess, I disabled the mod_rewrite instructions by changing "<IfModule mod_rewrite.c>" to

<IfModule XXXmod_rewrite.c>

Finally, in drupal/sites/default/settings.php, I uncommented the line

$base_url = 'http://www.mysite.com';

Not sure if there might be a better solution, but this seems to work fine.

Comments

avolve’s picture

I do the same as you are wanting with many sites. It has benefits far beyond what you get through your solution... and does not require modifying .htaccess files

I use dreamhost (i am sure many other hosts have similar options) and specify example.com/drupal as the web directory for the domain.

--> when people visit example.com what they actually access is example.com/drupal.

Hope this is clear
c.

avolve designs | ethical by design

lemmax’s picture

Avolve, I like your suggestion but just thought I'd point out my experience using hostmonster. Using hostmonster, specifying the web directory is possible for addon domains but **not** the primary domain.
Also, the content for each addon domain is stored as a subfolder of the primary domain folder (public_html).

IsabelG’s picture

I also use dreamhost. I installed in a /drupal folder and everything worked fined except that my 'clean' url (www.mydomain.eu) showed a file listing of the directory, not my initial page.

After I changed the web directory in the dreamhost panel to mydomain.eu/drupal, I got plenty of 'page does not exist' errors in my site.

I had to edit /drupal/sites/default/settings.php and remove /drupal from this line:

$base_url = 'http://www.mydomain.eu'; // NO trailing slash

Just to confirm, there is no need to modify the .htaccess file.

tsavo’s picture

Hi,
I did ask my host to point to the subfolder; however the bartik logo image is not displaying.

Many thanks.

Bent Aarup’s picture

Thanks for the private message, where you state that your site is working fine inside a subfolder, but the urls to images is broken. I thought it would benefit a wider audience to put my answer here instead of in a private reply.

So you have like: http://www.domain.com/subdomain/ and the subdomain is working as a root when entering with a browser, but urls for images (and other resources later on) is not working, they are basically asking for a full qualified url from the main domain.

You could edit all your content and put full qualified urls there, but there is an easier way.

Use the module pathologic

Pathologic requires Filters to be enabled, and then you set it up for each filter you want it to scan, usualy "full html" and "filtered html". If you set it up for using "Full URL", you will enter all the base paths you need, fx. like this:

http://www.domain.com/
http://www.domain.com/subdomain/
http://www.domain.com/subdomain/sites

Pathologic will look for your resources in all base paths you set up. In you content you will just have to enter the relative path from one of these roots. A relative url for the bartik logo would then be:
src="themes/bartik/logo.png". Do NOT start your relative url with "/"!
Make sure that "Correct URLs with Pathologic" is the LAST filter in the Filter processing order, and from there on it's fire and forget, you don't have to think about that problem anymore.

qasem’s picture

really perfect solution...it helps me to clear my root and to be clean as much as possible
until now there is no any problems
there is only one missing clue....until now i did not install my SSL Certificate ...i wish i do not see any problems because of rewriting condition
i will try and be back ;)

udijw’s picture

looks like a very clean solution.
can you get clean URLs with it?

wmw’s picture

What you've talked about may be the solution to my Drupal website. I uploaded drupal to my site about 2 months ago. For reasons I don't remember now, I placed the drupal 5.1 folder right underneath the the public_html folder. I fiinally get back to this drupal project and just realize that my web server recognizes only index.html or index.php at the root level as the home page. To comply with this rule, I thought my options are

1. move all drupal files to the root level
2. in the root level index.php, redirect to the drupal index.php.

I wasn't quite sure which way is better until running into this thread. I think the solution of interest here is the 2nd option above. I'll appreciate greatly any suggestions from you. Thanks,

Warren

udijw’s picture

OK, I tried it and it worked like a charm.
I am still having problems with some of my multy site pointing to the original site. but hope to get that fixed soon.
for some reason pointing my browser to www.second_site.com displays www.first_site.com
any ideas? - I guess it must be realted to the www.mysite.com rules in the htaccess.

jiangxijay’s picture

Your htaccess script works great on requests with HTTP_HOST= www.mysite.com! Thank you!

However, it also appears to process requests from any domain, not just www.mysite.com, including:

subdomain1.mysite.com
subdomain2.mysite.com
subdomain3.mysite.com
www.anothersite.com
subdomain1.anothersite.com
subdomain2.anothersite.com
subdomain3.anothersite.com

I want it to leave those alone ;)

If the script is supposed to leave those other domain requests alone, is it possible that it requires a particular flavor of Apache? (My host runs 1.3.37 on Unix.)

If not, is there any alteration that will just pick up that one HOST domain and leave the rest?

pulsifer’s picture

Its these two lines that make it redirect all hits to www.mysite.com

RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule .* http://www.mysite.com/ [L,R=301]

If you want to only rewrite hits to www.mysite.com, and leave everything else alone, you would need to replace the rewrite rules with something like this:

RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
jiangxijay’s picture

As someone who is not familar with the subtleties of rewrite syntax, I have to bow very low and say thank you.

I suspect that there are others who want to host multiple Drupal domains — along with static domains and subdomains — all in one Apache account and with one MySQL database. You have made that possible!

udijw’s picture

Hi Pulsifer,
I am using a slightly modified version of you rewrite rules:

Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*wiki.*
RewriteRule .* drupal/index.php?q=$0 [QSA]

This enables me to have all my muti sites on the the same installation AND have drupal installed in public_html/drupal
I am no trying to install a wiki at public_html/wiki and set it with short URLs, but no matter what I do, drupal "steals" my WIKI's short URLs.
any advice?
thanks,
Udi

whatdoesitwant’s picture

I am still figuring this out myself.
As far as i can see, you are directing everything to your drupal directory.
If you base your redirect rules on the domain name, you can use that as an indicator for the directory you want to redirect to.

I hope someone else can provide another example of how to do this.

Two cases where you want to do this are:
When you want to have a drupal installation run next to another installation on the same server.
Or - in my case - when you want to to do something like http://justinhileman.info/articles/a-more-secure-drupal-multisite-install and can't use cpanel or httpd.conf to setup the preliminary redirects.

Normally in drupal, the goal is to redirect everything to the drupal directory and let drupal do the sorting. In our case, before letting drupal do its thing we want to use .htaccess in our root to redirect different domains to different directories. That seems to be outside the scope of drupal (which it isn't) and I think that this is the reason why there is so little information available on the subject.

jiangxijay’s picture

Ok, so after having everything generated through index.php working fine, I discover that files aren't passing correctly.

For example, this is working ...

...however, this isn't ...

I should add that I'm seeing the files fine via ftp, they're just not being delivered through http via the htaccess instructions.

Thoughts?

FOLLOW-UP

For anyone who is reading this thread: the htaccess instructions, above, work fine ...

The file system path has to reflect a /files/ directory inside the specific site that is being referenced.

Then it works fine!

andrabr’s picture

I wanted to handle both www.mysite.com and mysite.com,
so I basically added another copy of your rules but without "www" and it seems to be working so far (not that I know why).

RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
rptrevor’s picture

Thanks. I did exactly the same thing (on Drupal 5.3) with the same results (ditto on the "not that I know why").

BUT

Although all the page requests seem to be getting resolved correctly (both within and without Drupal), I'm seeing tons of "page not found" entries in the Drupal log (for non-drupal pages) - even though those pages actually seem to be loaded OK.

details:

drupal lives in a subdirectory off the base (httpdocs in my case).
Several other non-Drupal pages/sites also live off the base, e.g.:

(base)/drupal
(base)/abc
(base)/def
etc.

- mysite.com (and www.mysite.com) resolve (or are redirected?) to the drupal site.

- URLs like: mysite.com/abc/index.php, mysite.com/def/index.html seem to load the non-drupal sites just fine.

- If I access a non-drupal page: http://mysite.com/abc/index.php - the page loads, seemingly correctly, but the Drupal log shows "page not found" errors for things like:
abc/CSS/foo.css
CSS/foo.css

Notes:
1. I did set the $base_url in settings.php to http://mysite.com
2. I did not touch the .htaccess file in the drupal subdirectory.
3. I'm sure that all the non-drupal sites use relative URLs in their html to access their own subdirectories. e.g.: link href="CSS/foo.css" rel="stylesheet" type="text/css"

Any insights? Thanks!

superjacent’s picture

Ok, I've got the exact same code as above to make it appear that Drupal is installed in the root. It works (thanks to this thread).

I've since created a sub-domain called develop. So is accessed www.develop.mysite.com . Initially I made the sub-domain folder a symlink which pointed to the Drupal installation folder (mysite.com/drupal). I correctly set up my Drupal 'sites' directory to account for 'mysite.com' and 'develop.mysite.com'. This all worked and had successfully created my first Drupal multi-site set-up.

My problem is that I can't activate clean url's for the sub-domain 'develop'. Drupal (6.1) administration page has disabled my ability to enable clean url's. As the 'develop' folder is only a symlink there is no way to create a .htaccess file for that folder (or is there?).

If I want clean url's for the sub-domain, how do I handle this situation?

My attempts so far have led me to deleting the symlink 'develop' and creating a normal directory 'develop'. Within the 'develop' directory I created a .htaccess file based upon the RewriteRules as suggested in this thread. I'm stuck. Can't even get the sub-domain up and running under this method.

Any advice most welcome.

__________________________

Steven Taylor
Melbourne, Australia.
http://superjacent.net/cms

Anonymous’s picture

Woohooo it wooorrkss

snevarez89’s picture

You just saved my a lot of time. Thank you very much.

tterranigma’s picture

Be sure to add the B flag in the last rule to prevent urls from being decoded:
RewriteRule .* drupal/index.php?q=$0 [B,QSA]

kjv1611’s picture

Could you expand on this idea? Does that mean that it would not be possible for the site visitor to go from:

www.MyVisibleSiteName.com

to:

www.myMainSite.com/MyVisibleSiteName

??

For instance, if I have a link within that goes to the 2nd example above (on accident), would the B flag help fix that?

Thanks for any info

cog.rusty’s picture

Amazing! I didn't believe this was possible without major Drupal surgery, but its seems to hold against everything I have thrown at it so far!

jiangxijay’s picture

The second version is especially powerful for multiple domains using both Drupal and static content.

cog.rusty’s picture

True. I didn't mean to use it actually -- I simply use domain pointers, and with this solution I would be afraid to modify .htaccess for other things that I wanted. But it is an elegant solution to a problem which has puzzled many people.

jiangxijay’s picture

Hi pulsifier-

The second version of your htaccess code works like a charm for a Drupal-delivered site, in that it makes sure URLs are handled according to Drupal processes, including clean URLs. Bravo.

I also want to pass non-Drupal domains into another subdirectory, leaving the entire path intact. (Again, I'm not skilled with rewrite syntax. ;( Which rules would go away in order to leave the paths for a non-Drupal domain?

I'm making a donation to Drupal for your help on this. It deserves attention at the highest levels ...

-jay

jiangxijay’s picture

I found the answer to my own question.

Here are instructions for rewriting domains for static (non-Drupal) sites to subdirectories in a site with parked domains:

http://www.bluehostforum.com/archive/index.php/t-288.html

-Jay

aviceda’s picture

I'm trying to achieve the same end, to make my drupal subdirectory think that it is the /root.
However in my drupal subdirectory .htaccess file there appears to be 2 similar sections using the IfModule mod_rewrite.c tags, an automatically generated one at the start of the file (which instructs not to edit) and another further down......which did you mean?

jiangxijay’s picture

The first if statement appears to surround an expiration rule; I didn't change it.

# Reduce the time dynamically generated pages are cache-able.
<IfModule mod_expires.c>
  ExpiresByType text/html A1
</IfModule>

The second surrounds the rewrite rules; I did change that one.

# Various rewrite rules.
<IfModule XXXmod_rewrite.c>
  RewriteEngine on

I didn't find a second IfModule mod_rewrite.c statement.

The process is working fine for me making the changes in this way.

TheFazz’s picture

thanks. [subscribe to thread]

k_and_j’s picture

I have a multisite, so I used the second .htaccess fix and put that in ~/public_html/ with only partial luck.

NOTE: The mod_rewrite.c suggestion messes up my site2 (I get in and see content, but all the links are broken). And, for multisites, the settings.php change must occur in drupal/sites/site1/settings.php not the default directory.

The /drupal/ is not needed for any pages (good!)
BUT...
Some links go to a Drupal "Page Not Found" page. If I go to www.mysite1.com, which is linked from the "Title" at the top left and from "Home"...I have to navigate specifically to www.mysite1.com/index.html (since that's what I have for my URL alias of the front page).

Also, if I go to mysite1.com I get a non-drupal "Page Not Found"

Any suggestions?

k_and_j’s picture

I tried udijw's .htaccess code and that gets rid of the non-Drupal "Page not found" when the www is omitted (ie, now I get the same Drupal version of "Page not found" with and without www). I still would like suggestions for how to avoid the "Page not found" completely.

k_and_j’s picture

I just needed to re-set the frontpage URL in Home » Administer » Site configuration

accountsmart’s picture

Clean url is a good choice.

----------------------------------------------------
accounts mart

coupet’s picture

What effect does this solution have on Search Engines?

----
Darly

kriskd’s picture

I would love to make this work for me so I don't have to put Drupal in my root, but it's not working out that way. Here is the .htaccess file I put in my root:

Options -Indexes
Options +FollowSymLinks 
RewriteEngine On 
RewriteRule ^$ drupal-5.3/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal-5.3%{REQUEST_URI} -f
RewriteRule .* drupal-5.3/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ drupal-5.3/index.php?q=$1 [L,QSA]

I uncommented the line in setting.php, but I did it for sites/mydomain.com/settings.php Is that a problem?

I didn't have a .htaccess in drupal-5.3 so I created one with just that line. Is that the problem? I saw elsewhere in this thread another user didn't need that step.

Also, what role does setting up a symlink play in this procedure?

Thank you in advance.

k_and_j’s picture

This only changes the way the URL looks, so you should be able to have a working drupal set-up where it is not installed directly in your Root without changing any of the things listed here. You should try this only after you have your website working and you can navigate to it from mydomain.com/drupal-5.3/

You'll only need symlinks if you are doing a multisite install or if, for some reason, you don't put the drupal installation in a subfolder of your webroot folder.

bsuttis’s picture

The first post works great -- thanks! However, I'm looking to take things a step further -- my drupal install is in a subdirectory called /content/, and while the above allows me to keep the install in /content/ while removing it from the url, there are incoming links with /content/ from third party web sites and search engines.

Ive been trying to redirect /content/ with this added as another line to the root htaccess: RewriteRule ^/?content/([a-z/.]+)$ /$1 [R=301,L] no such luck though, any suggestions are appreciated.

bib_boy’s picture

Think I may have a slightly similar problem to 'bsuttis'. I have an old site which I have now 'converted' to drupal. I have the drupal installation in a sub-folder called /drupal. I have used the htaccess as above and am really impressed that it works..however..

I have used url aliases in drupal to match my old article urls so I dont get broken links from other sites...when I apply the htaccess, as descibed at the beginning of this post, drupal is not taking control. What I mean is, instead of showing the new drupal alias (e.g. www.example.com/archive/story.html) it shows the old version (which is obviously still on my server). OK, if I delete the old file drupal then takes control, but surely there is a way in the htaccess to control whether drupal has precedence or not??

bib_boy’s picture

just to clarify...

in the htaccess it looks for the old file first then if it is not there drupal takes over...what I want is for drupal to take control first and if it is not a drupal page it is at least picked up by the 'old look' static site page with the same url.

thanks

delete_my_sorry_self’s picture

I'm with bsuttis: I want to change the URLs on my site to make it look like drupal is installed in the root directory, but I need to redirect the old URLs so they're not lost to search engines, bookmarks, etc.

For example:
- Drupal is installed in /drupal and has been running there for a long while (i.e., URLs are well known to SEs, etc.). A common URL for a node might be: http://mysite.com/drupal/post/this_is_a_node
- I now want it to appear as if the site is based in the root dir as far as the visible URLs are concerned: http://mysite.com/post/this_is_a_node
The solutions posted here do the above just fine.
- I also need to keep the old URLs (with 301 redirect); e.g., http://mysite.com/drupal/post/this_is_a_node still loads the proper node by redirecting to http://mysite.com/post/this_is_a_node

I've tried numerous combinations of additional rules, both in my root .htaccess and /drupal/.htaccess, but no combination has shown any promise--again, similar to bsuttis. So far, there haven't been any working solutions posted here, elsewhere on drupal.org, or anywhere else that I've been able to find. Has anyone actually solved this problem yet?

Thanks much.

delete_my_sorry_self’s picture

I think I found the answer to this. My root .htaccess is pretty much what was originally proposed in this thread, plus the changes to drupal's .htaccess, plus the base-url change (leading Drupal to believe it's in the root dir). I also added to my root .htaccess bsuttis' rewriterule meant to enable use of the old URLs, redirecting them to the new ones (minues the "/drupal" in the path). The final piece was renaming drupal's actual directory from "/drupal" to, say, "/drupal4", and adjusting .htaccess accordingly. Note, though, I kept bsuttis' old-URL-redirect referencing "/drupal", a now nonexistent directory.

Here's my root .htaccess:

# Now if it's anything but mysite.com, fix it
RewriteCond %{HTTP_HOST} !^mysite\.com
RewriteRule (.*) http://mysite.com/$1 [R=301,L]

# Check for old /drupal-based URLs and remove /drupal
RewriteRule ^/?drupal/(.*) http://mysite.com/$1 [R=301,L]

# Make drupal the home page
RewriteRule ^$ drupal4/index.php [L]

# If an actual file exists in drupal4 dir, prefer it
RewriteCond %{DOCUMENT_ROOT}/drupal4%{REQUEST_URI} -f
RewriteRule .* drupal4/$0 [L]

# Otherwise change behind-the-scenes URL to drupal4
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal4/index.php?q=$0 [QSA]

Notice I fix the domain name first and don't bother checking it subsequently; just my personal choice. Next is the redirect for the old "/drupal" paths previously bookmarked by visitors, RSS readers, and search engines. Then, the rest is the same as was originally proposed, except that the "/drupal" path is now "/drupal4". So far, this seems to work like a champ, but please let me know if you see any problems with it.

bsuttis’s picture

Confirming that the code works for me, thanks much Wayne.

asb’s picture

Is it possible to fetch any subdomain (like www.domain.com, en.domain.com, de.domain.com etc.) with this rewrite rule? What has to be changed?

Thanks, -asb

Edit: I found out how to do this:

RewriteRule ^olddir/(.*)$ /$1 [R=301,NC,L]

where "olddir" is a path like www.example.com/olddir, that is redirected to www.example.com. All content index by search engines can be transparently accessed with this redirect.

djuricas’s picture

Hi i am having a problem with the front page which should be node listing page, everything is working ok, when drupal is in the root on my hosting account, but I put it in the subfolder and I am using mod_rewrite to hide the subfolder in url.

Both menus that link to home page are set to <front>.

So when I look at the article in one language and then click on the same article in other language, and then click on the home page, I am always getting the default language home page even if the menus are on the other language.

I do hope this is clear, anyway you can see the problem on www.spc.yu

I am using the following setting for mod_rewrite

here is .htaccess file in the root of my hosting account
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^$ cms/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/cms%{REQUEST_URI} -f
RewriteRule .* cms/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ cms/index.php?q=$1 [L,QSA]

and the in cms/.htaccess file I changed <IfModule mod_rewrite.c> to <IfModule XXXmod_rewrite.c>

and in settings.php I uncommented the line
$base_url = 'http://www.spc.yu;

Any solution?

JosephineSoap’s picture

I've storys with images loaded that aren't getting displayed now...anyone have any ideas of what else i need to change?

JosephineSoap’s picture

figured it out...i didn't change www.example.com to my own domain!

thx538’s picture

I have drupal 6 RC2, and while the solution works for rewriting URL's I have had all subdirectories below drupal not accessible by Drupal itself and generating "Page not found" errors.
I have discovered that those statements do not work with my hosting provider

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/mysubdir%{REQUEST_URI} -f

After fighting with various trials and errors , I have tweaked the settings as below :

Options -Indexes
RewriteEngine on
Options +FollowSymLinks

#site
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mysubdir
RewriteRule ^$ /mysubdir/index.php [L]


#files
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^.*\.(jpg|jpeg|gif|png|css|js|pl|txt)$
RewriteCond %{REQUEST_URI} !^/mysubdir/
RewriteRule ^(.*)$ /mysubdir/$1 [L,QSA]

#url 's
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mysubdir
RewriteRule ^(.*)$ /mysubdir/index.php?q=$1 [L,QSA]

.. and it seems to work. Hope it helps someone else.

Tatyana-dupe’s picture

I am so new to all these, so please...

I also have drupal installed in shared hosted environment in www.mywebsite.com/drupal directory
I was trying to follow your instructions, however:
1. i do not know what is the easiest way to modify files that are located on the hosted server - when I download them on my PC and open with notepad - they come in a way that's hard to read and modify;
2. i noticed that there was already file in root directory of www.mywebsite.com with the name .htaccess - should I delete this one and replace with the one you mentioned?

Thanks!

k_and_j’s picture

1. Try Wordpad instead of Notepad or try another text-editing program. I use UltraEdit and I love it...it's not free, but it's not too expensive.
2. Open the .htaccess file in your text editor and see if it contains anything...it might be empty. If it's not, just add the new text recommended here to the existing .htaccess.

graou’s picture

I use Notepad++, GPL and free.
hope that helps

tekken’s picture

I used the above description with Drupal 4.7 and it seemed to work nice because I could access the page from the root. The problem was that I couldn't access the admin section anymore. I was able to login and saw my user menu but any further click would give me an "access denied" error.

I "solved" it temporarily by installing Drupal 6 and doing the same changes proposed here in the thread. During the first day of my installation all way working well, I could even log in and do changes to my site.

However now, I am suddenly getting the same errors over and over again: access denied. Can't administer my site anymore.

Deactivating CleanURLs didn't help...

What is causing this? Is it maybe a cookie error? Help is greatly appreciated!

tekken’s picture

I reverted all the changes suggested in this thread. But the login problem remained. I got the access denied message over and over again.

After reading through "access denied" threads for hours without finding any solution, I finally found the solution in this post http://drupal.org/node/92517#comment-169664

Adding session_write_close() at the end of index.php solved it for me!

Isaacheng’s picture

(1) I did as you suggested but it does not point me to the drupal database.

(2) I place drupal in the subdir www.mysite.org/www/DRUPAL (caps)

(3) I changed the root .htaccess. The 'mysite' is changed to my domain name. 'drupal' is changed to 'DRUPAL'. I deleted the original contents in the root .htaccess.

The deleted original contents are:
Options +Indexes
php_value error_reporting 7
php_flag display_errors off

(4) I changed the drupal .htaccess as suggested.

(5) The www.mysite.org/www/DRUPAL/sites/default/settings.php already has the line commented out. So no action there.

......

But it does not work :(

I read as many posts here as i can find and did my best to find out the source of the problem. Can someone help please? Thank you.

STyL3’s picture

I'm pretty new to mod_rewrite rules and .htaccess altogether. I was wondering if someone could summarize the different methods for the different situations. There seem to be many methods listed in the post and I'm confused as to which one I'd need. I think it would also help others with similar problems.

My dilemma is as follows:

- I host many sites, both drupal and non-drupal
- My primary domain (primarysite.com) is running via drupal 6
- I would like to host my primary domain under ~/public_html/primarysite/drupal_files_here, instead of under ~/public_html/
- I would like primarysite.com to display as primarysite.com instead of primarysite.com/primarysite/
- I need secondarysite.com which resides under ~/public_html/secondarysite to be unaffected and display as secondarysite.com

Any advice?

STyL3’s picture

My setup:
I want my primary domain to reside in a subdirectory under /public_html so I have added some rewrite rules. However, before we get to that I'm running the following types of sites on my shared webhost:

- static
- drupal 5.x
- drupal 6.x

Drupal 6 runs my primary domain which i have in a subfolder called primary. Its path is ~/public_html/primary and all the drupal 6 files reside here. It will not contain multiple sites.

For simplicity, we'll say that my static site sits under ~/public_html/mystaticsite.

Using the following code in my ~/public_html .htaccess file:

Options -Indexes
RewriteEngine on
Options +FollowSymLinks

RewriteCond %{HTTP_HOST} ^www\.myprimarysite\.com$ [NC]
RewriteRule ^$ primary/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.myprimarysite\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/primary%{REQUEST_URI} -f
RewriteRule .* primary/$0 [L]
RewriteCond %{HTTP_HOST} ^www\.myprimarysite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* primary/index.php?q=$0 [QSA]

and by setting my ~/public_html/primary/sites/default/settings.php baseurl to:

 $base_url = 'http://www.myprimarysite.com';  // NO trailing slash!

my static and drupal 6 sites work great. If I go to www.myprimarysite.com i am redirected to ~/public_html/primary and it does not display /primary in the URL. Also, it does not effect www.mystaticsite.com or any of my drupal 5 sites (which is a multiple site installation with sites pointing to a ~/public_html/drupal directory)

All of my static sites are true add-on domains through the cpanel and so are my drupal 5 sites. The drupal 5 sites just point to ~/public_html/drupal directory since drupal is able to render the appropriate site based on the sites folder.

bassio’s picture

STyL3, I am glad you got it. :)

but can you please further explain how the above code made you manage each site separately? (As I cannot see any www.mystaticsite.com in your syntax)

I was looking for a solution very much similar like yours.

[I am poor in rewrite syntax .. sorry] but I was looking for a solution of multiple installs (not one multisite install) .. with every install in a separate subfolder. And I want to place a htaccess file in the root to manage both domains.

For instance:
www.firstdomain.com points to root/domain1
www.seconddomain.com points to root/domain2

via .htaccess of course (since other methods like symlinking for example or vhosts cannot be managed on shared hosts .. and you are left with .htaccess method)

I assume someone can help me with that. ;) Is this feasible? Or do I have to resort to multisite and spare the fuss?

Thanks

STyL3’s picture

Bassio-

I only edited the .htaccess that sits directly under /public_html. There was no need to have any other information in the file (regarding static sites or other drupal sites) since they did not share the same domain name. The above .htaccess file will allow all sites to go to their respective directories (it just leaves them alone and does not do anything to the request). However, if someone requests www.primarysite.com it will direct them from /public_html to the /public_html/primarysite folder and rewrite the url - displaying www.primarysite.com instead of www.primarysite.com/primarysite.

Does this help?

jsibley’s picture

It looks like if someone uses

www.myprimarysite.com/about-me it will fetch

www.myprimarysite.com/primary/about-me.

That's great. But what about if someone types in or the menu shows

www.myprimarysite.com/primary/about-me?

Won't that fetch the same page and display the full URL? One of the issues that I run into is that my menus include the "/primary/" in their links, so that is what the user sees when they use my menu (if someone has an idea about how to handle the menu issue, I'd appreciate it).

Maybe this is naive, but I'm concerned that redirecting

www.myprimarysite.com/primary/about-me to

www.myprimarysite.com/about-me

in addition to the rule you have to hide "/primary/" could lead to an infinite loop. Is that not an issue or is there a way to make sure that there isn't a loop while making sure that the user always sees the version without "/primary/"?

Thanks

mtraian’s picture

Thank you all for your contribution. I was using redirect 301 :))) and I was sooo unhappy.

Traian

kjv1611’s picture

I like this method a lot, but didn't notice it (sadly) until the last person replied, thanking you for the code. I'll have to take a look at adjusting my .htaccess in the public_html folder to line up more with this one. I have one that's working fine on one hosting package with a few websites under the same public_html folder. And one thing I noticed (hind site) was that when going to the home page, it shows the way I want (without the subdirectory name), but when someone clicks a link in the site, it ends up showing the subdirectory name. So it sounds like this will fix that. I'll give it a shot.

Thomasr976’s picture

that this most obvious needs are not so easy to implement. At least for a newbie like myself.

Nevertheless, I have the same situation, except I have a test site on my new host's server. See details at http://drupal.org/node/236106 which I created before I found thread.

I also have not pointed my domain to my host yet as my non-drupal site is still running on a different host server. Would hate to implement the changes, point my domain to my new host and find that it does not work.

So is there any way to test whether this will work before I point my domain name to my new webhost? Thanks for your help and contribution. I would be totally lost without this thread.

dman’s picture

The 'Obvious' thing to do is of course place your files where they are expected to be.
Anything beyond that is slightly tricky, depending on your abilities.

Options include:
- having a webhost who realizes this is not really that big a deal, and provides a control panel interface that lets you define where your webroot is served from. This comes with scale. It's just not needed or appropriate for small out-of-the-box hosting packages, but is handy when you really start running multisites.
- experimenting with symlinks. Useful sometimes, but probably out-of-scope for cheapo hosting packages also. Also probably not a great idea when you have already put your files in a subdir of the 'target'. Could get messy.
- Just bung in a redirect page from the top. Super-easy, but you have to live with the subdirectory in your URL. Bonus however is is you can run other stuff alongside with no conflict.
- Getting enough control of your server to edit your own apache configs and just do it the way you want. Requires knowing what you want, how to do it, and a non-trivial hosting package.
- Actually figure out what you want from telling the browser one things and the server another to alias/redirect via .htaccess redirects. Yes it's messy, but it's only a work-around because you've chosen to forego all the above proper ways to do it. rewrites are usually cheats. Nifty ... but cheats, because they are lying to the system :-)

ANYWAY. As you've chosen option E ... yes, you do want to test :-)
It's probably covered elsewhere, but

BEFORE changing your DNS propogation etc, preview the site as if the DNS propogation had already happened.

Do this by editing your c:\windows*\system32\drivers\etc\HOSTS file to include your domain name and the new hosted IP.
This lies to your personal machine about where to go to find the website.
The website is already listening for requests on that IP ... although no-one is (yet) looking there for it.
You will now be the only machine that knows to ask that host machine for that named website.
It'll be happy to respond privately.

Test and fix any problems you find.
Your existing/old site will be inaccessable (to you) while this hack is in place. Undo it to put the internet back to normal. No restarts needed usually, but I dunno, you may have to close your browser session, depending.

Pay attention to the difference between yoursite.com and www.yoursite.com. Use one, the other or both, but do it consistantly.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Thomasr976’s picture

that deal with option A and provides that control panel access? Mine has essentially abandoned me. This is not an el cheapo hosting package either, but they don't really support drupal. A friend used them and I thought I could manage. Lesson learned is that I need a higher level of support.

Will try what you proposed and also what the next commenter suggested. But I will ultimately change hosts.

dman’s picture

I'm using (among others) Servage.com
and it made me smile when I saw that feature.
Makes testing sites totally trivial also, as I can just switch from having

[ http://demo.mysite.com/ => /home/xxx/xxx/domains/www_v2/ ]
to
[ http://www.mysite.com/ => /home/xxx/xxx/domains/www_v2/ ]
without file copys, backups or config strangeness.

and, yes, I built a drupal install underneath siteroot in /home/xxx/xxx/domains/www/drupal5.1/
But when I was ready, I just set
[ http://www.mysite.com/ => /home/xxx/xxx/domains/www/html/drupal5.1 ]
and it went. And I knew I could flick back.

Also runs great with Drupal multisite, as I have
[ http://personal.mysite.com/ => /home/xxx/xxx/domains/www/html/drupal5.1 ]
[ http://reference.mysite.com/ => /home/xxx/xxx/domains/www/html/drupal5.1 ]

(All going to the same place)
and that triggers the respective

.../www/html/drupal5.1/sites/personal.mysite.com/settings.php
.../www/html/drupal5.1/sites/reference.mysite.com/settings.php
etc ad infinitum. Same codebase. Which is the point of multisite.

I have other issues with Servage at the moment (Not the fastest, and a troubling security vulnerability recently) but the features are great.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

superjacent’s picture

I'm also with Servage.net I posted this http://drupal.org/node/144643#comment-775981 (this thread) a few hours ago. I got a multi-site up and running but having problems with clean url's for the sub-domain site. Can you please advise on what method you used to point your sub-domains to your Drupal core and how you resolved clean url's.

Thanks in advance.
___________________________

Steven Taylor
Melbourne, Australia.
http://superjacent.net/cms

dman’s picture

As described above, I just did:
Control panel : Web Server : List Virtual Hosts

I dunno what side effects symlinks will have, or whether they behave the same all the time. Apache has a FollowSymLinks option or something that may or may not be enabled.
But illustrated above is my favorite option #1 - no question.

You'll see it even has Drupal in a subdir. That was not strictly neccessary ... but feels better.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

Note- the subdomains printed here are just placeholders that are actually falling back to 'default' so clicking won't be too enlightening. I have other working ones I didn't list in the screenshot, and yes, clean-urls is totally normal under them.

superjacent’s picture

Thanks for the advice and screen shot, that's made it a whole lot clearer. I've done away with .htaccess files redirecting to Drupal core and gone with the above Virtual Host thing. I've now got Clean Url's working with my sub-domain.

Next question. I'm now fiddling with the www thing, wanting to enforce only non www. Am I right that I only alter the .htaccess file that resides in the Drupal Core directory. This is a snippet from the Drupal Core .htaccess.

  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

If I have 5 different accounts, do I merely copy the above 5 times substituting for the correct accounts.

Once again, you've made things heaps easier.

___________________________

Steven Taylor
Melbourne, Australia.
http://superjacent.net/cms

dman’s picture

copy & paste is always a warning flag.
I'm sure there is a slightly better pattern you can come up with in the rewrite that will wildcard your domain in the replacement rule.

Play with that. mod_rewrite is deep magic, but when you get it you'll have earned a beer.
You want me to do it for you ... it'll cost more than a beer ;-)

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

superjacent’s picture

So at least I'm on the right track.

I've bookmarked a few Apache RewriteRules type sites and scribbled down four pages of notes. So even though I'm no longer going with the .htaccess redirect method it's been time well spent from a learning point of view. I'll see if I can nut out a pattern matching routine to account for all my sites.

Once again, thanks.

___________________________

Steven Taylor
Melbourne, Australia.
http://superjacent.net/cms

dman’s picture

Enable the RewriteLog in Apache for a while, or you'll be working blind. It's way scary informative when getting to grips with the arcane-ness.

Oh, that won't really be possible on most shared hosts - it's more of a local dev site thing.

Ah well, just an idea.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

jscoble’s picture

If you have a dedicated IP address at your new host, access your site on your new host via IP address.

If you do not, I would set the site up locally and implement the changes. Once done move your local site to new host and repoint your domain.

I usually use .dev instead of .com for my local sites. Most of your urls should be relative so it shouldn't matter if your site ends in .dev instead of .com. For situations where the TLD is important, I do edit the HOST file so that it has the actual domain name too but I always make sure that I comment it out when done. This approach keeps me from being confused.

meshackdotcom’s picture

Subscribed

Isaacheng’s picture

Hi pulsifier,

I tried your methods on five separate occasions and it was a few hours ago that i got it to work.

I did not know why it works now and not before, but it works.

Thanks.

....

I am writing this so that others may consider that if they cannot get it to work, it is possibly something else other than the code.

That's about the extent of my knowledge. Cheers.

river1’s picture

It seems to me that the problem here started at the installation stage. I just installed Drupal 6 by way of ftping the .gz file to my server under public_html folder. The installation extracted drupal under a new folder called "drupal-6.1/?" now in order in access my site I need to go to www.mysite.com/drupal-6.1?

Now can someone please help me figure out how to install drupal-6.1.tar.gz in a way that the site will be located under public_html and not in its own sub-folder?

Thanks,

"I get by with a little help from my friends"

superjacent’s picture

From within this thread - check this entry http://drupal.org/node/144643#comment-776336 and replies that follow. From your control panel or whatever it's called that you use to administer your site, simply point your domain to that sub-folder (drupal-6.1). Look for settings relating to Virtual Hosts or Document Root. After changing, you'll have to wait a few hours for the effect to take place. After it's done you will not have 'drupal-6.1' in your url.

I'm assuming that you use cpanel and that's not what I use so can't elaborate any further, which links to click etc.

___________________________

Steven Taylor
Melbourne, Australia.
http://superjacent.net/cms

dman’s picture

It's normal practice for application packages to unpack into an identifiable subdirectory. Not doing so is possible, but referred to as a 'tarbomb' as it can unexpectedly shower unknown files all over the place when unpacked uncautiously.
Most folk who can find their way around a file system can then inspect the files and place the distribution where it's needed.
Often, the unzip facility you use can give you the choice. What command did you use to unzip it?

For you now, you need to find out how to "move" the files in that subdirectory into the place you want them. That's up to your control panel, or your FTP or shell access.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

j-clark’s picture

It worked for me too. Thank you so much, and to pulsiter for starting the thread off.

I was having awful trouble debugging RewriteRules on a machine without access to the rewrite log, till I blindly copied this.

Moddy’s picture

This has worked perfectly for me thanks!

TonyTheTiger’s picture

Pulsifer,

I use godaddy as my host and they do not allow redirects off the root directory for their basic hosting plans. Thank you for your tip on using the .htaccess file to accomplish this task. The tip, plus a couple of hours going over apache documentation saved me some money.

Tony

bsenftner’s picture

godaddy must have changed their policy, because I have it working and documented here:
http://drupal.org/node/520700

cheers!

hepabolu’s picture

I've read through the entire thread and got it partially working. Here's what I have:

/httpdocs/oldsite/static.html
/httpdocs/drupal/
/httpdocs/drupal/gallery2/

Clean URLs and Gallery module enabled.

What I want is (a) make drupal appear as root and (b) add a bunch of redirects for the static site (i.e. /oldsite/somefile.html -> /drupal/node/X).

I've used the above configuration suggestions and came up with the rules below[1] (www.drupal.development is my local test site).
This works for all drupal urls, but not for the gallery part. All gallery urls show '/drupal/' in the url.

Also, if I enter a url with '/drupal/' in it, the correct page displays, but the '/drupal/' part remains in sight.

Finally, where do I put the static site redirects? Before the drupal manipulation code, in between somewhere or after?
update: the static redirects below all drupal related redirects work fine. They look like:
RewriteRule ^/?oldsite/some.html$ /node-name [R]

update: I haven't updated /drupal/.htaccess nor set the baseurl in sites/default/settings.php

[1] /.htaccess

# Check for /drupal-based URLs and remove /drupal
RewriteCond %{HTTP_HOST} !^www\.drupal\.development$ [NC]
RewriteRule ^/?drupal/(.*) http://www.drupal.development/$1 [R=301,L]

# Make drupal the home page
RewriteRule ^$ drupal/index.php [L]

# If an actual file exists in the drupal dir, prefer it
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]

# Otherwise change behind-the-scenes URL to drupal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

#Old pages are redirected to new nodes
RewriteRule ^/?oldsite/some\.html$  /content/newnode [R]
RewriteRule ^/?oldsite/other\.html$  /node/123 [R]

Thanks.

dgarzila’s picture

I am using godaddy as my hosting and I installed drupal in a subdomain that points to the root of that folder where drupal 6 is installed for that aliased subdomain.

Please help.

Can't get Clean Urls to work at all.

Should I have installed it as a subdirectory?

cog.rusty’s picture

I don't understand exactly how your site is set up, but I am not sure that you need any of the above. Can you explain your setup with an example?

Do you have a subdomain http://sub.example.com where you want to run Drupal?
Does that subdomain leads to a public_html/sub directory?
Do you have Drupal installed in the public_html/sub directory?
Have you uploaded Drupal's .htaccess file included in the tar.gz package?

If everything is as a described, try this: Edit Drupal's .htaccess file, find the line # RewriteBase / and remove the front # .

LegacyAtty’s picture

I had a hard time getting this to work. I could get the initial drupal page to load at www.mysite.com, but any subsequent link returned a drupal/ in the path and led to a non-existent page. I could manually remove the drupal/ from the url path and the page would load.

I subsequently found this thread. Before implementing this .htaccess coding, I contacted my service provider, WestHost, who suggested that I edit the httpd.conf file by changing the DocumentRoot and Directory settings to the directory I wanted. It worked! And clean urls are working fine.

For those who don't know (like me) the httpd.conf file was located in the etc/httpd/conf directory on my server. Now even though drupal is in a subdirectory, the base url points directly to that directory as the root.

cog.rusty’s picture

That was the right thing to do. The complicated .htaccess techniques discussed in this thread are a bit fragile and are intended for the poor souls on shared host who can't point a domain to the directory they want in any other way.

Even on a shared host, if the cpanel allows pointing a domain to the directory you want, that is a more solid way than these techniques.

kjv1611’s picture

I tried another method which was apparently incomplete to do the same deal as the original poster. So, since the other wasn't 100% complete, I figured I'd give this one a try. It worked like a charm. No issues so far.

By the way, I'm using anhosting/midphase for hosting of the quoted site. I suppose if I end up setting up any more sites underneath the same directory, I'll need to add them into the root .htaccess file. That won't be a big deal, I'm sure.

If anyone else here uses anhosting/midphase, and knows a way to do this with shared hosting there other than using the .htaccess file, it'd be great if you could point me down the correct path to do so. :0)

Regardless, it's working, and I'm happy.

tjerah’s picture

When I tried to update the DB script at http://example.com/update.php, it gave me error: "Page not found."

The solution: Add this line to .htaccess

RewriteRule ^update\.php$ /drupal/update.php [L]
aac’s picture

Subscribing!!!

---~~~***~~~---
aac

danfinney’s picture

Ok, you all seem smart enough to solve my problem. Any help you can offer would be much appreciated!!!!

I installed Drupal in a sub directory on my website to use as a Blog. http://www.danfinney.com/blog/

Everything is running and working well. I submitted the XML feed url to Google Webmaster tools as a Sitemap. Google successfully downloaded the feed but tossed the following warning:

URLs not followed
When we tested a sample of URLs from your Sitemap, we found that some URLs redirect to other locations. We recommend that your Sitemap contain URLs that point to the final destination (the redirect target) instead of redirecting to another URL.

HTTP Error: 301 (Moved permanently)
URL: http://www.danfinney.com/blog

I did some investigating and figured out what the problem was. The feed url http://www.danfinney.com/blog/rss.xml is indicating the base path of the site without the trailing slash:

alternateURL="http://www.danfinney.com/blog"
base href="http://www.danfinney.com/blog"

When you go to: http://www.danfinney.com/blog it redirects you to include the trailing slash: http://www.danfinney.com/blog/

I am using the Global Redirect module, but the "remove trailing slash" feature does not seem to work on the main directory.

I also tried to edit settings.php so that the feed would include the trailing slash in the urls, but my attempts were not successful, as you are not allowed to add a trailing slash to the base url.

I either need to change the base path to include the trailing slash, or I need the trailing slash removed. Can someone point me in the right direction?

kjv1611’s picture

Perhaps you can make the necessary changes in the .htaccess file? I'm not 100% sure myself, as I've only messed with that portion of things enough to get things done on an as needed basis on my own sites so far.

Otherwise, there are other modules that might help. For instance, I think there is one that is either in progress, or already available for the Google Sitemap or XML SiteMap idea. Try searching around the modules section of this site, or either over at www.drupalmodules.com for info.

Not to mention one other that worked really well for me the other week. The Path Redirect module worked wonders for a different situation of mine, but it might help just the same for this situation. I realize it wouldn't be the best case scenerio, in that you probably want to make sure it's fixed without extra code, but if it works, the site visitors won't care how it's fixed. ;0)

john.davies.nau’s picture

Here is what I did. I only have 1 site and no subdomains so I did not test this with them. I DO have other non-Drupal applications being served out of the same root, like this...

public_html/drupal/
public_html/foo/
public_html/bar/

In public_html/.htaccess:

Options -Indexes

RewriteEngine On

RewriteRule ^$ drupal/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ drupal/$1

In public_html/drupal/sites/default/settings.php:

$base_url = 'http://www.mysite.com';

Apache MUST have read+execute permissions on the public_html directory for it to work, othwise you get many access denied errors.

danfinney’s picture

John,

These instructions do work for removing the subdirectory. So in your case you would have mysite.com/drupal/postname and with your rewrite it would come out as mysite.com/postname

What I am trying to accomplish is to leave the "drupal" directory but remove the trailing slash.

Right now the Stories act this way, so mysite.com/drupal/postname/ becomes mysite.com/drupal/postname But what I am trying to accomplish is to have mysite.com/drupal/ become mysite.com/drupal

Can you lend a hand?

DL Dunning’s picture

I have the same sort of situation as you describe, John, so I tried your simplified approach and it's working just fine for me so far. I did not have the option of changing the Document Root with my hosting situation. Thank you, sir!

patriiiiiiiiiick’s picture

I like this simple approach. It seems to be working fine indeed. The only thing I am not able to do (both with this way of doing and with the more detailed one in this thread) is to visibly redirect the users going to the subdirectory. When I insert a line similar with
RewriteRule ^/?home/(.*) http://mysite.eu/$1 [R,L]
it goes into an infinite redirection loop. Can you get this working?

Thank you in advance for any help!

Patrick

mat8iou’s picture

I've been doing something with almost exactly the same code as this, which has worked well, for running 6 sites on a single codebase within a subdirectory. However, since upgrading to Drupal 7, it seems to have killed clean urls from working - previously they worked fine, but now, if I try to enable them, the setting box doesn't show up. If I upgrade with them already enabled, than all the pages are inaccessible using the clean url links within the admin menu in Drupal.

Does anyone know what changed between D6 & D7 that stopped this technique from working fully?

alladdin’s picture

I can confirm it doesnt work under D7.

mat8iou’s picture

Looking at the not found errors that I'm getting, it appears that it almost works, but that the first few letters of the clean url are truncated for some reason. E.g. there is a not found error for "n/config/search/clean-urls/check" When presumably the first part should have read as "http://sitename.com/admin/config/search/clean-urls/check" for instance.

I have no idea why this is happening now when it never did before with the same .htaccess files though.

rCharles’s picture

So much Noise. The Question and likely, simplest Answer for...

Primary Domain on Shared Hosting plan needs redirect to public_html sub-directory drupal install;
(while other "Add-On" domains use Cpanel "point and click" directory configuration), is:

public_html/shprimarydomain0-drupal/
public_html/aod1_example-drupal/
public_html/aod2-that_drupal/
public_html/aod3-this_drupal/

In public_html/.htaccess:

Options -Indexes
RewriteEngine On
RewriteRule ^$ shprimarydomain0-drupal/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ shprimarydomain0-drupal/$1

In public_html/shprimarydomain0-drupal/sites/default/settings.php:
$base_url = 'http://www.mysite.com';

Note that directory names can be nearly anything you want. Good luck.

dorys’s picture

I too need to change the url to just www.mysite.com. But before I totally screw up my site, I"m going to ask stupid noobie question. Do I just cut and past the exact code above and overwrite what is currently in the root .htaccess file?

Here is a copy of my code:

# Hostmonster.com
# .htaccess main domain to subfolder redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.

# Do not change this line.

RewriteEngine on

# Change yourdomain.com to be your main domain.

RewriteCond %{HTTP_HOST} ^(www.)mysite.com$

# Change 'subfolder' to be the folder you will use for your main domain.

RewriteCond %{REQUEST_URI} !^/drupal/

# Don't change this line.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Change 'subfolder' to be the folder you will use for your main domain.

RewriteRule ^(.*)$ /mysite.com/drupal/$1

# Change yourdomain.com to be your main domain again.
# Change 'subfolder' to be the folder you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.

RewriteCond %{HTTP_HOST} ^(www.)mysite.com$
RewriteRule ^(/)?$ drupal/index.php [L]

dorys’s picture

I went ahead and made one change to the core .htaccess file above to:

RewriteRule ^(.*)$ /mysite.com/$1

I the changed to IfModule XXXmod_rewrite.c>

But when I change the settings.php file by uncommenting the line # $base_url = "http://www.mysite.com'; I lose most of the style sheet formatting and access to all the nodes.

Any ideas?

Dorys

kjv1611’s picture

Just curious, as I didn't read this post until just now. Are you still having issues?

What I found best for me was to do at least one of three things always when making changes to the .htaccess file or php.ini file:

1. Create a backup copy of the file being edited (of course with a different name)
2. Document, Document Document. Document what/when you change, so that you can easily undo those changes if need be.
3. Instead of deleting lines of code, you can simply comment them out initially, so that if you find you DID need them after all, you can simply uncomment them.

dorys’s picture

Thanks for the response, but I've had to move on from this issue for the moment. I've recently moved from a PC to a Mac and I've been trying to copy my live site to the Mac using MAMP and I have huge issues here.

The trials and tribulations of a noobie.

Once I finally get my live site copied to my local computer, I will be re-visiting this issue.

dorys’s picture

Sometimes you just have to walk away for a while.

~d

ericpugh’s picture

I've tried several of the solutions in this post, without success. Is this supposed to work on ALL content of the site, or only the front page?
I am trying to do this with a subdomain, which (beyond my control) points at the root (public_html). Following the instructions above, sub.domain.com points to my front page of the drupal site (which lives in a subfolder.)

However, links to content with path alias don't work. (also images). For example, sub.domain.com works, but sub.domain.com/blogs/this-is-a-blog-post-node (this is a path alias) returns "Page not Found". In fact, I actually have another folder under public_html named "blogs" and it looks in there.

How have all of you been able to get this to work with the content on your site, or do all pages except the home page have to have the subfolder in their path?
(ie: sub.domain.com/drupalroot/blogs/this-is-a-blog-post-node)

cog.rusty’s picture

It is supposed to work on ALL content. However any little detail may matter. It is a bit fragile. A "poor man's" solution.

If you have a real "public_html/blogs" file or directory it is natural that the rewrites won't work, because of RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d. Delete or rename that directory.

Also make sure
- that you have disabled the rewrites inside Drupal's .htaccess file in the subdirectory
- that clean URLs are enabled in the ?q=admin/settings/clean-urls page.

If the menu links or the images don't work, pay attention to the paths where the links are trying to go, to figure out what the problem is. Rewrites can be hard to debug. Doing one thing less or one thing more than necessary everything can break.

tamlin’s picture

This almost works for me but when I'm not logged in and I point my browser to www.mysite.com/user
The menu links all point to www.mysite.com/drupal/node/X

Once I log in this displays correctly www.mysite.com/node/X
It also displays correctly when not logged in and browsing pages other than www.mysite.com/user

I'm trying to understand the mod_rewrite rules so that I can figure out why this is. Any help would be much appreciated.

Cheers
I'm using Drupal 5 on a shared server (Jumba.com.au)

P.S.
I also had dorys problem with loosing the style sheet and access images in www.mysite.com/drupal/images directory
I think this may have something to do with the base_URL setting in drupal/sites/default/settings.php

tamlin’s picture

Ok this is really weird!

I commented out the first two lines

RewriteCond %{HTTP_HOST} !^www\.windwanderer\.com$ [NC]
RewriteRule .* http://www.windwanderer.com/ [L,R=301]

And it fixed my problem on www.mysite.com/user.
Then I commented out then next line

RewriteRule ^$ windwanderer.com/index.php [L]

And quickly put it back again because it made the server try to give a directory listing when requesting the url www.mysite.com

Then I uncommented the first two lines again so my .htaccess file is exactly what it was when I had www.mysite.com/drupal/node/X showing up in the menu and... It's not there now!??

How can this be!!??
I haven't edited any other file.
I've logged in and out of my drupal site several times, but why should this make any difference?
I also uncommented

#Options -Indexes
#Options +FollowSymLinks

which I had commented for some reason, but even if I put those comments back I can't reproduce the error!

This is maddening! I've fixed it but I don't know HOW!! :-|

Well sorry to any others trying to get this to work. Keep persisting, it does work, somehow. Hopefully you'll stumble upon the magic as I did.

Now the last thing I'd like, but I'm not going to try to get it to work now, is that I used to be able to log in to my site twice; once at http://www.mysite.com and then with a different user at http://mysite.com This was great because Firefox would assume it was two different sites and I could make changes as the root user and see how they looked to an autonomous user without having to log out and back in again. I can't do this now.
Don't know why.
A problem for another day.

jessiet12’s picture

very cool! thanks for those!

_______________________
photography blog

tamlin’s picture

Ok I think I've figured out how this rewrite stuff works, it's still voodo but I now consider myself an apprentice as opposed to a total beginner.

Firstly Rewrite Rules are in the fomat RewriteRule Reg_Expression Substitution [Flags]
and Rewrite Conditions are in the format RewriteCond Test_String Cond_Pattern [Flags]

Here's the .htaccess directives explained as I understand them.

Turn off indexing so as not to allow browsing the files in your root directory
Options -Indexes
Turn on the rewrite engine
RewriteEngine on
Allow traversing Symlinks
Options +FollowSymLinks

The 1st Rule forces the www. i.e. yourdomain.com becomes www.yourdomain.com
It starts with has a condition that says: If the requested Host (i.e. domain name) is NOT www.mysite.com then rewrite anything to http://www.mysite.com/ stop processing and reload with a 301 "Moved Permanently" redirection code
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule .* http://www.mysite.com/ [L,R=301]

%{HTTP_HOST} is an apache environment variable for the the requested host. This can be www.yourdomain.com OR yourdomain.com OR I think even an ip address (haven't checked that though)
The "!" means NOT
The rest of the condition pattern is a standard regular expression (if you don't know about those please read up on them, they are very very useful)
The [NC] flag means No Case i.e. case insensitive.
The Rewrite Rule starts with a regular expression: "." means any character "*" means 0 or more times so .* means anything. This is fine because we've already specified our conditions with the RewriteCond. So anything that matches them we want to send to http://www.mysite.com/
The Flag "L" means "Last Rule" i.e. Stop here and don't process any more rules. The "R" mean "Reload" with the new request and send the user a 301 code.

Personally I got rid of this rule because I didn't like it. Feel free to make up your own mind.

This next rule is to send any requests to the root filesystem to drupal/index.php
RewriteRule ^$ drupal/index.php [L]
This rule says if there is a request for nothing in particular eg. the DomainName alone
which points to the root directory then redirect to drupal/index.php and stop processing here.

These next two lines say: If the request is for something that should be under the document root eg. where your domain Name actually points to (often public_html) but it is actually under public_html/drupal send it there
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
%{DOCUMENT_ROOT} is another apache variable that equals the full local filesystem path eg. /home/user_you/public_html
%{REQUEST_URI} is the requested URL that comes after the domain name e.g. with a request for www.mysite.com/photos/album.php
the %{REQUEST_URI} = "/photos/album.php" So What this condition is saying is that if the request www.mysite.com/photos/album.php is found here /home/user_you/public_html/drupal/photos/album.php and it's a regular file... Then fetch it.
The Rewrite Rule simply does so. It matched anything ".*" and rewrites it as drupal/what_was_matched The $0 = what was matched.
The [L] again means "Last Rule" stop processing here.

NOTE: My shared server runs apache 1.3 which does things slightly differently. I have to put braces around the bit I want to refer back to like this
RewriteRule (.*) drupal/$1 [L]
It also starts with $1 not $0

This last rule is for precessing the query strings. Somehow requests must come through for "?node/1" which is not a regular file or a directory.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]
The 1st condition says if the full local filesystem request is NOT a regular file
The 2nd condition says if the full local filesystem request is NOT a directory
Then
Send the request through as an argument to index.php

NOTE: Again my shared server does it differently. I have to have this
RewriteRule (.*) drupal/index.php?q=$1 [QSA]

Drupal's original .htaccess also contained
RewriteCond %{REQUEST_URI} !=/favicon.ico
I'm afraid I don't know what this was for. It seems to say if the request is NOT for "=/favicon.ico"
It works with it left in so I'd leave it there.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule .* drupal/index.php?q=$0 [QSA]

The last thing I'll say, and it's the last one because I should be asleep already is that I happened to have an installation of phpList also running and I wanted to keep it separate from the drupal files but still have it accessible. Here's the spell, I mean Code that did it.

## These rules are for accessing phplist
##
RewriteCond %{HTTP_HOST} mysite\.com [NC]
# If the hostname contains mysite.com (Note there is no "^" to mark the start so it can appear anywhere)
RewriteCond %{REQUEST_URI} /lists/.*
# if the part after the hostname is /lists/.*
RewriteCond %{REQUEST_FILENAME} -f
# and its a file
RewriteRule ^(.*)$ $1 [L]
# then let it through as it is

Hope I haven't got things too wrong and that this helps other dabblers in the arcane arts of mod_rewrite

djwesto’s picture

Hi @Tamlin,

I just wanted to thank you for taking the time to put together this super-clear and well-written basic guide on how all this actually works.

Judging by the number of frustrated comments, questions and un-answered cries for help on d.o about getting Apache rewrite commands, Clean URLs and everything else to actually play nicely together, these sort of instructions are much-needed and I am so grateful to you for putting them together.

As a complete Apache-noob it's taken me an awfully long time to get a relatively simple D7 site working on a shared hosting account. Now, thanks to you, it's hopefully going to actually happen.

Chris

nortont’s picture

I needed to access a subdomain at forums.mysite.com

to make this work I removed

RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule .* http://www.mysite.com/ [L,R=301]

Not sure what it does but it's working!

All good

busydoingnothing’s picture

After I change my $base_url to be $base_url = 'http://www.mysite.com';, when I attempt to go to my site while it's offline, I'll just get the "Site offline" page. I can't login, I can't administer, nothing. When I change it back, I can go to my www.mysite.com URL and see the administer page. What could be wrong?

busydoingnothing’s picture

I'm a total drupal newb. I went to mysite.com/user and was able to login.

southweb’s picture

Hi

Thanks for this thread. Some useful ideas. I tried everything though, and unfortunately did not succeed for my Drupal 6 install.

The closest I got was not havine base_url set, and having a .htaccess file in the subdirectory as well. Everything worked except galleries, yet the subdirectory still showed up in the path despite xxx.com/fubar and xxx.com/drupal/fubar working the same.

I think the give away is that the menu links were showing as xxx.com/drupal/fubar

sigh.

Ah well, keep hacking ...

kjv1611’s picture

Where is your site hosted? Is it on a box you own, or hosted via a hosting service? If with a service, you may can ask them to take a look at your .htaccess file(s), and make sure you have everything set correctly. I remember I had somewhat of a time getting mine working.

If it's important to you, just keep hacking away at it, eventually, surely it'll work out.

;0)

nuncium’s picture

Super clean and works great.

Thanks a lot

vonn.new’s picture

I have found this thread really useful and educational.
I've gotten this partly working, but not quite.

Here's the situation:
I'm installing a new Drupal 6.16 site into a subfolder called /drupal
I got the redirection described here to work so I can access the drupal site with http://mysite.com
(yay)

There are many flat html sites also hosted here and each is in its own folder. For example site2 is http://mysite.com/site2/index.html and out in the world, many links to it exist as http://mysite.com/site2 (i inherited this setup)

Unfortunately, Drupal takes this folder over and I get a 'page not found' error. I've been trying to tell Drupal to leave it alone with .htaccess, but this is what I haven't gotten to work.

Here's my root .htaccess file:

Options -Indexes
RewriteEngine on
Options +FollowSymLinks

RewriteRule /folder1 - [L]     # a subfolder Drupal should ignore

RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

I followed the directions above for the /drupal/.htaccess and settings.php files.

I'd really appreciate some suggestions here because I'm running out of ideas and suspect I'm missing something obvious.

Thanks.

cog.rusty’s picture

For static sites in subdirectories, forget about all the rewrites and specific directory names and just change

DirectoryIndex index.php

to

DirectoryIndex index.php index.html index.htm
vonn.new’s picture

Thank YOU!!!!! Wow, was I making this more difficult than it needed to be. Your help saved my day.

Joey96’s picture

The code shown in the message above worked perfectly for me. I merely had to put this code into the webserver root's .htaccess file and then tweak it for the directory and site names particular to my environment. It worked fine for both mysite.com and www.mysite.com (my drupal site files are in the /var/www/mysite directory).

One tip: be sure to get rid of anything in the root's index.php file that could conflict with the .htaccess file's actions. In my case, the root's index.php only contains the default PHP code that displays the "It works!" message that you see when you browse to the IP address for the webserver.

Many thanks to everyone here!

kjv1611’s picture

I'm trying to tweak my redirects and/or .htaccess file to handle multiple separate drupal-installed sites under the same hosting directory, and I think I just remembered or thought of something that will work for me.... and maybe, hopefully, it'll help with this issue, or at least someone else with similar issues... I've yet to test it, BUT it makes sense.

I'm assuming that apache or php or whatever will follow the logical path through an .htaccess file. So in other words, if it doesn't meet the 1st condition, it will go on to the latter condition, and so on. So....

In my situation, I've got a "main site", so it's at www.mainsite.com - (this is a psuedo address, in case anyone is wondering - I've not checked, and have no clue as to whether or not someone has something at that specific address or not, though I wouldn't doubt if they do). Then I've got 1 now, and possibly a few others down the road, under other subdirectories....

Well, mainsite.com is also the domain the whole web hosting package is registered with, so when I forward it's address, no big deal. But the others seem to want to go to mainsite.com/othersubdirectorysite....

So.....

If I put the conditions for the "others" up front, and then filter down to the "main" site, then I think I'll be able to get mine working correctly... and maybe that'll help in your situation? I don't know. The whole .htaccess thing still seems to be somewhat of a mystery to me.

I hope to give this idea - scattered as it is - a shot this evening..

Oh, when I say conditional, I'm talking about he opening line, "RewriteCond ..."

izmeez’s picture

subscribing

shiva7663’s picture

subscribing

Summit’s picture

Hi,

I hae already a working site on www.campings-europa.com.
I want to put a drupal website next to it in /drupal directory, but it needs to take over the homepage of the old site, but the suburls need to exist next to each other.

Complexity: The old website has already a .htaccess with a couple of rewrite rules..

How would I preceed please? I tried everything stated here, but adding drupal code gives me error 500 everywhere..

Thanks a lot in advance for your reply!
Greetings,
Martijn

nsincaglia’s picture

I have a very basic Drupal website that I installed in a subdirectory. I don't have any other subdomains or static websites to worry about. I followed the directions provided here and I am able to get the URL to be rewritten so that it hides the subdirectory. However, when I get this to work, my theme stops working. Can someone suggest a possible solution to this problem. I think I am close but I just can't seem to find a solution to fix this issue.

kjv1611’s picture

Can you copy/paste your .htaccess code here? Of course, remove the real addresses, etc, and replace with "fake" information if you want to protect any info about your site from the .htaccess file. Just glance over the text, and rename the folder names, domain names, etc.

I thought I had already posted this here, but I must not have. If you want to get some REALLY good advice, I think, on setting up the .htaccess file correctly, take a look here:
http://corz.org/serv/tricks/htaccess2.php

One of the folks on tek-tips.com found this site, and gave me the link, when I was dealing with some very annoying issues. One such issue was dealing with a custom theme, as well, so needless to say, when I used the methods described in this link, all the theme, domain/subdomain, etc issues all seemed to disappear!

kjv1611’s picture

Here's the tek-tips thread, in case it helps you to see another example:
http://www.tek-tips.com/viewthread.cfm?qid=1602845&page=1

nsincaglia’s picture

I just used the code from the first posting on this thread.

In the .htaccess file in my root directory I updated the code what is listed below.
Let's say my website URL is at the URL 'http://www.myawesomesite.com'. And my Drupal instance is installed in the 'drupal1' subdirectory. I modified the code as below.

Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.myawesomesite\.com$ [NC]
RewriteRule .* http://www.myawesomesite.com/ [L,R=301]
RewriteRule ^$ drupal1/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal1%{REQUEST_URI} -f
RewriteRule .* drupal1/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal1/index.php?q=$0 [QSA]

Then I changed the .htaccess file in my [root]/drupal1 subdirectory from to

Then I changed my settings.php file in my [root]/drupal1/sites/default from $base_url = 'http://www.myawesomesite.com:80/drupal1'; to $base_url = 'http://www.myawesomesite.com';

The URL rewriting seems to work now but the Theme (Obsidian) stops working. My theme is located in [root]/drupal1/sites/all/themes/obsidian

nsincaglia’s picture

I just used the code from the first posting on this thread.

In the .htaccess file in my root directory I updated the code what is listed below.
Let's say my website URL is at the URL 'http://www.myawesomesite.com'. And my Drupal instance is installed in the 'drupal1' subdirectory. I modified the code as below.

Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.myawesomesite\.com$ [NC]
RewriteRule .* http://www.myawesomesite.com/ [L,R=301]
RewriteRule ^$ drupal1/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal1%{REQUEST_URI} -f
RewriteRule .* drupal1/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal1/index.php?q=$0 [QSA]

Then I changed the .htaccess file in my [root]/drupal1 subdirectory from < IfModule mod_rewrite > to < IfModule XXXmod_rewrite >

Then I changed my settings.php file in my [root]/drupal1/sites/default from $base_url = 'http://www.myawesomesite.com:80/drupal1'; to $base_url = 'http://www.myawesomesite.com';

The URL rewriting seems to work now but the Theme (Obsidian) stops working. My theme is located in [root]/drupal1/sites/all/themes/obsidian

cog.rusty’s picture

Browse to the admin/build/modules page and let it load, to make Drupal update the module and theme paths. This may fix the problem.

nsincaglia’s picture

I was finally able to figure out a solution. I was never able to get the URL rewrites to work properly using the above method. The URLs would look good but my these would no longer work. I experimented for a while and it seemed like my entire drupal site was informed about the URL rewrites except for my theme. For some reason, the theme was still referencing the old URL.

The solution I used was I updated my DNS setting on my hosting provider (Network Solutions) from http://www.myawesomesite.com/htdocs to http://www.myawesomesite.com/htdocs/drupal1.

Once the DNS servers were notified of this change, I updated my .htaccess file in my [root]/drupal1 directory with the change 'RewriteBase /drupal1' to 'RewriteBase /'.

Then I updated my settings.php in my [root]drupal1/sites/default/settings.php from $base_url = 'http://www.myawesomesite.com:80/drupal1' to 'http://www.myawesomesite.com'

This seems like a much more straight forward solution than anything previously suggested in this thread. There may be some disadvantage to doing what I did but I am not aware of what they are.

Thanks for the help along the way!

cog.rusty’s picture

There is no disadvantage to what you did. Pointing a domain straight to drupal is the proper method.

This whole thread is for the folks on hosting plans which don't provide the option to point domains wherever you want.

big_smile’s picture

My site is hosted on Lunar Pages.

I tried all the tips in this page and found the instructions posted by john.davies.nau worked best.

I thought I would post this to help any other LunarPages users save time.

espirates’s picture

I can't get any of these to work
Internal Server Error every time, is there something else we are suppose to do to get this to work ?

jhood’s picture

I've been struggling to get this working with my Bluehost.com hosting. After much trial and error, I have this working.

Here is the new .htaccess file that I have in my root web folder (public_html/.htaccess for me)
Remember to change "yourmaindomain" to your domain and "subdirectory" to the subdirectory you would like your main domain to point to.

Options -Indexes

RewriteEngine on

# For security reasons, Option followsymlinks cannot be overridden.
# Options +FollowSymLinks
Options +SymLinksIfOwnerMatch

RewriteCond %{HTTP_HOST} ^yourmaindomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourmaindomain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www\.yourmaindomain\.com$ [NC]
RewriteRule ^$ subdirectory/index.php [L]
RewriteCond %{HTTP_HOST} ^\yourmaindomain\.com$ [NC]
RewriteRule ^$ subdirectory/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.yourmaindomain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdirectory%{REQUEST_URI} -f
RewriteRule .* subdirectory/$0 [L]
RewriteCond %{HTTP_HOST} ^www\.yourmaindomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* subdirectory/index.php?q=$0 [QSA]

As far as I can tell, this is exactly what I needed. Hope it helps someone!

espirates’s picture

Tried your example it didn't work, got server error.

jwillers’s picture

I've been struggling with this using Drupal 7 and BlueHost for hours. Nothing was working, but yours worked like a charm. Each other solution either gave me a 500 error or a 404 or kept the example.com/drupal/* in it.

FINALLY! THANK YOU!

jainparidhi’s picture

This is marvelous! Thank you so much! Been struggling for so long!

PJ

ehamburg’s picture

This worked for me with bluehost hosting. Thanks!

jasonabc’s picture

Have a site on Bluehost. Have the new site in a sub folder. The root is an absolute mess (tons of junk, big files and folders etc). Rather than having to archive it all and move Drupal to the root, I used this htaccess code to simply make the subfolder the document root for the primary domain. Worked like a charm - thanks!

sustainablecomputing’s picture

Hi,

I was able to get http://thehowleyfoundation.org/ to display my drupal site in the drupal dir.

I don't want duplicate urls at http://thehowleyfoundation.org/ and http://thehowleyfoundation.org/drupal/

Now I want to eliminate the http://thehowleyfoundation.org/drupal/ dir by redirecting to http://thehowleyfoundation.org/
which I still want to go the the drupal site.

Any one have any ideas? is this possible?
here is the redirect part of my .htaccess

RewriteEngine on

# change the host name to thehowleyfoundation.org
# no www. (or dev1.)
RewriteCond %{HTTP_HOST} !^thehowleyfoundation.org$ [NC]
RewriteRule .* http://thehowleyfoundation.org/$0 [L,R=301]

RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

bwooster47’s picture

+1 on this question - now that the root dir serves up drupal which actually resides in /drupal6, it would be nice if no external access to the /drupal 6 url was allowed.
Is that possible?
Maybe fail all ^/drupal6/.* accesses if the HTTP_REFERER is not the same web site?
Remove the drupal6/.htaccess file?

Just rewriting URLs to drupal6/$1 does not prevent users from getting same page using two URLs:
/... and /drupal6/... both will work, which seems bad.

Update: Easy way to do this, using .htaccess, using THE_REQUEST.

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /drupal6/ [nc]
RewriteRule .* - [nosubreq,redirect=404,last]

Along with appropriate addition of this rule:
RewriteCond %{ENV:REDIRECT_STATUS} !(403|404|500)
to prevent 500 internal server error related to infinite looping of internal redirects due to the 404 redirect. (To be safe, trapping 403 and 404 and 500, not just 404).

Full install is in /drupal6, so above rule disallows any direct URL access to that folder, but internal redirects won't be affected.

But - while the above thing works, the end-user gets a page saying "404 not found", it also reports "Additionally a 500 Internal Server Error" was seen. So, in the rules in previous comments, which redirect root accesses to /drupal6/* accesses, just add the ENV:REDIRECT_STATUS rule.

And ErrorDocument causes problems - not sure why Drupal .htaccess has an ErrorDocument for 404, since index.php internally handles missing pages. And the Drupal admin config for Error Reporting also seems to do nothing when changed to point to a different file. So, make ErrorDocument point to a static file, commenting it out may work fine, I set it to:
ErrorDocument 404 /404.shtml
in the drupal6/.htaccess folder.

All done, now with all this, things work great. Drupal is in a subdir /drupal6, but all accesses go through /. And, web search engines will never find duplicate content, so no degrading of search results.

Now if Drupal finally fixes the issue of moving index.php (wordpress for example, make it easy to just move the wordpress index.php to a top level directory and it just works), as reported in issues [#22269] and #22336: Move all core Drupal files under a /core folder to improve usability and upgrades instead of keeping on pushing that out (now to release 8.x), life would be even better.
That would remove the need of a lot of the redirection in this discussion, and there would be no longer any need to hide that drupal6/ folder since it would not have a index.php. Maybe someday...

izmeez’s picture

That would be nice.

@bwooster47

Now if Drupal finally fixes the issue of moving index.php (wordpress for example, make it easy to just move the wordpress index.php to a top level directory and it just works), as reported in issues [#22269] and #22336: Move all core Drupal files under a /core folder to improve usability and upgrades instead of keeping on pushing that out (now to release 8.x), life would be even better.
That would remove the need of a lot of the redirection in this discussion, and there would be no longer any need to hide that drupal6/ folder since it would not have a index.php. Maybe someday...
jspotswood’s picture

Does anyone know what the impact of this solution has on the use of robots.txt? When using a subdirectory for the Drupal install, would the robots file need to placed into the root for the website?
In other words, will public_html/drupal/robots.txt get used or ignored?

espirates’s picture

If you use the robot.txt module you don't need the robot.txt file.

mt3ch’s picture

Search engines access robots.txt through normal HTTP request so as long as your .htaccess is correct it will find the robots.txt at http://www.example.com/robots.txt.

You can test this by navigating to the file yourself through your browser. If you can see it, the search engine can see it.

nano8soft’s picture

I followed this example in setting up drupla in a subdirectory. i had drupal installed in Document_root/drupal and applied the code provided by Pulsifer.
it seems to work fine.
Thanks a lot Pulsifer

jamesrward’s picture

This seems like a lot more code then necessary. I would be interested in seeing a breakdown of each line and what specific case it is handling. For my sub-dir sites (I use /drupal as my sub-dir) I just put the following in the root dir .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !^/drupal
RewriteRule ^(.*)$ drupal/$1 [L]

and uncomment the /drupal/sites/default/settings.php base_url value:

$base_url = 'http://www.mysite.com';

No need to mess with the drupal .htaccess file. clean urls work fine like this and I have not seen any problems with my sites. I'm open to hearing any potential problems I am creating with this method but it seems to work fine for me.

izmeez’s picture

Since you uncomment the $base_url does that mean all your site file references in the database become absolute rather than relative?

jamesrward’s picture

If I understand correctly what you are asking the answer is no. For example images in a story can be referenced with or without the drupal sub-directory and still appear fine on the site, so:

<img src="/drupal/sites/default/files/images/myimage.jpg" />

and

<img src="/sites/default/files/images/myimage.jpg" />

Both work exactly the same.

As a side note the reason I prefer to setup my .htaccess file this way, aside from the nice short code, is so I don't need to make any changes for domain aliases. I can add domains at will without editing the .htaccess or the settings.php.

It just seems like a more domain agnostic approach.

dalareo’s picture

Great! It works perfectly. It also serves to mantain old URL associations, so there is no need to worry about search results and old url links. Thank you!

I have problems accesing to some files and directories out of drupal subdirectory!
I have a Elgg installation in another folder of my server and I have no problem accessing mysite.com/elgg, but I have other services (also into other folders) installed and when I try to access it appears a "page not found" message on the screen, inside my drupal.

dalareo’s picture

It does not allow to access other folders outside drupal. It is buggy as it allows to access some of them but not all. I cannot understand why.
I have an Elgg installation outside drupal folder and I can access it without problems from mysite.com/elgg. But other services hosted outside drupal folder are not reachable!

jamesrward’s picture

If you are referring to my example you would exclude directories with the following line added to the bottom of the .htaccess file:
RewriteCond %{REQUEST_URI} !^/(dir1|dir2|dir3)/.*$

This example would exclude directories called dir1/ dir2/ and dir3/ from being handled by Drupal. I have never experienced any bugs with this setup. I also put up a small tutorial about running Drupal in a sub-directory at:
http://www.wardnet.com/tutorials/running-drupal-sub-directory-htaccess

stesind’s picture

The above variants I tried did not work for me, don't know why. So I created my own using a tutorial. Here it is:

Options +FollowSymlinks
RewriteEngine On

#remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

#forward to drupal folder
RewriteCond %{HTTP_HOST} myip.com
RewriteCond %{REQUEST_URI} !^/drupal
RewriteRule ^(.*)$ drupal/$1 [L]

Do you expect any problems with it?

Branjawn’s picture

jspotswood’s picture

A number of the solutions posted worked for Drupal 6, has anyone got this working for Drupal 7. My attempts to do so have failed.

kheptan’s picture

what i have:
- domain on a shared hosting;
- the main webpage under /public_html;
- drupal installed on /public_html/drupal;
- on drupal settings file, activate base_url :www.mysite.ro;
- i also have clean url activated;
- put .htaccess on /public_html with a simple line : RewriteRule ^(.*)$ drupal/$1 [R=301,L]
- my links on drupal webpage are correct ex. www.mysite.ro/contact; www.mysite.ro/products;

now what i want is a simple redirect from
www.mysite.ro => www.mysite.ro/drupal
www.mysite.ro/contact => www.mysite.ro/drupal/contact; etc..
but if i put R=301 on htaccess my url address appear like www.mysite.ro/drupal witch i won't
remore R=301 url address it's ok but if i click on contact link or product link, url will be ok (www.mysite.ro/contact or www.mysite.ro/products) but nothing happen; only it will redirect to my home page

i also have another domain hosted on the same hosting server, the same configuration with drupal installed on a subdir (/public_html/drupal) and with the same settings everything works great, only differece is that i have drupal 5 installed not 7 like this one so i thing it's a drupal 7 problem

espirates’s picture

Drupal should include this capability in the core.

One reason is the main directory ie public_html is the public root directory and users most likely will be adding other items in there besides drupal. Making it easy to put drupal in subfolder ie /drupal, it keeps everything neat and tidy. It also saves a lot of confusion when having to upgrade and upload files to server. It's very easy to accidentally delete files in the public directly when it's full of drupal files.

If not in core then in a module, Wordpress does this so easily, we shouldn't have to touch the htaccess file.

jspotswood’s picture

@ espirates this is being worked on for Drupal 8. But given that it was possible to have the install under /drupal in Drupal 6, it would be nice to know what one has to do to accomplish the same in Drupal 7. Anybody?

jspotswood’s picture

Thanks to Stack Overflow, I have obtained an answer to this problem.
Reference http://stackoverflow.com/questions/5716839/how-to-remove-drupal-from-url....

espirates’s picture

I tried all of the above examples and all I got was a forbidden page. None of these work on the latest drupal.

kjv1611’s picture

avizzino’s picture

Hi.
First of all: thanks for sharing.

I followed your method and everything works ok...but now I can access my home page both from "http://www.mysite.com" and "http://www.mysite.com/drupal".
I would like to have the request: "http://www.mysite.com/drupal" redirect to 'page not found' (or to the home page http://www.mysite.com)

Maybe the answer is really simple, but I do not have much knowledge on the apache rewrite module, so your help would be extremely appreciated.

Thanks again.

A.

tk421jag’s picture

You have no idea how many sites I've read today about getting rid of that subfolder name in the URL. I've tried a bunch of things but I knew I was missing something in the subfolders .htaccess file.

Thank you for you're help!

Gham’s picture

Thanks for sharing this .. I'm another who's spent ages reading around trying to figure this one out.

Cheers

G.

Summit’s picture

Hi,
Can anyone please give the exact .htaccess method underneath which worked with drupal in a subdirectory but working through the homepage? I see so much links here above. Which version are you thanking all for?
Thanks a lot in advance!
greetings,
Martijn

iandale’s picture

I wanted to do something similar with my site and came across your post. I have a serious concern though about your method. Although it works like a charm, disabling the mod_rewrite in your "drupal" directory has a security issue.

One of the rewrite rules in the .htaccess file in the "drupal" directory restricts access to any dot files or dot directories (hidden file or folders with a preceding period), by disabling rewrite these files and directories can be be exposed if someone knows where to look.

Here is a quote from the .htaccess file in the "drupal" directory.

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # Block access to "hidden" directories whose names begin with a period. This
  # includes directories used by version control systems such as Subversion or
  # Git to store control files. Files whose names begin with a period, as well
  # as the control files used by CVS, are protected by the FilesMatch directive
  # above.
  #
  # NOTE: This only works when mod_rewrite is loaded. Without mod_rewrite, it is
  # not possible to block access to entire directories from .htaccess, because
  # <DirectoryMatch> is not allowed here.
  #
  # If you do not have mod_rewrite installed, you should remove these
  # directories from your webroot or otherwise protect them from being
  # downloaded.
  RewriteRule "(^|/)\." - [F]
cog.rusty’s picture

The directives in an .htaccess file are inherited by all its subdirectories. So, you can simply add that code in the .htaccess file in the parent directory, which enables the rewrite engine.

iandale’s picture

Agreed.

I just wanted to point this out so people don't have their hidden files and folders exposed if they don't add the line to the parent .htaccess file.

connellc’s picture

I have D7 and godaddy. It works! :-)

http://www.lakeshorespeech.com/

spyderpie’s picture

Good Advice, pinging so that it will stay on my dashboard. ;)

Peace,
Julie

yudarik’s picture

I've tried to do the same about an year ago, but without a seccess. What I've missed than was the modifications in the .htaccess file at /drupal and on settings.php

Now that I acheaved my goal, l able to access the site from both URLs: mysite.com and mysite.com/drupal which are both goes to the same index.php page.
what I'm afraid is that google will scan both URLs and will find duplicate content.

Should I add a rule on robots.txt to Disallow /drupal/ ? Or it does not mater since googlebot won't see the /drupal folder anyway?

Thanks.

ivadenis’s picture

It seems like a solution, however Redirect module isn't working for some reason...I'm on a Bluehost and module works on the addon domain.

Also, if the address is like: www.mydomain.com/drupal - redirects work too.

Any ideas?

Summit’s picture

Hi,

Could anyone please direct me to the exact issue which helps for the following use case:
Provider: Yourhosting ; multiple sites in public_html.

1) My site is a folder within public_html
2) When I add drupal to this folder which name is [sitename] The url which holds the drupal site is:
www.[sitename].com/[sitename]/user/login etc..

I want the sitename login be www.[sitename].com/user/login.

Thanks for your help in advance!
greetings, Martijn

Christopher James Francis Rodgers’s picture

I think this is as simple as logging into you webhost account,
going to the CPanel, finding the Domain manager page,
and directing/ pointing your original domain to start
at your new drupal site's folder.

So if it had been pointed at 'public_html',
then point it instead at the folder 'public_html/site-name.


All the best; intended.
-Chris (great-grandpa.com)
___
"The number one stated objective for Drupal is improving usability." ~Dries Buytaert *

Summit’s picture

Hi,

I see this in my public_html .htaccess
How can I change www.mysite.com to show instead of www.mysite.com/de ?

#RULE:,mysite.com,/
#RULE:www.mysite.com,/de
#RULE:www.mysite.com,/de
RewriteCond %{HTTP_HOST}    ^mysite.com$
RewriteCond %{REQUEST_URI}  !^//
RewriteRule (.*)    		//$1 [last]
RewriteCond %{HTTP_HOST}    ^www.www.mysite.com$
RewriteCond %{REQUEST_URI}  !^/de/
RewriteRule (.*)    		/de/$1 [last]
RewriteCond %{HTTP_HOST}    ^www.mysite.com$
RewriteCond %{REQUEST_URI}  !^/de/
RewriteRule (.*)    		/de/$1 [last]
sibiru’s picture

#In htaccess
RewriteRule ^$ mydomain/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/mydomain%{REQUEST_URI} -f
RewriteRule .* mydomain/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* mydomain/index.php?q=$0 [QSA]

#In setting.php
$base_url = 'http://www.mydomain.tld'; // NO trailing slash!

And I have multisite subdomain with this setting,
http://drupal.org/node/960934

but my sites/sub1.mydomain.tld is still in /public_html/mydomain/sites/sub1.mydomain.tld

ehamburg’s picture

This worked for me with bluehost hosting. Thanks!

SandPond’s picture

(1) I wish someone would summarize these posts and provide a current and revised procedure that is proven to work with Drupal 7.
(2) How much processing overhead is involved in all of this URL rewriting and redirecting? Maybe performance impacts are too expensive?

espirates’s picture

If it can't be done via admin like wordpress then it's a waste of time.

kjv1611’s picture

This is NOT an attempt to summarize everything in this thread, as that would be a surmountable task - feel fee to be the guinea pig on that, and put it all together yourself if you really want that. ;0)

this is my personal current suggestion based on my off-and-on experiences trying to work around in shared hosting for a few years... (I am far from being an expert)

If your webhost supports Add-On domains, I'd suggest you do this:

1. Point your domain registration for each domain to the same exact IP address where you have everything hosted

2. Don't even mess with the .htaccess file in the main directory unless you have to

3. Use the Add-On Domains feature in CPanel (or whatever other control mechanism is given)

When I quit fiddling with the .htaccess file (well, had to keep settings for one site), my performance jumped up for all other sites. Could have just happen at that given time, but that's what happened for me.

If it's for the "main domain" or the domain you have registered via a webhost for your hosting account, then that's where you'll still have to use .htaccess settings, I think. Either that, or you can use another cpanel setting... best I recall anyway.

If I remember later, I might try to get some more detail together, such as the current working .htaccess code I'm using for the "main" account associated with my shared hosting package. It's only 3 or 4 lines.

rCharles’s picture

If you have a shared host where Add-On domains or subdomains can be configured as subfolders of public_html BUT your primary domain name used for the shared hosting account is configured by default to use the public_html folder, this link explains it best.

https://drupal.org/comment/1611860#comment-1611860

Use these lines in the public_html/.htaccess file:

Options -Indexes
RewriteEngine On
RewriteRule ^$ d7primedomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ d7primedomain/$1

Then edit public_html/d7primedomain/sites/default/settings.php

$base_url = 'http://www.primarydomain.com';

Originally posted 2009.

Christopher James Francis Rodgers’s picture

This page's most recently posted comment is above at:

So much Noise. The Question
https://drupal.org/comment/8299947#comment-8299947

(Sometimes it is tough to track down 'new' replies)


All the best; intended.
-Chris (great-grandpa.com)
___
"The number one stated objective for Drupal is improving usability." ~Dries Buytaert *

alvilag’s picture

Thank you! Good shot! :)
On Drupal 7.x and Hostgator is working. :)

implementor’s picture

Thanks for your clear and very useful script.
For others, I thought that the following corz's primer was very useful:
http://corz.org/server/tricks/htaccess2.php?page=all

Till next time, m a r t i n

gilsbert’s picture

Hi.

Thank you all for the information posted.
The informations allowed me to elaborate a working solution for a multiste drupal7 instalation and I would like to share it here hoping to be useful for someone and to receive feedback.

The drupal's local directory is "drupal" and it is inside apache's web root.
There are others tools/solutions that must work without any changes. For example: limesurvey and wiki.

My drupal's sites may exist as subdirectories using symlinks to send it to drupal's directory as well as subdomains using DNS resolution and virtual servers in apache.

Two steps at apache's web root: 1 - .htaccess and 2 - robots.txt; plus one step at drupal's location subdirectory site/[your-primary-site].

You might complete or change what I'm using to adapt it better to your real situation.

.htaccess at apache web root

Options -Indexes
Options +FollowSymLinks
RewriteEngine On
# Drupal will answer as the first page only when there is none.
RewriteCond %{DOCUMENT_ROOT}/index.php !-f
RewriteCond %{DOCUMENT_ROOT}/index.htm !-f
RewriteCond %{DOCUMENT_ROOT}/index.html !-f
RewriteRule ^$ drupal/ [L]
# Allow apache feed the requester using the drupal's location if the file exists inside it and not outside it - slightly faster than send everything to drupal's script.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule ^(.*)$ drupal/$1 [L]
# Send to drupal's script everything that can't be feeded without it!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ drupal/index.php?q=$1 [QSA,L]

robots.txt at apache web root

Disallow: /drupal/ 

settings.php at drupal's location subdirectory "sites/[your-main-site]"

$base_url = 'http://[your-main-site]';
fairisle’s picture

Thank you, for your all codes

The code works good except the i18n(internationalization).
My site use three languages and sub-directory of public_html on shared server.
When I use only one language, it works great, but when I tried to use two more language, unfortunately it is not work correctly.
My problem is the redirection focuses only default language.

If I changed a language select, I got a "Page not found" error or display only default language.

I am not sure which is a problem between your .htaccess code and i18n.
I think i18n module has a problem, because when I used one language, there was no problem.

I used parts of your code on drupal's .htaccess.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule ^(.*)$ drupal/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ drupal/index.php?q=$1 [QSA,L]

and this

$base_url = 'http://[your-main-site]';

If anyone has solution about this, please let me know.

Thanks,

gippy’s picture

I have not been able to get this working with D8.0.0 beta. What I get is a blank white screen. It works fine with Drupal 7.35.
Two things: If I place info.php in the docroot of D8 (public_html/drupal/info.php) then that does display. So the request is being forwarded. Also, I am getting the blank white screen even when D8 is not the primary domain for Bluehost.

loyarc’s picture

I am using the following to serve a D7 site from a sub-folder off of web root. The problem is that when I change may Base_URL in settings.php as discussed above, (to 'http://www.mysite.com', for instance), all of my existing redirects break and generate 'Page not found' messages. Changing my Base_URL to www.mysite.com/2014 works, but then what's the point...

Here are my directives in HTACCESS. The site I am serving is in a directory named, "2014" :

Shared host
cPanel
Drupal 7.38

RewriteCond %{HTTP_HOST} ^www\.[mysite]\.com$ [NC]
RewriteRule ^$ 2014/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.[mysite]\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/2014%{REQUEST_URI} -f
RewriteRule .* 2014/$0 [L]
RewriteCond %{HTTP_HOST} ^www\.[mysite]\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 2014/index.php?q=$0 [QSA,L]

Any insight would be hugely appreciated.

UPDATE: Resolved. See https://www.drupal.org/node/1198028#comment-9804713

Kwb’s picture

Since I started using drush, I had the need to move my drupal installation to a subfolder. Now that I was able to make everything work again with drush, I would like to get back my original URL and avoid using mysite.com/drupal .
I have tried many changes to both root .htaccess and drupal-folder .htaccess but with no success.
By following pulsifer guide all I get is a 500 internal server error.
The best result is achieved by adding at the bottom of root .htaccess RewriteBase /: the site loads but it's EXTREMELY SLOW (due to the huge amount of redirects) and with no stylesheet.

I must say that when I had my drupal install into the root directory of my 1and1 shared hosting I had to change drupal .htaccess and uncomment RewriteBase / to make it work properly.

How can this be fixed?

susan5in7’s picture

No need to alter settings.php or .htaccess file. just point your domain to the directory where you have installed Drupal by using hosting's control panel addon domain feature.

Kwb’s picture

I tried looking into it but it seems there's no way to do that...
I don't know if this can be done through some php.ini parameter or what..

EDIT: The setting can be found under Domains -> Domain Management (not sure about the exact path as I'm translating from my own language)

mwithrow’s picture

Thanks for the solution.

stephen Piscura’s picture

I'm trying to accomplish this for Drupal 8, using specifically *one* subdomain, but with no luck.

Options -Indexes
RewriteEngine on
Options +FollowSymLinks

RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com$ [NC]
RewriteRule ^$ drupal8/index.php [L]

# Clean the /drupal8 from the URL
RewriteCond %{DOCUMENT_ROOT}/drupal8%{REQUEST_URI} -f
RewriteRule .* drupal8/$0 [L]
RewriteCond %{HTTP_HOST} ^subdomain\.mysite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal8/index.php?q=$0 [QSA]

Any good samaritans want to try and tell me what i'm doing wrong?

mat8iou’s picture

Not sure what exactly you are doing wrong.

What error is it giving when you try to load the site?

I've started a different thread for running domains from a sub-directory in Drupal 8 here:
https://www.drupal.org/node/2716043

I've had problems, but only occasionally. I think my re-write rules need more work though, as they don't actively force the removal of the sub-directory URL if it is typed in, whereas I would ideally like it to be only possible to access any page with a single version of the URL.

fngatia’s picture

I would like to thanks @pulsifer for this great useful helpful article Tancred 10(ten) years ago and still valid up to today. However i had a problem with the redirect rule. Am using drupal 7. This is the code that is being used to redirect:

1. RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
2. RewriteRule .* http://www.mysite.com/  [L,R=301]

Explanation: It means if the HTTP_HOST or the url is not www.mysite.com (NC means case insensitive), redirect to be served as a http://www.mysite.com. This will redirect all users to access the site WITH the 'www.' prefix. Futhermore if submit your url as http://mysite.com, it will be rewritten to http://www.mysite.com.

My problem now was when i submit a url like http://mysite.com/subfolder1/subfolder2/ or http://mysite.com/subfolder1/ it rewrites/overrides this urls and returns back to http://www.mysite.com. This is because of the conditions above.

However i manged to solve this problem as follow:

Instead of specifying the domain in line above i replaced with getting the requested/submitted url, added the 'www' preffix to it then returned the submitted url as shown with code below.

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

I would also urge pulsifer to replace the first two lines with above code. Now the whole code becomes:

Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^$ drupal/index.php	[L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

Thanks.

C.E.A’s picture

After hours and hours of test i can say the following (100% working):

Your main domain will use the public_html directory for all of its Web site files by default. Addon domains use sub directories inside the public_html directory.

In order to also set up your main domain to use a subdirectory on your hosting account you will need to set up a redirect in the .htaccess file in the public_html folder so that the server knows that any request for your main domain will be redirected to a subdirectory on public_html.

NOTE: for both methods below replace yourdomain with your actual domain name and the subdir with the name of subdirectory where drupal is installed.

FIRST METHOD: The subdirectory will not appear in the url, and the visitors will only see www.yourdomain.com

1. Create a .htacces file in the /public_html directory and add the following code:

Options -Indexes
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} !^www\.yourdomain\.com$ [NC]
RewriteRule .* http://www.yourdomain.com/    [L,R=301]
RewriteRule ^$ subdir/index.php    [L]
RewriteCond %{DOCUMENT_ROOT}/subdir%{REQUEST_URI} -f
RewriteRule .* subdir/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* subdir/index.php?q=$0 [QSA]

2. Goto the subdir/.htaccess, modify & uncomment:

# Rewritebase /drupal
to
Rewritebase /subdir

3. Do not modify anything in: /site/default/setting.php

# Chech if the below line is uncomment and comment it now because uncommenting this line will serve your site with no css style and a lot of page not found error.

# $base_url = 'http://www.yourdomain.com';

SECOND METHOD: The subdirectory will appear in the url as well, and the visitors will only see yourdomain.com/subdirectory


1. Create a .htaccess file in the /public_html directory and add the following code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourdomain.com$
RewriteRule ^(/)?$ index.php [L]

2. Goto the subdirectory/.htaccess, modify & uncomment:

# Rewritebase /drupal
to
Rewritebase /subdir

3. Do not modify anything in: /site/default/setting.php

# Chech if the below line is uncomment and comment it now because uncommenting this line will serve your site with no css style and a lot of page not found error.

# $base_url = 'http://www.yourdomain.com';