.htaccess with dynamic php - subfolder problems

时间秒杀一切 提交于 2019-12-11 15:17:57

问题


Necessary Knowledge

My .htaccess file redirects like this: domain.com/about to domain.com/index.php?page=about

RewriteRule ^([^/.]+)/?$ index.php?page=$1 [L]

The "page" variable is used in a php include:

<?php include_once($_SERVER['DOCUMENT_ROOT']."/contents/".$page.".html"); ?>

The "contents" folder simply contains .html files that are included as the content

Okay here's the problem:

I have a "subfolder" in the "contents" folder with additional .html files that I need to access Now I'm trying to redirect like this: domain.com/subfolder/about to domain.com/index.php?page=subfolder/about

This works:

RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1

But now I can't access the subfolder from: domain.com/subfolder/ because there is a 'page' variable

<?php $page = $_GET['page']; if(!$page) { $page = 'index'; } ?>

Any thoughts, ideas, or help would be greatly appreciated.

Thanks in advance


回答1:


With this, you shouldn't have to define any directory names - it rules them all out.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1

You may need to test the trailing slash though, it may work on /subfolder/ but not /subfolder




回答2:


You could exclude specific folder via

rewritecond %{REQUEST_URI}!^/folder1/  
rewritecond %{REQUEST_URI}!^/folder2/  
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1  

Edit: Link to the manual




回答3:


Just do another check:

if (preg_match('/^[^\\/.]+$/', $page)) {
    $page .= '/index';
}

If $page is just subfolder, /index would be appended to have subfolder/index.




回答4:


rewriteRule ^foldername/.*$ - [PT] 
rewriteRule ^foldername/.*$ - [PT] 

before any other rewrite rule...



来源:https://stackoverflow.com/questions/1170880/htaccess-with-dynamic-php-subfolder-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!