Forum Moderators: phranque

Message Too Old, No Replies

Changing A Domain's Root Directory To A Folder

         

Driklyn

3:56 am on Dec 31, 2005 (gmt 0)

10+ Year Member



Is it possible to change a domain's root directory to a folder instead of the main directory?

I just bought a domain, http://www.example.com, from Yahoo and I am using my cousin's paid hosting with it. The only problem is that my domain points to his website instead of mine. I need it to point to http://www.example.com/example/ but display http://www.example.com in the address bar. It needs to work exactly how it would with the /example/ in the address bar, just without it. Like, if you were to go to http://www.example.com/example/profile.php?id=andrew , I want it to go to the same page but with this url: http://www.example.com/profile.php?id=andrew . I don't just want a redirect.

Pretty much, I just want the /example/ to be out of the address bar even though the files are located in that folder. Sorry if this sounds confusing.

[edited by: jdMorgan at 4:27 am (utc) on Dec. 31, 2005]
[edit reason] No URLs, please. See Terms of Service. [/edit]

jdMorgan

4:30 am on Dec 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Whether you can do this depends on what 'tools' your host provides. If you can run mod_rewrite, that's one way to fix this, by doing a rewrite rather than a redirect, based upon the requested hostname.

It's also possible you may be able to use the existing site's control panel to define the new domain and its folder.

This is sort of half an answer, I'll try to add more later if time allows.

Jim

Driklyn

4:45 am on Dec 31, 2005 (gmt 0)

10+ Year Member



I already looked on my control panel and I don't think I can specify what folder the domain points to. I think I have to pay extra for that which I don't want to do. Also, I think I can run mod_rewrite. I tried some stuff with .htaccess before I made this post, but I only got it to redirect to the folder. I'm not quite sure of what code to use.

