.htaccess optimization

Can someone help with optimizing .htaccess? Actually, I have the following code:

RewriteEngine on RewriteBase / DirectoryIndex frontend/www/index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} cpanel/(.*)$ # Why not "-f" - because it domain of 3d level: VirtualDocumentRoot /path/to/htdocs/%-3/ and if I use "-f" file not founds :( RewriteCond backend/www/%1 -F RewriteRule . backend/www/%1 [L] RewriteCond %{REQUEST_URI} cpanel/(.*)$ RewriteCond backend/www/%1 !-F RewriteRule . backend/www/index.php?b=%1 [L] RewriteCond %{REQUEST_URI} !cpanel/(.*)$ RewriteCond frontend/www%{REQUEST_URI} -F RewriteRule . frontend/www%{REQUEST_URI} RewriteCond %{REQUEST_URI} !cpanel/(.*)$ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . frontend/www/index.php?f=%{REQUEST_URI} 

Folder structure:

 . ├── .htaccess (with code I provided) ├── frontend │ ├── www │ ├── css │ │ ├── base.css │ └── index.php ├── backend ├── www ├── css │ ├── base.css └── index.php 

I need:

  • my.domain.com/base.css shows the file if it exists in the root directory or under the interface / www
  • my.domain.com/dir/base.css shows the file if it exists in the root / dir directory or under the interface / www / dir
  • my.domain.com/cpanel/base.css shows the file if it exists in the backend / www directory
  • my.domain.com/cpanel/dir/base.css shows the file if it exists in the backend / www / dir directory
  • If the file is not found in any of the nedded directories, I show index.php from frontend / www / index.php or from backend / www / index.php (if the URI starts as cpanel / ...).

It seems my .htaccess is working fine, but I'm not sure about that.

+4
source share
1 answer

If your httpd.conf installed correctly, this RewriteBase / not needed

in RewriteCond %{REQUEST_URI} cpanel/(.*)$ part should be ^cpanel/(.*)$ . Without it, every URL containing "..cpanel / something .." will match.

I assume that in ...?f=%{REQUEST_URI} you want to pass the URL that the client requested for index.php (possibly to register 404's). However, if REQUEST_URI contains unwanted characters (e.g. & ), this will lead to unexpected behavior, for example. only the part before & from REQUEST_URI will be passed as the value of the parameter f , after & part will be another, newly created GET parameter. (Please, try.)

You can get the (original) REQUEST_URI in php with $_SERVER['REQUEST_URI'] . Your .htaccess will be more clear if you omit this part.

The last two rules deserve the flag [L] , but this is not important, however, if you do not want to bind your rules, it is always useful to add the flag [L] to ensure safety and maintainability.

I think your .htaccess is fine except for these little things.

+3
source

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


All Articles