Help with loading and regex for PHP

I am working on a new PHP structure for personal use in future projects and below is my planned file structure. I just need help with some regex for my .htaccess file and some help on how I can upload the files I need.

In principle, any "folder" after the domain should be loaded from my "modular" folder. I would like him to download www.domain.com/account/ from www.domain.com/module/account/ . I also want it to be in this format for any other folder the modules have. All folders / files under the "module" should be loaded as if they were at the top level.

In this example, although in my module / account / folder, if I have a file called home.php, then I have to access it using www.domain.com/account/home www.domain.com/module /account/home.php , and www.domain.com/module/user/register.php will have access to > www.domain.com/user/register

I hope this makes sense and appreciates any help and any advice. I need help with a .htaccess file for this folder structure to work. I did not decide whether to access all the files through a single index file, or if I just included a file such as bootstrap on each page. The bootstrap file will set all variables and configuration parameters, as well as automatically load all class files and create the necessary objects.

myFramework/ --/assets/ --------/css/ --------/images/ --------/javascript/ --/includes/ ---------/classes/ ---------/config/ ---------/language/ ---------/header.php ---------/footer.php --/module/ --------/account/ ----------------/create.php ----------------/login.php ----------------/logout.php ----------------/settings.php ----------------/editprofile.php --------/admin/ --------/blog/ --------/forums/ --------/messages/ --------/users/ --index.php 
+4
source share
5 answers

The answer from jasonbar is actually almost there. Everything that it does not have is associated with the .php extension, as you described:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/module RewriteCond %{REQUEST_URI} [.]php$ RewriteRule (.*)[.]php$ /module/$1 

As I said, I strongly recommend that you consider the front controller paradigm (as you have avoided describing the problem), as it allows you to significantly increase control, encourages the MVC approach, etc. = o)

EDIT:

I fixed some forgotten points and added the correct PHP extension handling. Note that the [L] argument at the end causes further processing to stop, which makes these code blocks useful as logical structures in your .htaccess file (i.e., preventing further processing); remove this argument if such functionality is not needed.

I also added a line to check if the requested php file really exists.

 RewriteEngine On # if the uri matches a directory in the module dir, redirect to that. Disable # this block if you don't wish to have either directory browsing or to have the # default apache file load. RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}/module%{REQUEST_URI} -d RewriteCond %{REQUEST_URI} !^/includes RewriteCond %{REQUEST_URI} !^/assets RewriteCond %{REQUEST_URI} !^/module RewriteRule (.*) /module/$1 [L] # if the uri matches a file sans the .php extension in the module directory, # then redirect to that. RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}/module%{REQUEST_URI}.php -f RewriteCond %{REQUEST_URI} !^/includes RewriteCond %{REQUEST_URI} !^/assets RewriteCond %{REQUEST_URI} !^/module RewriteRule (.*) /module/$1.php [L] 

EDIT:

To also allow files ending in ".php" from the modules directory, add the following to your .htaccess file:

 # if the uri matches a file with the .php extension in the module directory, # then redirect to that. RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}/module%{REQUEST_URI} -f RewriteCond %{REQUEST_URI} !^/includes RewriteCond %{REQUEST_URI} !^/assets RewriteCond %{REQUEST_URI} !^/module # note that the following line restricts access to php files only. comment out # the following line to allow any existing file under module director to be # accessed (or modify the following to allow other file extensions to be read) RewriteCond %{REQUEST_URI} [.]php$ RewriteRule (.*) /module/$1 [L] 
+3
source

I would try to solve this in PHP itself if it depended on me. Just create a .htaccess file that displays all possible requests in a single file (possibly index.php) and determines what to do next. This gives you the ability to do all kinds of bootstrapping and logging before delegating the request to any piece of code that should handle this request. You can even turn on and use a micro structure like Limonade to accomplish what you want. Here is an example:

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d # if the requested directory does not exist, RewriteCond %{REQUEST_FILENAME} !-f # and the requested file does not exist, RewriteRule ^ index.php # map everything to index.php. 

