Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.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
Php 在重写的URL上使用.htaccess_Php_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

Php 在重写的URL上使用.htaccess

Php 在重写的URL上使用.htaccess,php,.htaccess,mod-rewrite,redirect,Php,.htaccess,Mod Rewrite,Redirect,我正在使用一个.htacces文件来缓存单个页面。我的文档根目录中有一个master.htaccess,这个htaccess将所有内容重定向到PHP路由器。路由器然后在给定的url上提供内容 然而,对于我正在使用的页面,URL是动态的,并且取决于用户名 url可能如下所示: http://example.com/username/custom-page $parent = $_SERVER['DOCUMENT_ROOT'].'/content/custom-page/.htaccess';

我正在使用一个.htacces文件来缓存单个页面。我的文档根目录中有一个master.htaccess,这个htaccess将所有内容重定向到PHP路由器。路由器然后在给定的url上提供内容

然而,对于我正在使用的页面,URL是动态的,并且取决于用户名

url可能如下所示:

http://example.com/username/custom-page
$parent = $_SERVER['DOCUMENT_ROOT'].'/content/custom-page/.htaccess'; 

  if (!is_file($parent)){
    $noCache = '<FilesMatch "\.(.*)$">
      FileETag None
      <IfModule mod_headers.c>
      Header unset ETag
      Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
      Header set Pragma "no-cache"
      Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
      </IfModule>
      </FilesMatch>';

    $file = fopen($parent, "w");
    if (!$file) die("error writing to parent directory");

    fwrite($file, $noCache);
    fclose($file);
    }
else {
    echo "no htaccess";
}
提供给该url的内容位于:

http://example.com/content/custom-page/
但是,我注意到,当我将.htaccess文件放在提供文件的目录中时,它没有任何效果

此外,由于url是动态的,因此很难对该页面使用单独的.htaccess

不管怎样,我正试着写这个。htaccess如下:

http://example.com/username/custom-page
$parent = $_SERVER['DOCUMENT_ROOT'].'/content/custom-page/.htaccess'; 

  if (!is_file($parent)){
    $noCache = '<FilesMatch "\.(.*)$">
      FileETag None
      <IfModule mod_headers.c>
      Header unset ETag
      Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
      Header set Pragma "no-cache"
      Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
      </IfModule>
      </FilesMatch>';

    $file = fopen($parent, "w");
    if (!$file) die("error writing to parent directory");

    fwrite($file, $noCache);
    fclose($file);
    }
else {
    echo "no htaccess";
}
$parent=$\u SERVER['DOCUMENT\u ROOT']./content/custom page/.htaccess';
如果(!is_文件($parent)){
$noCache=
FileTag无
标题未设置ETag
标题集缓存控制“最大年龄=0,无缓存,无存储,必须重新验证”
标题集Pragma“无缓存”
标题集过期“1984年1月11日星期三05:00:00 GMT”
';
$file=fopen($parent,“w”);
如果(!$file)死亡(“写入父目录时出错”);
fwrite($file,$noCache);
fclose($文件);
}
否则{
回显“不可访问”;
}

我在这个问题上得到了“no-htaccess”。我想知道我应该如何处理它。我真的不想在master.htaccess上设置一个max-age头。

您有一个简单的逻辑错误

  if (!is_file($parent)){
请注意,如果文件不存在,则执行代码;如果文件存在,则回显“no htaccess”

我不知道你为什么想要这个检查,因为你只是覆盖了文件,如果它存在的话


当然,以这种方式编写.htaccess可能不会产生您想要的效果,但这是一个完全不同的问题。

因为您甚至没有听说过Front Controller,这让您觉得必须以这种方式编写。URL重写的问题早就解决了。看看Symfony2,Zend,Laravel-对于一个项目,基本上只有一个
.htaccess
包含一条规则!