4

Can anyone help with .htaccess optimization? Actually I have next code:

RewriteEngine on
RewriteBase /
DirectoryIndex frontend/www/index.php

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} cpanel/(.*)$
# Why not "-f" - because it's 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}

Folders 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 file if it exists in root directory or under frontend/www
  • my.domain.com/dir/base.css shows file if it exists in root/dir directory or under frontend/www/dir
  • my.domain.com/cpanel/base.css shows file if it exists in backend/www directory
  • my.domain.com/cpanel/dir/base.css shows file if it exists in backend/www/dir directory
  • if file not found in any of nedded directories I show index.php from frontend/www/index.php or from backend/www/index.php (if URI starts as cpanel/...).

Looks like my .htaccess work good, but I'm not sure of it quality.

Arthur Halma
  • 3,943
  • 3
  • 23
  • 44
  • I think your rules are OK. The only problematic/expansive part is the -F switch... however it seems you have good reason to use it – Kamil Šrot Nov 15 '12 at 09:54

1 Answers1

3

If your httpd.conf is set properly, this RewriteBase / is unneccessary

in RewriteCond %{REQUEST_URI} cpanel/(.*)$ the part cpanel/(.*)$ should be ^cpanel/(.*)$. Without it every url containing '..cpanel/something..' would match.

I guess in ...?f=%{REQUEST_URI} you want to pass the url that the client requested to index.php (maybe to log 404-s). However, if REQUEST_URI containes unwanted characters (e.g.&), it will result in unexpedted behaviour, e.g. only the part until & of the REQUEST_URI will be passed as the value of f parameter, the part after & will be an other, newly created GET parameter. (Please try it.)

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 an [L] flag, it's not important, however if you don't want to chain your rules, for security and maintainability it is always a good idea to add [L] flag.

I think your .htaccess is all right except these little things.

Denes Papp
  • 3,835
  • 3
  • 32
  • 33