Dear, Is there a way to redirect from the root without hurting other files in the root? I want to rewrite everything from the root expect for the files/urls i already have. Expected result: http://www.domain.com/***/*** Without rewriting: http://www.domain.com http://www.domain.com/stylesheet.css http://www.domain.com/images/*** (folder with some images for the stylesheet.) What i tried: RewriteRule ^(.*)/(.*)$ pages.php?pages=$1&page=$2 [L] Hope someone is willing to help me with this issue. Regards, Quiz
Try this: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]*)/([^/]*)$ /pages.php?pages=$1&page=$2 [L] Code (markup):
RewriteEngine on RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^(.*)$ index.php [L,QSA] Code (markup): That should do it. Replace index.php with whatever your entry script is. Handle all your route requests from the control of your own code.
I don't parse it into directories or try to pass it as getdata like other folks do. I find it unreliable, and as you just found it can be a bit of a PITA... ESPECIALLY if you have files or directories you want to lock out. Like say... ALL .php since you're doing "one index to rule them all" I just use a whitelist of files to go ahead and serve directly, and then redirect ALL other requests to my index.php # redirect all 'unknown' requests to index.php RewriteEngine On RewriteRule !\.(gif|jpg|png|css|js|html|htm|txt|ico|zip|gz|rar|pdf|xml|wav|mp3|mp4|mpg|flv|swf|mkv|ogg|avi|webm|woff|svg|eot|ttf)$ index.php Code (markup): gives you complete control over what people are allowed to access, locking out anything else! Then on the PHP side, I just parse $_SERVER['REQUEST_URI'] -- I have a singleton set up to handle that. final class request { private static $data = []; public static function value($index = 0, $restrict = false) { if (count(self::$data) == 0) { $path = trim(str_replace( ['\\','%5C'], '/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PASS) ),'/') if (strpos($path,'..')) die('hacking attempt detected, up-tree URI are not allowed!'); self::$data = empty($path) ? ['default'] : explode('/',$url); } return isset(self::$data[$index]) ? ( $restrict ? preg_replace('/[^\w]/', '', self::$data[$index]) : self::$data[$index] ) : false; } // method value } // class request Code (markup): The first parameter is the index in the path you want to read, the second is some sanitization -- I strip out all non-word characters for when I'm doing filenames that could be parsed into includes. So if you had: yourdomain.tld/test/gimboid with that .htaccess in the root echo request::value(); // outputs 'test' echo request::value(0); // outputs 'test' echo request::value(1); // outputs 'gimboid' Now, if it was yourdomain.tld/test+dumb/gimboid echo request::value(); // outputs testdumb As the non a..zA..Z0..9_ character is stripped out. Not too complex, and works better than trying to have apache parse it into getdata. It's also handy if on some other type of server where you can get a redirect to a file handler, but not be able to parse it. Ngnix for example you could set @missing... basically making a 404 handler out of it; instead of 404, send the proper page. Just be sure to send a 200 header