Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.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 从子目录重写页面_Php_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Php 从子目录重写页面

Php 从子目录重写页面,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我有以下设置: index.php config.php pages/ index.php about.php contact.php 当我想访问“about.php”时,我需要访问 现在我想重写URL,使其看起来像这样: .htaccess应该是什么样子?我是一个完全不懂修改的人。这应该适合你: RewriteEngine On #External redirect with THE_REQUEST trick; change R to R=301 whe

我有以下设置:

index.php
config.php
   pages/
     index.php
     about.php
     contact.php
当我想访问“about.php”时,我需要访问

现在我想重写URL,使其看起来像这样:


.htaccess应该是什么样子?我是一个完全不懂修改的人。

这应该适合你:

RewriteEngine On
#External redirect with THE_REQUEST trick; change R to R=301 when everything works correctly
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?page=(.*)\ HTTP
RewriteRule ^ /%2.php\? [R=302,L]

#Internal rewrite
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^(.*).php$ /index.php?page=$1 [L]

如果您访问
http://example.com/index.php?page=about
它会将您重定向到
http://example.com/about.php

我尝试了“重写规则^pages/([^/]*)\.php$?page=$1[L]”之类的方法。谢谢,效果不错!但是现在错误文档404不再工作了。当我访问一个以.php结尾的不存在的页面时,我会看到一个白色的站点。当不存在的页面不是以.php结尾时,一切正常。是否有许多页面需要使用上述代码重定向?然后您可能需要在
index.php
文件中执行此操作,检查重定向文件是否存在。如果它不使用类似
header('Location:http://example.com/linktoerrordocument');