How do I set base URL for all pages of my website?

2019-01-25 12:37发布

How do I set a base URL for my website and get it to include in every page?

Is there a way for me to easily change a variable to be the base url for the website, such as <?php $baseurl = "http://www.website.com/website/"; ?>, and include this on every page so that all CSS, JavaScript, images and PHP includes follow this $baseurl?

标签: php url
3条回答
该账号已被封号
2楼-- · 2019-01-25 13:00

You can’t make both PHP and client-side assets use the same base URL, unless you use PHP to echo a base URL variable or constant to the page.

The usual approach is to have a bootstrap file that you include on every page, and define your base URL and other site-wide variables in there.

bootstrap.php:

<?php
    define('BASE_URL', 'http://example.com');

index.php:

<?php
    include('bootstrap.php');
?>
<!DOCTYPE html>
<html>
  <head>
    <!-- // -->
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
  </head>
  <body>
    <!-- // -->
  </body>
</html>
查看更多
别忘想泡老子
3楼-- · 2019-01-25 13:04

(xampp), the How do I use on the local computer. folder layout,

http://www.resimagaci.com/img/90rvnrf.png

ust.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
</head>
<body>

alt.php

</body>
</html>

sabitler.php

<?php
#sabitler
define('BASE_URL', 'base-url');
?>

index.php

<?php
include 'kutuphane/sabitler.php';
?>
<?php
$ust= BASE_URL . '/kutuphane/ust.php';
$alt= BASE_URL . '/kutuphane/alt.php';
?>
<?php
include ($ust);
?>
<?php
include ($alt);
?>
查看更多
乱世女痞
4楼-- · 2019-01-25 13:06

You may want to take a look at the html base tag.

Inside the <head> section of your html, put

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

On top of that, you may want to have a base.php with default directories and whatnot that you include into your project.

查看更多
登录 后发表回答