如何修改.htaccess文件以避免同一index.php文件有多个副本?
我正在开发一个电子学习网站,平台上的每门课程都有一个课程预览页面。平台的文件结构类似于如何修改.htaccess文件以避免同一index.php文件有多个副本?,php,apache,file,.htaccess,web-development-server,Php,Apache,File,.htaccess,Web Development Server,我正在开发一个电子学习网站,平台上的每门课程都有一个课程预览页面。平台的文件结构类似于https://example.com/courses/[course_id…]/index.php,php代码从父目录名称获取课程id,以便在数据库中查找。这里的问题是,我必须在我制作的每个课程目录中制作相同的index.php文件副本,因此是否有办法修改.htaccess文件,使我只能拥有一份index.php文件副本?假设您有一个名为course.php的文件。此文件根据URL上的ID调出一个课程,如下所
https://example.com/courses/[course_id…]/index.php
,php代码从父目录名称获取课程id,以便在数据库中查找。这里的问题是,我必须在我制作的每个课程目录中制作相同的index.php
文件副本,因此是否有办法修改.htaccess
文件,使我只能拥有一份index.php
文件副本?假设您有一个名为course.php的文件。此文件根据URL上的ID调出一个课程,如下所示:
http://localhost/course.php?id=1
在您的.htaccess中
RewriteEngine on
RewriteRule ^courses/([0-9]+)$ course.php?id=$1
然后你可以去http://localhost/courses/1 获取课程ID 1
<?php
#For security course must exist.
# -1 will display 404
$course = isset($_GET['course']) ? $_GET['course'] : -1;
$courses = [
'1', '2', '3',
];
if (!in_array($course, $courses)){
header("HTTP/1.0 404 Not Found");
echo '404';
exit();
}
include_once("courses/".$course."/course.php");
RewriteEngine On
# course/x to /index.php?course=x
RewriteRule ^course/([0-9]+) /index.php?course=$1 [QSA,L]
# protect folder courses
RewriteRule ^courses/(.+)$ - [F,L]
root/
index.php
.htaccess
/courses
/1
/course.php
/2
/course.php
Goto http://app.local/course/1 to /courses/1/course.php