p.s. sorry about the links :(

Driklyn

6:35 am on Dec 31, 2005 (gmt 0)

10+ Year Member



oh yah, i also need it to only work for a certain domain, nothing else because there are other domains to the site that don't need to be messed with...

Driklyn

7:16 am on Dec 31, 2005 (gmt 0)

10+ Year Member



I was doing some searching on google and I found some code that works, but it doesn't take out the /directory/ in the url.

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.net
RewriteCond %{REQUEST_URI}!^/dir/
RewriteRule ^(.*)$ [domain.net...] [R=301,L]

When I go to www.domain.net, it displays www.domain.net/dir/. How can we get the /dir/ out?

Driklyn

7:44 am on Dec 31, 2005 (gmt 0)

10+ Year Member



sorry about all the posts...I would edit them, but the time expires...

I found this on the search...

------------

If abc.com is your original domain and xyz.com is the domain alias, and
you want people going to abc.com to see one site and people going to
xyz.com to see a different site, and you want the hostname to stay as
the user typed it, make a directory called xyz under /htdocs/www and put
the xyz.com website in it. Then use mod_rewrite in an .htaccess file in
/htdocs/www

RewriteEngine On
RewriteCond %{HTTP_HOST} xyz.com$ [NC]
RewriteCond %{REQUEST_URI}!^/xyz/.*$
RewriteRule ^(.*)$ /xyz/$1

Now when people go to abc.com they will see the site in /htdocs/www.
When people go to xyz.com, they will be served pages from
/htdocs/www/xyz and the URL in their browser will not change the
hostname or show them the /xyz/.

------------

I put this in:

RewriteEngine On
RewriteCond %{HTTP_HOST} domain.net$ [NC]
RewriteCond %{REQUEST_URI}!^/dir/.*$
RewriteRule ^(.*)$ /dir/$1

and it says it cannot find the directory, but if i put this in

RewriteEngine On
RewriteCond %{HTTP_HOST} domain.net$ [NC]
RewriteCond %{REQUEST_URI}!^/dir/.*$
RewriteRule ^(.*)$ /dir/index.php$1

it loads the index page but without any images or style sheets...I think this is because it is looking in the wrong directory. Although, the url does stay how it is supposed to and the links no longer contain the /dir/ in them, but none of the links work because it thinks the page is in www.domain.net/file.php, when it is really located in www.domain.net/dir/file.php; it is just supposed to display www.domain.net/file.php... Make any sense? :P lol...I should probably just wait for you to respond now, hehe....

jdMorgan

9:36 pm on Dec 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This 'optimized' code is correct and *should* work:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{REQUEST_URI} !^/dir
RewriteRule ^(.*)$ /dir/$1 [L]

However, because you tried something 'close enough' to this above, it probably won't change anything, and you will likely still get the 'directory not found' error, indicating that further adjustment is needed.

Plug in this code, request a page from your domain as a test, and then go look at your server error log file. It will likely indicate the adjustment needed to the local URL-path to get this working. Specifically, look at *what* directory path the error log says was not found, and compare that to what you typed in, plus the added '/dir/' path-part. You will likely find that there is extra path information in the directory it's trying to fetch, and that problem can usually be handled by using RewriteBase or by modifying the RewriteRule itself.

Added: I removed unneeded ".*$" tails and left your domain unanchored so the rule won't fail if a port number is appended to the request, which is perfectly valid. For example, a request might be for "example.com:80/page.php"

Jim

Driklyn

12:54 am on Jan 2, 2006 (gmt 0)

10+ Year Member



yah, it still does the same thing...how do i check my server logs?

Driklyn

3:00 pm on Jan 3, 2006 (gmt 0)

10+ Year Member



my server error logs on work on cgi scripts...

jdMorgan

8:02 pm on Jan 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like you need to have a talk with your hosting company. You really can't find and correc the problem easily without access to your raw server error logs. Just an opinion, but I would not use a host that didn't give me full access to all raw server log files...

Jim

Driklyn

9:58 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



i figured out that if you go to www.example.com/index.php, it works, but going to www.example.com doesn't work...not sure why, i tried 'DirectoryIndex index.php' but that didn't work and i also uploaded an index.html file to see if it was just loading .html files but it still didn't work when going to www.example.com....i also tried putting 'DirectoryIndex index.php' in the .htaccess file in the /deadend/ folder, but that didn't work either...

jdMorgan

12:57 am on Jan 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd hazard a guess that mod_dir is not loaded on your server.

Jim

Driklyn

1:39 am on Jan 10, 2006 (gmt 0)

10+ Year Member



isn't that what makes DirectoryIndex work? if it is, i should have it cause i uploaded 'DirectoryIndex post.php' to the main directory and when i went to www.example.com, it loaded post.php...i also tried it in the /deadend folder and it worked as well...

Driklyn

7:07 am on Jan 21, 2006 (gmt 0)

10+ Year Member



^bump^

i need to get it to rewrite from www.example.com to www.example.com/index.php...

Driklyn

3:47 am on Jan 27, 2006 (gmt 0)

10+ Year Member



anybody?

jdMorgan

4:38 am on Jan 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, but there's nothing obviously wrong with that code. Even your original code was 'close enough' that it should have worked for all except a few corner cases.

The problem is probably with your server. In addition to not allowing you to view your error log --a basic requirement to debug complex stuff like mod_rewrite and server-side scripts-- it seems to be mis-configured.

The options seem to be limited to:
a) Get your host to fix this.
b) Change hosts.

Sometimes we just run into a situation where code that should work doesn't work. And it is usually because the host has misconfigured something -- either by mistake, through ignorance, or because they believe it will make a shared server 'safer'. Without an error log to examine, there's just no way to find out which of these is the problem.

Jim

extras

4:15 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



Jim, I think you should put a slash at the end.
(I don't think it has something to do with his problem, though.)
If not a request for dirxyz.html wouldn't be treated by the rule, for example.

Your example:
RewriteCond %{REQUEST_URI} !^/dir

My suggestion:
RewriteCond %{REQUEST_URI} !^/dir/

And this is more efficient:
(By using %{REQUEST_URI} in the substitution, we can use the Regex for loop stopper.)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule !/*dir/ /dir%{REQUEST_URI} [L]

Driklyn

3:49 am on Feb 1, 2006 (gmt 0)

10+ Year Member



i tried your code, extras, but it still did the same thing. it didn't work when going to www.example.net but it didn when going to www.example.net/index.php

is there anyway I can just redirect it to index.php?

extras

3:25 pm on Feb 1, 2006 (gmt 0)

10+ Year Member



Maybe you just need to clear browser cache and/or reload pages.

For RewriteRule related issues, guessing with limited info is often pure waste of time for everyone, as many things can influence the outcome.

Actual URL tested, current .htaccess, and error.log output, for example, will help others to pinpoint the cause.

Also, you should be aware that some script will react badly to such setup, and you may need to modify the script or configuration.

Driklyn

6:50 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



is there any way that i can redirect www.example.net to www.example.net/index.php with the other code still working?