Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache URL重写和重定向_Apache_Url_Mod Rewrite_Redirect_Rewrite - Fatal编程技术网

Apache URL重写和重定向

Apache URL重写和重定向,apache,url,mod-rewrite,redirect,rewrite,Apache,Url,Mod Rewrite,Redirect,Rewrite,我刚刚用.htaccess完成了重写,一切正常 RewriteEngine on RewriteRule blog/([a-zA-Z0-9\-]+)-([0-9]+) post.php?url=1&id=$2 我唯一想避免的是重复的内容。。。我在这个问题上搜索了很多,遗憾的是发现了任何符合我需要的东西 事实上,重写的地址“blog/my-new-post-77”也可以通过“post.php?id=77”访问,我不希望发生这种情况。因此,我想将每个post.php页面重定向到重写规

我刚刚用.htaccess完成了重写,一切正常

RewriteEngine on
RewriteRule blog/([a-zA-Z0-9\-]+)-([0-9]+)    post.php?url=1&id=$2
我唯一想避免的是重复的内容。。。我在这个问题上搜索了很多,遗憾的是发现了任何符合我需要的东西

事实上,重写的地址“blog/my-new-post-77”也可以通过“post.php?id=77”访问,我不希望发生这种情况。因此,我想将每个post.php页面重定向到重写规则


有人对我有什么想法吗?

是的,在重写规则中添加额外变量以检查它:

RewriteRule blog/([a-zA-Z0-9\-]+)-([0-9]+)    post.php?check=ok&url=$1&id=$2
在post.php文件的顶部,检查
check
变量:

if(isset($_GET['check'])){
  if($_GET['check'] == "ok"){
   //it comes using rewrite rule
   //cut and paste all of your codes in post.php file to here
   //that is which codes are available when user view your page in desired way
   //don't redirect to rewrite rule again here, since visitor came using that rewrite rule to here. doing so will result infinite redirect and will show an error
  }else{
   //this visit is not used rewrite rule to come here
   //here you can redirect visitor to not found page or just show an empty page by putting die();
   //you can't redirect to post pages here, because you don't know what post(id) that visitor need to see
  }
}else{
  //this visit is not used rewrite rule to come here
  //here you can redirect visitor to not found page or just show an empty page by putting die();
  //you can't redirect to post pages here, because you don't know what post(id) that visitor need to see
}

通过浏览器,post.php?id=77->/my-new-post-77

/my-new-post-77->post.php?id=77通过内部重写

是这样吗

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule blog/([a-zA-Z0-9\-]+)\-([0-9]+)$ post.php?url=1&id=$2 [L]

RewriteCond %{THE_REQUEST} \s\/post\.php\?id\=(\d+)\s
RewriteRule . /my-new-post-%1 [R,L]

我找到了一个更符合我需要的解决方案

if($data["url"]!=$_GET["url"]) {
   header("Location:http://www.mywebsite.com/blog/".$data["url"]."-".$data["id"]);          
}
此解决方案强制post.php?id=XX按照我们的要求转到重写位置,同时遇到任何手动url重写

$result是“选择所有我的数据库”行

$data = mysqli_fetch_array($result);
我测试了@Janaka的解决方案,你的解释很好,而且效果很好


谢谢大家,;案例结束:)

我尝试过,但仍然不匹配。忘了精确地说,“我的新帖子”,正如重写规则中所写的,是一个dba行,由.php?url=1调用。我修改了它,但仍然可以访问blog.php?id=77内容
我的新帖子
必须是脚本URL的一部分,才能将其捕获到新的URL中。这是一个多么好的主意(我因为没有考虑它而打了我的额头)!但遗憾的是,它不起作用。仍然可以访问blog.php?id=77上的内容。谢谢因为你必须把你所有的代码移到
//它是用重写规则来实现的
die()
或其他东西放到
块中。我明白了,完全误解了你向我提议的方式。所以,如果我理解得很好,应该在if中放置header:location,在ELSE中放置die()?很抱歉理解错误。我编辑了它,将每个地方需要做的事情描述为commentsWell,感谢您的大力帮助@Janaka,我将立即尝试。我认为它应该是
url=$1
,在您的重写规则中不是吗?您错过了
$
标记谢谢您的提示:)