Link to home
Start Free TrialLog in
Avatar of moconn
moconn

asked on

Server Migration Absolute Vs Relative Paths [Apache / PHP / New Host / Mosso.com]



Hi experts.

I am am trying to migrate my site currently to a new Host (Mosso.com)

I have a site which has been coded to take advantage of relative paths
which was coded this way to ensure the code was portable and could
be placed on any server in any directory with little or no config..

On my previous server the absolute path was something like

/home/myaccount/public_html/

All our code was stored in the above folder in a folder called 'Test' so the
full path would have been /home/myaccount/public_html/test/

There was an alias setup so that when linking from index.php i could simply
use '/news/article.php'

On *Mosso, i see the abosulute path is something like:

/mnt/Target01/my_user_prefix/myUrl/web/content/

However i cant link in the same manner..

To rewrite all my links to abolute paths is a nightmare as you can imagine, and mosso will not setup such an alias..

Does anyone know of a way around this? I.e. perhaps a config file which i could emulate an alias to the
test directory so i would not have to insert code / rewrite the entire site?

Many thanks..
Avatar of smidgie82
smidgie82
Flag of United States of America image

I'm not understanding entirely...  /news/article.php is an absolute link.  Or was it actually /home/myaccount/public_html/test/news/article.php?

This is a *nix box, right?  If you're willing to play a little bit with sed, you can rewrite all the urls in a jiffy.

For instance,
sed -i 's:/news/article\.php:new_path/news/article.php:g' some_file.html
will search the file some_file.html for the regular expression "/news/article\.php", and globally replace all instances it finds with the text "new_path/news/article.php", saving the changes to the document.

To accomplish the same thing for all html files in the folder tree, use
cd /mnt/Target01/my_user_prefix/myURL/web/content/
find . -name '*.html' -print | xargs sed -i "s:/news/article\.php:new_path/news/article.php:g'

You can repeat that command as many times as you want for *.htm, *.php, *.asp, whatever.

But this will replace ALL instances, so if you have any links to other sites that include /news/article.php, they'll get changed, too.

You DEFINITELY need to backup your site before attempting to do this, though.

tar cfpz backup.todays_date.tar.gz *

will get the job done on the backup.  And if something gets messed up on the site,

tar xfpz backup.todays_date.tar.gz

will fix the problem.

This is assuming you have access to a command shell and the sed and tar programs, of course.
Avatar of moconn
moconn

ASKER


Hi there,

Thanks for your reply..

Unfortunatly we do not have shell access to our server..

We just need a simple elegant solution which involves changing as little code / files
as possible..

Is your site now at the root directory of your domain, or under some deeper directory?
e.g., www.example.com/ vs. www.example.com/test/

And what's the physical location of the file that you want /news/article.php to access?
Avatar of moconn

ASKER

Ok

The site is now at a deeper directory i.e.
www.example.com/test/

For example we need the file index.php in www.example.com/test/ to access
files such as:

/test/includes/header.php or
/test/includes/connect.php

I have a fuller description of the background to the issue below:

The website was written with absolute paths (see examples below).

The code was placed on our account directory on our previous host with a subdirectory of 'TEST'.

I believe it was an alias that was setup to allow us to specify which domains pointed to which directories on our account.

I.e. test.mysite.com -> Points to -> /home/our_account/test/
old.mysite.com -> Points to -> /home/our_account/old
live.mysite.com -> Points to -> /home/our_account/
www.mysite.com -> Points to -> /home/our_account/

As such we created a site using relative paths which could be transferred to any folder and would work instantly as long as we could point the domain to the correct folder. This allowed us to have a test / production and live environment on the same server and easily test each.

An example of our current code (index.php) is below:

<?
 
 // Do the PHPBB Session Stuff
 define('IN_PHPBB', true);
 $phpbb_root_path = './forum/';
 include($phpbb_root_path . 'extension.inc');
 include($phpbb_root_path . 'common.'.$phpEx);
 
 
 include './includes/connect.php';

As you can see from the above the code might be contained in the 'TEST' folder but we never had to include a reference to the test folder in our code.

If we wanted to go forward a directory we would use './folder/file.php' or back would be '../directory/file.php' etc as per web standards

If we were to do this now we would have to modify every file as follows: e.g.

<?
 include '/test/includes/connect.php';

If we moved the code to a different folder e.g. old we would have to modify all pages to include e.g. the following :

