简体   繁体   中英

fix preceding forward slash in php

I am trying to create a local copy of a website by installing the same XAMPP version and downloading the files and mysql.

On the web server the files are saved at http://www.website.com/html but it is accessible through website.com

I made my own in this directory http://localhost/website.com/html

I am having trouble with the script because all the css and javascript have a preceding "/"

src="/js/prototype.js"

I cant explain it very good but this is a sample problem

The script originally have this

<link href="/_design/_master.css" rel="stylesheet" type="text/css"> 

That doesn't work on my local install but works on the online version. But if I remove the first / on my local install it works.

<link href="_design/_master.css" rel="stylesheet" type="text/css">

This link also works on the online version

<a id="logout" href="/logout.php">logout</a>

But on my local version it links to only http://localhost/logout.php but it is supposed to be linking to http://localhost/website.com/html/logout.php

When I remove the first / it works how ever there are so many of them and I might miss some.

Is there a setting for php for fixing this? Or did I put it on the wrong directory? Please help. Thanks!

Ah that is the problem. When you have / as first character it assumes you are starting from the root directory, but on your local machine you don't keep it in the root directory. But if you remove the slash / it should work on both your computer and on the real website.

first wrote: (

substr($string, 1)

That function will return the string without the first character, in your case a slash

Edit: It should have been $string , I forgot variables in php have $

)

Yes it is. Use absolute paths. Define a constant that holds the base url.

define('BASE_URL', 'http://mysite.com/');

and in includes use

$src = BASE_URL.'/js/prototype.js'

you can also check with preg_match if it has / at the begining and use substr() to substract it or use preg_replace()

$src = preg_replace('#^\/$#m', '', $src); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM