Forum Moderators: phranque

Message Too Old, No Replies

Redirecting root level requests with htaccess

         

jasonhollett

6:55 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



Hi everyone,

I'm trying to setup a .htaccess file to handle all url's with a / at the beginning. I don't want to be using ../ requests within my sites. I want all requests to start from the root folder. For examples "/images/bg.jpg" or whatever.

Problem is my sites are set up within sub-folders, so any requests start outside of the websites folder.

My server root folder is,
192.168.0.80/Web/

Within there is a folder called
192.168.0.80/Web/mysite

So if I have a link such as <a href="/about-us"> it looks for "192.168.0.80/about-us", which of course won't work cause it needs to go to "192.168.0.80/Web/mysite/about-us"

I having no luck getting anything to work and any help would be greatly appreaciated.

Thanks!

jdMorgan

7:17 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is the browser that resolves page-relative and server-relative URLs on your pages. These requests are always sent as full URLs to your server -- Otherwise they would never reach your server.

You must put the correct URL in the link on your pages.

If you have full control over the server, put the correct path to DocumentRoot in your server config file, and this will avoid the whole problem. It seems that the DocumentRoot path is currently missing the "/mysite" path-part.

Jim

jasonhollett

7:43 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



I have full control over the server, though if I set the DocumentRoot then it will only work for that one instance. This is development server so it needs to work for any number of sites.

The DocumentRoot needs to stay as is, but any folder created with the root needs to become the root for that site.

jdMorgan

7:51 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> any folder created with the root needs to become the root for that site.

That is not clear.

Are you simply trying to rewrite to a subdirectory based on the requested hostname? If so, there are many threads here on that subject.

Jim

jasonhollett

8:08 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



I am trying to rewrite to a sub directory, but the hostname will always be the same.

So say my server has the following folders,


/Web
/site-1
/site-2
/site-3

If I have a page within /Web/site-1 and the link is "/about-us" that needs to go to /Web/site-1/about-us, not outside of the "site-1" directory as it does presently.

I haven't had any luck finding any specific answers yet, and I've Googled it to death.

jdMorgan

8:29 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the hostname is always the same, what information in the URL do you intend to use to select the proper file subdirectory?

Jim

jasonhollett

8:38 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



I'm not sure what info in the URL I need to use. The only information out of the URL that I could see being useful is the websites directory name that is in the /Web folder.

jdMorgan

9:33 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, it's not at all clear how your server resolves these as different sites, then, if all the hostnames are the same. The usual approach is to use the requested hostname to resolve to a directory, e.g.

hostname/filename ......... filepath below root
www.site1.com/filename --> /site1/filename
www.site2.com/filename --> /site2/filename

Here, part of the hostname "www.site1.com" is used in the rewritten URL as the subdirectory. This can be done directly, as shown, or associatively, using a lookup techniique if the hostname does not directly match the directory name.

If the hostnames are all the same, then once a relatively-linked URL resolves to your server root, there is no longer any information to figure out which subdirectory to point it "back" to.

I suspect there is some degree of mis-communication here, and that I am not understanding how your server is set up, and the URLs that you use to access these "sites" that you describe.

Jim

[edited by: jdMorgan at 9:36 pm (utc) on July 28, 2008]

jasonhollett

9:48 pm on Jul 28, 2008 (gmt 0)

10+ Year Member



This is just an in-house development server, there are no domains being used. A site is accessed at 192.168.0.80/Web/[site directory name]

I'm hoping to set it up so it mimics a live environment and I can use URL's like the ones I described above with a beginning /. I'm not sure how else to explain it.

jdMorgan

10:11 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to mimic a live environment *and* have multiple sites 'work' at the same time, then set up the server using <VirtualHost> containers in httpd.conf for each domain, and then define those domain names in your workstations' hosts files (which serves as a local DNS lookup). Otherwise, you'll have to change the code every time you want to work on a different site.

As it stands with no info available in the requested URL to identify which 'site' the request came from and should be 'corrected' for, there is no solution that does not involve hard-coding the subdirectory path -- thus requiring you to change it whenever you want to work on another site.

The basic problem is that the server is not set up to support the environment you need. Anything short of fixing that problem is just a band-aid/work-around/kludge.

Jim