<?
 include '/old/includes/connect.php';
 

 Ok,

The website was written with absolute paths (see examples below).

The code was placed on our account directory on our previous host with a subdirectory of 'TEST'.

I believe it was an alias that was setup to allow us to specify which domains pointed to which directories on our account.

I.e. test.mysite.com -> Points to -> /home/our_account/test/
old.mysite.com -> Points to -> /home/our_account/old
live.mysite.com -> Points to -> /home/our_account/
www.mysite.com -> Points to -> /home/our_account/

As such we created a site using relative paths which could be transferred to any folder and would work instantly as long as we could point the domain to the correct folder. This allowed us to have a test / production and live environment on the same server and easily test each.

An example of our current code (index.php) is below:

Code:
 <?
 
 // Do the PHPBB Session Stuff
 define('IN_PHPBB', true);
 $phpbb_root_path = './forum/';
 include($phpbb_root_path . 'extension.inc');
 include($phpbb_root_path . 'common.'.$phpEx);
 
 
 include './includes/connect.php';
 
As you can see from the above the code might be contained in the 'TEST' folder but we never had to include a reference to the test folder in our code.

If we wanted to go forward a directory we would use './folder/file.php' or back would be '../directory/file.php' etc as per web standards

If we were to do this now we would have to modify every file as follows: e.g.

Code:
 <?
 include '/test/includes/connect.php';
 
If we moved the code to a different folder e.g. old we would have to modify all pages to include e.g. the following :

Code:
 <?
 include '/old/includes/connect.php';
 

I am sure there is a simple way around this using a single configuration file, and would be grateful in anyone could point me in the right direction..  







I must be misunderstanding something, still...  Lemme talk my way through it, and you correct me if I get something wrong.

You have three versions of your site that run concurrently.

In /home/public_html/test, you have a development version, accessible logically via www.example.com/test/.  On the old site, this was accessible via test.example.com
In /home/public_html/old, you have legacy code, accessible logically via www.example.com/old/, and on the old site was accessible via old.example.com
In /home/public_html/, you have the live, public site, which is accessible via www.example.com/.

You have certain things within each site version that you need to link to:
/home/public_html/news/*, replicated as /home/public_html/test/news/*, and /home/public_html/old/news/*
/home/public_html/include/*, replicated as /home/public_html/test/include/*, and /home/public_html/old/include/*
/home/public_html/phpbb/*, replicated as /home/public_html/test/phpbb/*, and /home/public_html/old/phpbb/*

And you want to be able to link to each resource in such a way that when you've reached a stable code version in /test/ you can copy the live version to /old/, and the /test/ version to / and have everything still work, right?

If all the links are relative already, there should be no problem.  e.g., if all links from www.example.com/test/news/somepage.html use ../otherpage.html to get to www.example.com/test/otherpage.html, then you're good.

If not, all is not lost.  If your webserver is running Apache and has .htaccess files enabled, you can specify rewrite rules in a .htaccess file to solve this problem.  Specifically, something similar to the following lines, when placed in the /home/public_html/.htaccess file should solve your problem:

# Make sure mod_rewrite is enabled
RewriteEngine On
# Specify the condition for rewriting URLs -- the HTTP referer tag starts with www.example.com/test/ (case-insensitive match)
RewriteCond %{HTTP_REFERER} ^www.example.com/test.*$ [NC]
# and also match only if the request isn't going to /test/something
RewriteCond %{QUERY_STRING} ^!(/test.*) [NC]
# If we get a request for www.example.com/something as a referral from www.example.com/test/somethingelse, rewrite the request to
# send out www.example.com/test/something instead of www.example.com/something
RewriteRule ^(.*)$ http://www.example.com/test/$1 [R,L]

You'd want to do some extensive testing to make sure the config works correctly, but is this sort of like what you're looking for?
Also, the rewrite stuff given above only works dealing with URL rewriting.  For PHP include statements, I don't believe there are any transparent config changes you can make to convert absolute to relative.  Since absolute PHP include() paths are relative to the system root rather than the document root, you're going to have to change all of them anyway, since your document root has changed.  This will be true regardless of whether the logical structure of your site has changed relative to your document root.  Relative PHP include() paths should work as before without any modification, so long as your logical site structure hasn't been modified.
ASKER CERTIFIED SOLUTION
Avatar of smidgie82
smidgie82
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial