emfi
emfi

Reputation: 659

rewrite condition does not work

In my php-project, I have the following folder structure.

/ [root]
|-- ... [some other folders]
|-- util/            // containing js,css files; should be accessible for anyone.
|-- client/          
    |--data/         // contains files which can be uploaded by users
       |-- private/  // should only be accessible for logged in users
       |-- public/   // should be accessible for anyone.
|-- ... [some other folders]
|-- index.php

I want to achieve the following behaviour:

  1. If someone directly access to anything within util/ he should just get what he has requested, if it's not an directory.
  2. If someone wants to access any file within client/ it should be redirected to index.php. For example someone enters the url www.test.com/client/data/private/test.jpg the server should get the request as index.php?request1=client/data/private/test.jpg.
  3. Everything else should be rewritten to index.php?request2=$1

I am not able to get point 2 function as per the expectation.

I use the following .htaccess file, to handle this:

RewriteEngine On

# allow access to all files within util/  WORKS!!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)(util)($|/) - [L]

# here i have problems.. how can i achieve, that access to folder client/ is rewritten to index.php?request1=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^client
RewriteRule ^(.*)$ index.php?request1=$1 [QSA,L]

# rewriting everything else   WORKS!!
RewriteRule ^(.+)$ index.php?request2=$1 [QSA,L]

What am i doing wrong here?

Upvotes: 3

Views: 975

Answers (1)

anubhava
anubhava

Reputation: 785128

Try this code:

RewriteEngine On

# allow access to all files within util/  WORKS!!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^util($|/) - [L]

# here i have problems.. how can i achieve, that access to folder client/ is rewritten to index.php?request1=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^client(/.*|)$ index.php?request1=$1 [QSA,L]

# rewriting everything else WORKS!!
RewriteRule ^((?!index\.php).+)$ index.php?request2=$1 [QSA,L]

Upvotes: 2

Related Questions