Then, in index.php, you can do all kinds of things to make sure you get the right answer. The easiest way to use a “structure-like controller” is to include an infrastructure like Limonade and use it. Example:

 <?php require_once 'vendor/limonade.php'; dispatch( 'account/home', 'accountHome' ); function accountHome( ) { require_once 'modules/account/home.php'; } run( ); 

Obviously, this is just a suggestion. Alternatively, you can simply use an even simpler system, although I think you will have to write it yourself. So you can say that if a file exists in the modules directory, just include this file and that.

 <?php $path = isset( $_SERVER['PATH_INFO'] ) ? trim( $_SERVER['PATH_INFO'], '/' ) : null; if( $path !== null ) { $filename = 'module/' . $path . '.php'; /** $path could be account/home */ if( file_exists( $filename ) ) { require_once $filename; } else { require_once 'error.php'; } } else { require_once 'home.php'; } 

What is it. Fully functional and all. You could use a library that sorts all this for you.

+3
source

After reading your requirements, I came up with the following solution:

 RewriteCond %{REQUEST_URI} !^/includes RewriteCond %{REQUEST_URI} !^/assets RewriteCond %{REQUEST_URI} !^/module RewriteCond %{REQUEST_URI} !^/index\.php RewriteRule (.*)(\.php)?$ /module/$1 

I have verified that this works with the following URL patterns using Apache 2.2:

Redirecting to the module folder :

http://local.sandbox.com/account/home.php?t=t
http://local.sandbox.com/account/home.php
http://local.sandbox.com/account/home.php/?t=t
http://local.sandbox.com/account/home?t=t
http://local.sandbox.com/account/home/?t=t
http://local.sandbox.com/account/home/
http://local.sandbox.com/account/home
http://local.sandbox.com/user/register
http://local.sandbox.com/user/register.php
http://local.sandbox.com/user/register?t=t
http://local.sandbox.com/user/register.php?t=t

Not redirected when these URIs are thrown :

http://local.sandbox.com/includes/header.php
http://local.sandbox.com/includes/header.php?t=t
http://local.sandbox.com/index.php?t=t
http://local.sandbox.com/?t=t

Note that RewriteCondition is essentially an AND consisting of NOT conditions, so any folder or file that you want to exclude from the rewrite rule should be added as a NOT condition.

The module rule is included, which means that any new folders that you place in the module folder will automatically obey your rewriting requirements.

+2
source

If I understand you, this should work. Conditions will cause redirection only when the requested resource is not a real file / directory and when it is not yet requested in the module directory.

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/module RewriteRule (.*) /module/$1 
0
source

I am developing a PHP framework with a colleague, so it really caught my attention.

Our .htaccess makes the minimum number of assumptions. It looks like this:

 DirectoryIndex index.php RewriteEngine on RewriteBase / RewriteCond %{REQUEST_URI} \.png$ RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond %{REQUEST_URI} \.gif$ RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond %{REQUEST_URI} \.jpg$ RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond %{REQUEST_URI} \.js$ RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond %{REQUEST_URI} \.pdf$ RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond %{REQUEST_URI} \.css$ RewriteRule ^(.*)$ $1 [QSA,L] RewriteCond %{REQUEST_URI} ^/favicon* RewriteRule ^(.*)$ favicon.ico [QSA,L] #RewriteCond %{REQUEST_FILENAME} !-f #RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?$1 [QSA,L] 

index.php, in turn, looks like this:

 <?php namespace System; try { ob_start(); $Indium = include 'src/bootstrap/IndiumFactory.php'; $Indium->run(); } catch (\Exception $e) // catch errors and display/log { Error::indium_exception_handler($e); Error::render_error_page(); } 

IndiumFactory is a bootloader that customizes the environment. Indium is the name of our structure. IndiumFactory is automatically generated from a set of configuration files.

Perhaps I should clarify that Indium has a mechanism for loading and calling the correct controller class with the rest of REQUEST_URI as arguments. Our forms rely on POST: ed data, so we can enforce pretty strict rules / filters on URIs.

0
source

Source: https://habr.com/ru/post/1334823/


All Articles