N
N
Nikolay Savelyev2019-08-26 20:48:27
PHP
Nikolay Savelyev, 2019-08-26 20:48:27

How to do routing in htaccess?

Hello!

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
RewriteRule ^users(.+)$ index.php?url=users&page=$1 [QSA,L]

How to properly implement cnc in htaccess
1 - /index.php
2 - /index.php?url=about
3 - /index.php?url=users&page=profile
1 - /
2 - /about
3 - /users/profile
--- - index.php
define('SKYLIGHT',dirname(__FILE__));

# Шапка
include('inc/head.php');

$url = $_GET['url']; // site.ru/url=название

if(isset($url)){ 
if(file_exists(dirname(__FILE__)."/pages/".$url.".php")){ 
include(dirname(__FILE__)."/pages/".$url.'.php'); // Ищем файл в директории
}else{ 
include(dirname(__FILE__).'/pages/404.php'); // Страница 404
} 
}else{ 
include(dirname(__FILE__).'/pages/main.php'); // Главная страница
}

# Подвал
include('inc/foot.php');

---- /pages/users.php
include ('./inc/menu.php'); // Меню пользователя будет со всеми страницами в /pages/user/

$url2 = $_GET['page'];

if(isset($url2)){ 
if(file_exists(dirname(__FILE__)."/user/".$url2.".php")){ 
include(dirname(__FILE__)."/user/".$url2.'.php'); // Ищем страницы
}else{ 
include(dirname(__FILE__).'/404.php');
} 
}else{ 
include(dirname(__FILE__).'/user/acc.php'); // Профиль основная в /user/
}

Description of files
/index.php - includes the directory /pages
/pages/users.php - includes the directory /pages/user
Here is the script for clarity: Yandex.disk
------------------ --------------------
Please do not recommend MVC.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Derepko, 2019-08-26
@xEpozZ

Please do not advise MVC.

I advise you to google. Your cases have already existed for 20 years

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question