Forum Moderators: phranque

Message Too Old, No Replies

htaccess: Files under virtual folders work but root virtual files not

         

bunal

10:00 am on Oct 3, 2007 (gmt 0)

10+ Year Member



Hi,

What i am trying to achieve is:

a) [domain.com...] => /view.php?folder=apple&id=abc.php
b) [domain.com...] => /index.php?folder=apple
c) [domain.com...] => /view.php?folder=&id=abc.php

I have created a htaccess file like this. It works for a and b but could not make it work for the c

HTACCESS:

order deny,allow
allow from all
Options +FollowSymlinks

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_URI}!(.*)/$
RewriteCond %{REQUEST_URI}!(.*)\.php$
RewriteRule ^(.*)$ [domain.com...] [L,R=301]

RewriteCond %{REQUEST_URI}!index\.php [OR]
RewriteCond %{REQUEST_URI}!admin/upload\.php [OR]
RewriteCond %{REQUEST_URI}!admin/folders\.php [OR]
RewriteCond %{REQUEST_URI}!view\.php [OR]
RewriteCond %{REQUEST_URI}!404\.php [OR]

# For folder/
RewriteRule ^([A-Za-z0-9_-]*)/?$ /index.php?folder=$1 [L]

# For folder/index.php
RewriteRule ^([A-Za-z0-9_-]*)/index.php$ /index.php?folder=$1 [L]

# For folder/*.php
RewriteRule ^([A-Za-z0-9_-]*)/([^/\.]+).php$ /view.php?folder=$1&id=$2.php [L]

Any help would be really appreciated

jdMorgan

1:25 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not really enough info here to completely diagnose the problem, but two immediate flaws stand out:

1) The negative-pattern RewriteConds should not be [OR]ed, they should be ANDed (which is the default behaviour when the [OR] flag is not used).

2) RewriteConds apply only to the single RewriteRule which follows them. Therefore, your RewriteConds will apply only to the first RewriteRule in the following set, and not to the last two. Consider using a 'local OR' to compact the negative pattern to one line so that the RewriteCond can be efficiently copied to each rule, or modifying the logic so that the final three rules are skipped (see RewriteRule [S=] flag) when any of the excluded URL-paths are requested.

Example of local [OR]:


RewriteCond %{REQUEST_URI} !(index¦admin/upload¦admin/folders¦view¦404)\.php$

Note that you must change all broken pipe "¦" characters to solid pipes before use; Posting on this forum modifies the pipe character.

The answer to your specific question --given that the above problems are corrected first-- is that your final rule (which I presume to be the one you expect to match the request that isn't working) does not match the URL you are requesting. Without knowing how the URLs you do and do not wish that rule to rewrite can vary, I can't suggest a fix.

Jim

bunal

2:10 pm on Oct 5, 2007 (gmt 0)

10+ Year Member



Hi Jim

Here is my final .htaccess that is working.

ErrorDocument 404 /404.php

Order deny,allow
Allow from all
Options +FollowSymLinks

RewriteEngine On

# Add missing trailing slashes.
RewriteCond %{SCRIPT_FILENAME}!-f
RewriteRule!()$ [domain.com%{REQUEST_URI}...] [R=301,L]

# Files to ignore.
RewriteRule ^(index.\php¦admin/upload\.php¦admin/(.*)¦view\.php¦404\.php¦randomFolderContent\.php¦includes/(.*))$ - [NC,L]

# For folder/ and folder/index.php
RewriteRule ^([a-z0-9_-]+)(/(index\.php)?)?$ /index.php?folder=$1 [NC,QSA,L]

# For folder/*.php
RewriteRule ^(([a-z0-9_-]+)/)?([^/\.]+\.php)$ /view.php?folder=$2&id=$3 [NC,QSA,L]