使用PHP我希望能够根据域更改特定HTML页面中的每个URL。
此代码允许我逐个更改网址,但我确信有更好的方法:
$domain = $_SERVER['SERVER_NAME'];
switch ($domain) {
case "example2.com":
$url1 = "http://".$domain."/secondsubfolder/thispage.html";
break;
case "example3.com":
$url1 = "http://".$domain."/thirdsubfolder/thispage.html";
break;
default:
$url1 = "http://example.com/firstsubfolder/thispage.html";
}
HTML:
<a href=" <?php echo $url1 ?> ">first url</a>
请注意,我还需要根据域更改第一个子文件夹,这就是为什么我不能使用相对网址。
所以我的目标是能够更改默认HTML代码中的每个网址:
<a href="example.com/firstsubfolder/thispage.html">hello</a>
<a href="example.com/firstsubfolder/somefolder/otherpage.html">bye</a>
如果通过example2.com访问我的网站,应该改变
<a href="example2.com/secondsubfolder/thispage.html" >hello</a>
<a href="example2.com/secondsubfolder/somefolder/otherpage.html">bye</a>
答案 0 :(得分:0)
你的问题对我来说不是很清楚,但我想你需要这样的话:
<?php
$domain = $_SERVER['SERVER_NAME'];
$scriptPath = $_SERVER['SCRIPT_NAME'];
switch ($domain) {
case "example2.com":
$domain = "example2.com";
$folder = "secondsubfolder";
break;
case "example3.com":
$domain = "example3.com";
$folder = "thirdsubfolder";
break;
default:
$domain = "example.com";
$folder = "thirdsubfolder";
}
$scriptPath = preg_replace('%^/.*?/%', "/$folder/", $scriptPath);
echo "http://{$domain}{$scriptPath}";