dongmo1708 2016-08-23 14:07
浏览 76
已采纳

HTACCESS重定向到index.php:子文件夹修复程序中的PHP级别

My current HTACCESS looks like this.

# Run everything else but real files through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule ^(.*)$ index.php?p=$1 [NC,L,QSA]

Great piece of code where when I type in what would be a subfolder directory, it redirects as a parameter to index.php. Works great for something http://localhost/blah (which redirects as http://localhost/index.php?p=blah), but not so great for http://localhost/blah/boink.

The problem with the latter is that the redirection does work and does redirect to index.php, but for some reason, the page believes that it is at the subfolder level, blah. So all my hardcoded ../ for my js, css, include and require_once paths are now technically "wrong". Anyway of fixing this?

In PHP, when I try to do this:

$file_url = explode("/", $_SERVER['PHP_SELF']);

// subtract 1 for left / and 1 for current file
$dir_loop = count($file_url) - 2;
$up_dirs = "";
// loop until to root directory

for ($i=0; $i<$dir_loop; $i++) {
    $up_dirs .= "../";
}

...this doesn't seem to work either, because the index.php is at the root folder level. I suspect this may be a HTACCESS issue? Thoughts?

  • 写回答

1条回答 默认 最新

  • duane2364 2016-08-23 15:36
    关注

    @Ron van der Heijden clued me into: RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$. Modified the code as follows:

    # Run everything else but real files through index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteCond %{REQUEST_URI} !^.*\.(js|css|png|jpe?g|gif|ico)$ [NC]
    RewriteRule ^(.*)$ index.php?p=$1 [NC,L,QSA]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多