我有一个PHP文件,它接收打印页面的参数,如下所示:
build.php?parameter=print-this-article我想要的是创建一个带有.htaccess的RewriteRule,它允许我将最后一部分发送到该PHP文件,而不管它的级别是什么,例如:
www.mysite.com/article/level-1/level-2/level-3因此,在这种情况下,build.php将接收参数level-3。
但如果用户键入以下URI:
www.mysite.com/article/level-1/level-2它的工作原理如下:
build.php?parameter=level-2对于level-1也是如此。
对此有解决方案吗?
发布于 2017-02-19 13:18:31
一个对mod_rewrite不太了解的php示例
此示例检查请求的url,然后重定向到所需的正确url。
<?PHP
//check for https or http
$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
//small function to help find the protocol and move it left
$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
//the basename(dirname()) example will show the folder the url script is in this only echos the if/else will do the work
echo $protocol."://".$_SERVER['SERVER_NAME']."/".basename(dirname($_SERVER['REQUEST_URI']));
if(basename(dirname($_SERVER['REQUEST_URI'])) == "level-1") header("Location: http://www.yoursite.com/build.php?parameter=level-1");
if(basename(dirname($_SERVER['REQUEST_URI'])) == "level-2") header("Location: http://www.yoursite.com/build.php?parameter=level-2");
if(basename(dirname($_SERVER['REQUEST_URI'])) == "level-3") header("Location: http://www.yoursite.com/build.php?parameter=level-3");http://php.net/manual/en/function.substr.php
function strleft($s1, $s2)
{
return substr($s1, 0, strpos($s1, $s2));
}如果重定向不起作用
function redirect($url)
{
if (!headers_sent())
{
header('Location: '.$url);
exit;
}
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>'; exit;
}
}
?>对于mod_rewrite
RewriteEngine on
RewriteRule ^oranges.html$ apples.html一切就绪后,访问以“/oranges.html”结尾的站点-所有显示的信息都将来自"/apple.html“站点。
https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite
发布于 2017-02-20 03:29:42
要获取级别,必须捕获请求URI的一部分。为了确保它是最后的部分,它不能包含任何斜杠。此正则表达式可以识别这一点
RewriteRule ([^/]+)$ build.php?parameter=$1 [L]最重要的部分是[^/],它是一个斜杠[...],由anything not ^ a slash /组成。
发布于 2017-02-20 07:42:07
我做了更多的研究,结束了PHP和RewriteRule的混用。
我在.htaccess上写道:
# this sends the whole part of the URI that is after the 'article/'
RewriteRule ^article/(.+)$ build.php?parameter=$1 [L]因此,如果URI是www.mysite.com/article/level-1/level-2,那么PHP文件build.php将收到这个字符串level-1/level-2。
在build.php中,我添加了这个函数:
function get_last_parameter($link)
{
// to split the string right on the '/'
$parts = explode("/", $link);
// to remove empty elements, this helps in case a URI ended with '/' is received
$parts = array_diff($parts, array(''));
// take the final element of the array and remove any string that starts with '#', so it won't be confused with sections
$final_array = explode("#", end($parts));
// the result is an array of two (or more elements) so send the first one (of this array)
$parameter = reset($final_array);
return $parameter;
}
// ... and wrote the query to select from the database the article这种方法的优点是我可以同时使用URI的所有“级别”,因为我将拥有完整的数组。
https://stackoverflow.com/questions/42323646
复制相似问题