1

my htaccess file is that:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  .*
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule .*\.php cms/index.php?request=$0&%{QUERY_STRING} [L]
RewriteRule ^(?:(?!website).).*\.(?:(?!php).)+$ cms/website/$0 [L]
RewriteRule ^[^./]*(\/[^./]*)*$ cms/index.php?dirs=$0 [L]

for example there is a file in uploads/aa.png, and if I request http://example.com/uploads/aa.png, it still redirect http://example.com/website/uploads/aa.png.

shortly, if there is file at the url requested it still redirect "cms/website", how can I disable that?

burak emre
  • 1,501
  • 4
  • 22
  • 46

2 Answers2

3

The RewriteCond directive only applies to the next RewriteRule. (Only one.)

So your rewrite conditions apply only to your first RewriteRule. The other rewrite rules are unconditional.

You have to repeat the RewriteCond's for each RewriteRule, or set an environment variable:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [E=FILE_EXISTS:1]

RewriteCond %{ENV:FILE_EXISTS] !=1
RewriteRule .*\.php cms/index.php?request=$0&%{QUERY_STRING} [L]

RewriteCond %{ENV:FILE_EXISTS] !=1
RewriteRule ^(?:(?!website).).*\.(?:(?!php).)+$ cms/website/$0 [L]

RewriteCond %{ENV:FILE_EXISTS] !=1
RewriteRule ^[^./]*(\/[^./]*)*$ cms/index.php?dirs=$0 [L]
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • I don't know why but it doesn't work. However I gor the idea and edited my htaccess file again. http://pastebin.com/nw9xxV4K thanks! – burak emre Sep 03 '11 at 17:12
1

1. What is the point in having these lines?

RewriteCond %{REQUEST_URI}  .*
RewriteCond %{QUERY_STRING} .* [NC]

They do nothing except wasting CPU cycles.

2. You already have good answer from @arnaud576875. Here is another approach which may work better (or may not fit your needs) -- it really depends on your overall rewrite logic:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# do not do anything for already existing files
# (no need to check for this condition again for other rules)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

# your rewrite rules -- they work with non-existed files and folders only
RewriteRule .*\.php cms/index.php?request=$0 [QSA,L]
RewriteRule ^(?:(?!website).).*\.(?:(?!php).)+$ cms/website/$0 [L]
RewriteRule ^[^./]*(\/[^./]*)*$ cms/index.php?dirs=$0 [L]

NOTES:

  1. I've got rid of &%{QUERY_STRING} in first rewrite rule -- QSA flag does the same, even better.

  2. Your matching pattern in first rewrite rule: .*\.php -- this is ain't great as will match /hello.php.png/tell-me-something as well. If that is OK -- no probs then, but if you wanted to only match requests to .php files (e.g. /interesting/text.php), then it will be better to add $ at the end: it to .*\.php$

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • thanks for your answer. your all points are great, I don't know htaccess very well, I took a look at some htaccess file and wrote this lines. However I couldn't understand this line "RewriteRule .+ - [L]", and is there any documentation for mod.rewrite, I couldn't find anything? – burak emre Sep 03 '11 at 17:29
  • What exactly you do not understand there? That rule (with conditions) in general reads: "if requested resource is a file or folder, then rewrite to the same URL, which means exit rewrite NOW". `.+` means at least one character; `-` in target URL place means "do nothing" / "rewrite to the same URL", `[L]` means "Last Rule in this iteration". Useful link: http://stackoverflow.com/questions/6797998/rewriterule-last-l-flag-not-working . – LazyOne Sep 03 '11 at 17:36