Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Mod Rewrite, Relative Links And Document Root

This is a discussion on Mod Rewrite, Relative Links And Document Root, within the Apache/IIS section. This forum and the thread "Mod Rewrite, Relative Links And Document Root" are both part of the Hosting Your Website category.

 
Reply to this topicStart new topic
> Mod Rewrite, Relative Links And Document Root
djeyewater
post Apr 23 2008, 04:37 AM
Post #1


Squeezing
***

Group: Members
Posts: 58
Joined: 18-February 08
Member No.: 169



In my .htaccess file I have set a custom error page for certain errors using ErrorDocument. But the path to the custom error page is relative to the Document Root, which is different between my local testing environment and my webhost. Is there any way I can set the Document Root using .htaccess?

Also, I am using htaccess to rewrite URLs, but this messes up my relative links to js, css and image files. I don't really want to use absolute URLs as this will mean they will all need changing when I upload the files to my webhost. I was thinking that if I could set the Document Root for the site then I could rewrite these URLs as absolute URLs. Does anyone have any suggestions for solving this problem?
Go to the top of the page
 
+Quote Post
c010depunkk
post Apr 23 2008, 08:06 AM
Post #2


Rapid Squeezer
****

Group: Advisors
Posts: 176
Joined: 14-February 08
From: Willich, Germany
Member No.: 56



I had a similar problem and solved it with PHP. I wrote a function which returns the absolute path of the file:
CODE
function getLinkPrefix() { // get link prefix
    $path='';
    $p=explode('/',$_SERVER['PHP_SELF']); // explode path
    foreach($p as $cp) {
        if(!preg_match('#\.(?:php(?:3|4|5)?|(?:x)?htm(?:l)?|cgi)$#i',$cp)) { // is current piece a file?
            $path.=$cp.'/'; // add to path
        }
    }
    return 'http://'.$_SERVER['HTTP_HOST'].$path; // return host plus path
}

Then store the value in a variable:
CODE
$link_prefix=getLinkPrefix();

Then you can do links/css/js like so:
CODE
<style type="text/css" media="all">@import "<?php echo($link_prefix); ?>code/css/core.css";</style>
<script type="text/javascript" src="<?php echo($link_prefix); ?>code/js/jquery-1.2.3.pack.js"></script>
<a href="<?php echo($link_prefix); ?>portfolio">PORTFOLIO</a>


--------------------
www.c010depunkk.com ~ the hangout of a web developer
Go to the top of the page
 
+Quote Post
ejg
post Apr 23 2008, 08:36 AM
Post #3


Squeezing
***

Group: Members
Posts: 75
Joined: 14-February 08
Member No.: 86



I don't know how to do it in the .htaccess file but I use the <base> tag in the html head to specify my document root.
Go to the top of the page
 
+Quote Post
rewake
post Apr 23 2008, 08:47 AM
Post #4


Rapid Squeezer
****

Group: Mentor
Posts: 223
Joined: 14-February 08
From: NY, USA
Member No.: 127



Hi djeyewater,

It seems like everything is a bit over-complicated. My suggestion would be to modify the apache config on your testing server to match the one on your host - at least on that particular apache virtual host. I would also strongly recommend using references relative to the document root. This will help you out with the path problems using mod_rewrite... you always know where you're starting from with a root path reference.

Hope this helps!
Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
djeyewater
post Apr 25 2008, 01:55 PM
Post #5


Squeezing
***

Group: Members
Posts: 58
Joined: 18-February 08
Member No.: 169



Thanks for the suggestions everyone, I think it will probably be best if I do as Rich suggested and set my local apache config to match that of my webhost.

Time to do some reading on apache virtual hosts!
Go to the top of the page
 
+Quote Post
djeyewater
post Apr 30 2008, 03:28 PM
Post #6


Squeezing
***

Group: Members
Posts: 58
Joined: 18-February 08
Member No.: 169



Just a quick question following on from this. When I set up my virtual host on my local machine, what should I set the domain for the site I'm testing to? If I set it to the same name as the actual site, then I won't be able to access the actual site from my machine.

Also, when setting absolute paths should they be in the form http://www.mysite.com/js/global.js or just /js/global.js?

Thanks

Dave
Go to the top of the page
 
+Quote Post
rewake
post Apr 30 2008, 06:32 PM
Post #7


Rapid Squeezer
****

Group: Mentor
Posts: 223
Joined: 14-February 08
From: NY, USA
Member No.: 127



Hi Dave,

I just use IP's because it's a lot easier. When you set a domain in your <VirtualHost> you also need to modify the HOSTS file on your local machine to point to the right IP on your local network (unless you have your own DNS server running, but that's a different story!) I typically reserve a certain block of IP's on my network for testing servers. For instance 192.x.x.50 thru 192.x.x.99. This would give me 49 different sites I could play with.

As for absolute paths, I use /js/global.js.

Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
djeyewater
post May 2 2008, 04:07 PM
Post #8


Squeezing
***

Group: Members
Posts: 58
Joined: 18-February 08
Member No.: 169



Thanks for your help with this. I've got both IP and name based virtual hosts working now. What do you find easier about IP based virtual hosts than name based?
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   3 temi 382 14th February 2008 - 09:15 AM
Last post by: Simon
No New Posts   0 geti 312 14th February 2008 - 03:50 AM
Last post by: geti
No New Posts   6 unitedcraig 262 21st February 2008 - 09:21 AM
Last post by: thesealportalteam
No New Posts   9 Sharron 294 28th February 2008 - 11:28 AM
Last post by: Sharron
No New Posts   3 Mark 200 6th March 2008 - 11:10 PM
Last post by: Monie