Forum Moderators: phranque
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
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$
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
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]