Php 如何使用.htaccess重写HTTP get请求
我需要Php 如何使用.htaccess重写HTTP get请求,php,.htaccess,Php,.htaccess,我需要.htaccess规则来重新路由: example.com/view post.php?id=40toexample.com/article/40 因此,浏览器在地址栏中显示example.com/article/40,但服务器使用example.com/view post.php?id=40以下功能: Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCR
.htaccess
规则来重新路由:
example.com/view post.php?id=40
toexample.com/article/40
因此,浏览器在地址栏中显示example.com/article/40
,但服务器使用example.com/view post.php?id=40
以下功能:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^article/(\d+)*$ ./view-post.php?id=$1
请参阅:.htaccess在nginx服务器中不起作用。因此,您应该尝试使用以下代码来仅使用php脚本进行路由。将此脚本保存在index.php文件中
$url = $_SERVER['REQUEST_URI'];
if(preg_match('/article/', $url)){
$id = $_GET['id'];
$url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/view-post.php?id='.$id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$httpResponse = curl_exec($ch);
echo $httpResponse;
}else{
echo "404 Error, Page not found";
}
如果您有另一个路由url Ex.example.com/post/40和example.com/porfile/40,那么您可以在If中添加一个条件,如下所示
if(preg_match('/article/', $url) || preg_match('/porfile/', $url) || preg_match('/post/', $url)){
我希望这个答案能帮助你:)什么是不可理解的?向我们提供您的代码、错误。您好。看起来您收到了所需的答案,然后删除了该问题。虽然你可以删除你的帖子,但如果有用的答案也会被删除,那么删除一个问题并不符合堆栈溢出的精神。问题是.htaccess你在为CSS和JS尝试什么URL?