apache服务器:将所有url请求重定向到index.php
我在mac上使用apache服务器,一切正常 现在我有一个网页,它的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
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']
参数中(如果使用第二条规则)