Pergunta

I was not sure how to phrase this question but hopefully my description will make more sense as to what I wish to achieve.

I am currently building a framework which supports multiple applications. The directory structure is like so:

/applications/

--/admin/
----/www/
------/css/
--------/bootstrap.css
------/img/
--------/logo.png

--/gallery/
----/www/

/system/
/www/ <- web root
--/.htaccess
--/index.php

I am using url rewriting for "pretty urls" using the PATH_INFO data in php.

site.com/index.php/path/info/data/

will be the same as

site.com/path/info/data/

So everything will always pass through that index.php

The basic premise of the framework is, the first segment of the url will relate to the application name:

site.com/admin/ <- will load the admin application
site.com/gallery/ <- will load the gallery application
site.com/ <- will load what ever the default application is (specified in a framework config)

Now here is what I want this rewrite rule to do (if it is possible), ill use some example urls to help illustrate my point.

site.com/admin/login/

  1. Rule gets first segment admin

  2. Rule checks if admin folder exists in the applications directory

  3. If it exists, make it so that the admin/www contents is accessible via site.com/admin/www/

  4. Disable directory viewing, only allow linking to files, so site.com/admin/www/img/ is not accessible, but site.com/admin/www/img/logo.png is accessible

  5. Only files have precedence over the index.php rewriting, so site.com/admin/www/img/ will be passed to the index.php even if the folder exists, but site.com/admin/www/img/logo.png always reference the file unless it doesnt exist in which case it gets passed to the index.php

So to clarify, here are some links and what I want them to do (using example directory listing above):

site.com/ < index.php
site.com/admin/ < index.php
site.com/admin/www/ < index.php
site.com/admin/www/nonexistantfile.php < index.php
site.com/gallery/ < index.php
site.com/admin/www/css/ < index.php

site.com/admin/www/css/bootstrap.css < bootstrap.css
site.com/admin/www/img/logo.png < logo.png

Although I can do what I want to using the framework itself, it would be too resource intensive to run the uri parsing engine and any other framework parts on every asset load which is why I want to handle this using .htaccess

Thanks

Foi útil?

Solução

Try to use this in your .htaccess file:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*) /index.php

RewriteRule %{REQUEST_URI} ([a-z0-9-_]+)\.(php|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) /index.php

You said you want to rewrite them, but if you doesn't want 'cause it's not search engine friendly, just add a [R] flag ever after /index.php on the code but don't forget a single space before the flag, to make them redirecting.

Outras dicas

Wouldn't it be something like this?:

# File: www/.htaccess

RewriteEngine on

RewriteBase /
# If a file exists, serve it directly
RewriteCond %{REQUEST_FILENAME} !-f

# otherwise forward the request to the index.php
RewriteRule . index.php

Make sure you've enabled mod_rewrite and put appropriate AllowOverride directive for .htaccess to take effect.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top