带有静态htmls的Php url缩短器
我的访问权限:带有静态htmls的Php url缩短器,php,html,.htaccess,url-shortener,Php,Html,.htaccess,Url Shortener,我的访问权限: RewriteEngine On RewriteCond $1 !^(index\.php) RewriteRule ^(.*)$ index.php?l=$1 [L] 和php: <?php $links = parse_ini_file('links.ini'); if(isset($_GET['l']) && array_key_exists($_GET['l'], $links)){ header('Location: ' . $links[$_
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?l=$1 [L]
和php:
<?php
$links = parse_ini_file('links.ini');
if(isset($_GET['l']) && array_key_exists($_GET['l'], $links)){
header('Location: ' . $links[$_GET['l']]);
}
else
include ('index.html');
?>
这些文件位于根目录下,url缩短工作正常
在index.html中,我有一个包含静态html页面的菜单:
<a href="/" >Home page</a>
<a href="page1" >page1</a>
<a href="page2" >page2</a>
etc.
但这也不起作用。。。我发现页面没有正确重定向错误。当您查看错误消息时,您会注意到 PHP警告:语法错误,第1行的links.ini中出现意外“=”\n第2行的/var/www/example.com/index.PHP中 当查看
links.ini
时,您将在URL中看到第二个等号=
youtube1=
因此,引用该值,例如
youtube1 = "https://www.youtube.com/watch?v=9bZkp7q19f0"
应使其按预期工作。因此我将php更改为:
<?php
$links = parse_ini_file('links.ini');
if(isset($_GET['l']) && array_key_exists($_GET['l'], $links)){
header('Location: ' . $links[$_GET['l']]);
}
else if($_GET['l'] == 'index' | $_GET['l'] == 'page1' | $_GET['l'] == 'page2')
include($_GET['l']. '.html');
else if($_GET['l'] == '')
include('index.html');
else
include('404.html');
?>
我不知道这有多优雅。。。
如果有人知道一个更好的方法…您可能想看看,它与您正在做的非常相似,但是使用json而不是ini文件
核心是.htaccess
文件:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!index\.php).+)$ /index.php?short=$1 [NC,L,QSA]
您是否尝试过
var\u dump($links)代码>?谢谢,我添加了youtube链接作为示例。我遇到的问题是它没有打开index.html中的静态htmls(菜单)。我认为htaccess规则可能是问题所在?不,不是。我将您的示例文件逐字复制到我的测试环境中,当我输入http://www.example.com/youtube1
(当然,在links.ini中引用后)。因此代码可以工作,问题在于links.ini或index.html。查看error.log以获得更多提示。
<?php
$links = parse_ini_file('links.ini');
if(isset($_GET['l']) && array_key_exists($_GET['l'], $links)){
header('Location: ' . $links[$_GET['l']]);
}
else if($_GET['l'] == 'index' | $_GET['l'] == 'page1' | $_GET['l'] == 'page2')
include($_GET['l']. '.html');
else if($_GET['l'] == '')
include('index.html');
else
include('404.html');
?>
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!index\.php).+)$ /index.php?short=$1 [NC,L,QSA]