php 如何为我网站的所有页面设置基本 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15481629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:18:45  来源:igfitidea点击:

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

phpurl

提问by user2183116

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

如何为我的网站设置基本 URL 并将其包含在每个页面中?

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?

有没有办法让我轻松地将变量更改为网站的基本 url,例如<?php $baseurl = "http://www.website.com/website/"; ?>,并将其包含在每个页面上,以便所有 CSS、JavaScript、图像和 PHP 包含都遵循此$baseurl

回答by Martin Bean

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

您不能让 PHP 和客户端资产都使用相同的基本 URL,除非您将 PHP 用于页面echo的基本 URL 变量或常量。

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.

通常的方法是在每个页面上包含一个引导文件,并在其中定义基本 URL 和其他站点范围的变量。

bootstrap.php:

引导程序.php:

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

index.php:

索引.php:

<?php
    include('bootstrap.php');
?>
<!DOCTYPE html>
<html>
  <head>
    <!-- // -->
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
  </head>
  <body>
    <!-- // -->
  </body>
</html>

回答by castis

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

您可能想看看html base tag

Inside the <head>section of your html, put

<head>你的 html 部分,把

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

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

最重要的是,您可能希望有一个base.php带有默认目录的目录以及包含在项目中的内容。

回答by Mustafa YILMAZ

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

(xampp),如何在本地计算机上使用。文件夹布局,

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

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

ust.php

.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

sabitler.php

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

index.php

索引.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);
?>