Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.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
在子目录中使用.htaccess重定向url_.htaccess_Mod Rewrite_Url Rewriting - Fatal编程技术网

在子目录中使用.htaccess重定向url

在子目录中使用.htaccess重定向url,.htaccess,mod-rewrite,url-rewriting,.htaccess,Mod Rewrite,Url Rewriting,我有一个名为“blog”的子目录。我想在web浏览器中访问此目录时清除url。所以当我打字的时候我想要这个 www.example.com/blog/index.php?id=1 webbroswer必须显示如下: www.example.com/blog/posts/1 当并没有子目录时,我成功地实现了这一点,但在子目录的情况下,它显示了404错误 下面是我在.htaccess文件中所做的工作,以使它在根目录(而不是子目录)中实现时正常工作 我在index.php文件中把我的url写为 e

我有一个名为“blog”的子目录。我想在web浏览器中访问此目录时清除url。所以当我打字的时候我想要这个

www.example.com/blog/index.php?id=1
webbroswer必须显示如下:

www.example.com/blog/posts/1
当并没有子目录时,我成功地实现了这一点,但在子目录的情况下,它显示了404错误

下面是我在.htaccess文件中所做的工作,以使它在根目录(而不是子目录)中实现时正常工作

我在
index.php
文件中把我的url写为

echo '<a href="www.example.com/posts/' . $row['id'] . '">' .  $row['title']  . '</a>';
我将我的
.htaccess
文件保存在名为
blog
的子目录中

我在
StackOverflow
中尝试了许多问题,但似乎没有一个能解决我的问题。
如有任何帮助,我们将不胜感激。

为您已有的规则添加重写基础:

RewriteEngine On
RewriteBase /blog/
RewriteRule ^posts/([^/]*)$ index.php?id=$1 [L]
这些将位于目录
/blog/
htaccess文件中

并确保在index.php中生成如下链接:

echo '<a href="www.example.com/blog/posts/' . $row['id'] . '">' .  $row['title']  . '</a>';
echo';

我已经这样做了,但是
RewriteBase
似乎不起作用,因为它使用根目录的
index.php
文件。这意味着当我希望
博客的
index.php
文件能够被提供时,这个链接就不存在了。而是为根目录的
index.php
文件提供服务。@AmitYadav删除index.php之前的
/
RewriteEngine On
RewriteBase /blog/
RewriteRule ^posts/([^/]*)$ index.php?id=$1 [L]
echo '<a href="www.example.com/blog/posts/' . $row['id'] . '">' .  $row['title']  . '</a>';