[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

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..
0
moconn
Asked:
moconn
  • 5
  • 2
1 Solution
 
smidgie82Commented:
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.
0
 
moconnAuthor Commented:

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..

0
 
smidgie82Commented:
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
moconnAuthor Commented:
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..  







0
 
smidgie82Commented:
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?
0
 
smidgie82Commented:
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.
0
 
smidgie82Commented:
Actually, it appears I was wrong...

If you have access to the .htaccess file I mentioned earlier, you can specify PHP ini settings for that directory within the .htaccess file as follows:

php_value ini_directive value

So if you wanted to, you could, say, specify

php_value auto_prepend_file filename.php

to prepend the given file to EVERY PHP file opened under that directory as though it were included via an include() statement on the first line of the file.  Doing so you could conceivably trap calls to include() and redirect absolute URLs to the appropriate ones.  I've never done it, though, and it seems like it'd probably be more effort to implement and debug properly than just changing all the filenames, which could be done fairly easily using the find / sed method.  There are Windows ports of both utilities, so you can download all your files locally, run sed on them, and upload again without needing access to a command shell on the server.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now