Forum Moderators: coopster

Message Too Old, No Replies

Need to rewrite a URL using just PHP

         

londrum

4:40 pm on Aug 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi.
i've got a little problem that i'm hoping you can help with. i need to rewrite a url, but i don't have access to the .htaccess file (lousy webhost!), so i can't use mod_rewrite.

i have a <base href="" /> tag on all of my pages, which i fill in with php...

<?php echo'<base href="http://'.$_SERVER['HTTP_HOST'].''.$_SERVER['REQUEST_URI'].'" />';?>

this code works absolutely fine, and i end up with something like this:

<base href="http://www.example.com/" />

the problem comes when people visit the site through http://example.com, rather than http://www.example.com.
because the base tag fills up with this...

<base href="http://example.com/" />

it's not much of a problem, granted, but i was wondering if there was a way to rewrite the above code so that it adds in the www.

or, even better, is there a way to rewrite the actual url itself just be using php?

ayushchd

4:59 pm on Aug 5, 2007 (gmt 0)

10+ Year Member



<?php echo'<base href="http://www.'.$_SERVER['HTTP_HOST'].''.$_SERVER['REQUEST_URI'].'" />';?>

This should do. If not, revert back

londrum

7:27 pm on Aug 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i forgot to mention one important detail... i can't hard-code the www into it, because then it messes up the links on my localhost server.
so i need something that will either display
[localhost...]

or
http://www.example.com/

but not
http://example.com

i know i'm probably asking for the moon here

londrum

7:34 pm on Aug 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



actually, i've just sussed it out

<base href="http://<?php
if(($_SERVER['HTTP_HOST'])=='localhost'){ echo'localhost/'; }
else { echo'www.example.com/'; }
echo $_SERVER['REQUEST_URI']
?>" />