Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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请求重定向到index.php_Apache_.htaccess_Redirect - Fatal编程技术网

apache服务器:将所有url请求重定向到index.php

apache服务器:将所有url请求重定向到index.php,apache,.htaccess,redirect,Apache,.htaccess,Redirect,我在mac上使用apache服务器,一切正常 现在我有一个网页,它的index.php非常复杂: 在index.php中,它需要像这样的url:localhost/~username/hostname/page\u a。 而page\u a的内容实际上是存储在content文件夹中的.php文件。 因此,在index.php中,它只解析page\u a,并执行include:“”来呈现请求页面 但在.htaccess中,重定向规则未正确配置 例如,如果我单击一个链接:localhost/~use

我在mac上使用apache服务器,一切正常

现在我有一个网页,它的
index.php
非常复杂: 在
index.php
中,它需要像这样的url:
localhost/~username/hostname/page\u a
。 而
page\u a
的内容实际上是存储在
content
文件夹中的
.php
文件。 因此,在
index.php
中,它只解析
page\u a
,并执行include:“”来呈现请求页面

但在
.htaccess
中,重定向规则未正确配置

例如,如果我单击一个链接:
localhost/~username/hostname/page\u a
, 有没有办法将此url重定向到
index.php
, 同时,url仍然是
localhost/~username/hostname/page_a
, 这样
index.php
就可以正确地解析和呈现它了


谢谢。

这取决于
index.php
如何处理请求。如果它只是在
$\u服务器
变量(如
'REQUEST\u URI'
)中查找请求的文件,则不需要传递任何内容。如果它查找查询字符串参数,则重写规则需要传递该参数

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
或者,如果需要传递查询字符串参数:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
如果规则中没有
R
标志或
http://
,浏览器上的URL地址栏将不会更改。如果有人输入
http://localhost/~username/hostname/page_a
在他们的浏览器中,
/~username/hostname/page_a
请求被发送到服务器,重写规则将在内部将其重写为
/index.php
,php脚本可以在
$\u服务器['request\u URI'中查找
查找
/~username/hostname/page\u a
请求并正确路由它,或者在
$\u GET['url']
参数中(如果使用第二条规则)