How do I target multiple directories with RewriteCond in the htaccess file?

时间:2015-06-15 15:05:50

标签: php apache .htaccess mod-rewrite error-handling

I have .htaccess file which I am using to remove .php extensions and apply some 'vanity' to a folder. I am also trying to add custom error pages. I am not very familer with htaccess at all and have only ever used it to redirect a user. I have this so far

RewriteEngine ON
# hide php extentions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]

#vanity on a directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/ownitall/profile.php?user=$1 [NC]

#Error Handling
ErrorDocument 403     /errors/403.php
ErrorDocument 404     /errors/404.php
ErrorDocument 500     /errors/500.php

There are two main probelms with this: 1: The error handling does not work at all, if a page doesn't exist anyway I just redirected to profile.php.

RewriteRule ^(.*)$ http://localhost/ownitall/profile.php?user=$1 [NC]

So far example if i was to go to page /about-us/sub-folder/ - which doesn't exist I would get redirected to profile.php.

* EDITED * //PROFILE.php document

<?php if(isset($_GET)){
    print_r($_GET);
}?>

To further exlpain - If i went to page '/about/index' in the site I get this output, which comes from profle.php

Array ( [user] => about/index.php/index )

Instead of my error handling document. * EDITED *

  1. That same line is also being applied to any sub folders in the root directory, is there a way I can target that to apply to that folder and not any sub-folders with that directory or should I handle this in the profile.php document then redirect the user to the relevant error document if the page doesn't exist on the site.

0 个答案:

没有答案