Reputation: 21
Friends I am newbie to .htaccess and Rewrite Rule and very puzzled about these rules. I am trying to execute these following lines for the index.php page. When I write in url, one or two or three or four query arguments then it goes OK. Any of these pattern matches I collects query and proceed further in php. But when query arguments exceeds more than four arguments (say someone intentionally trying to give wrong urls) I want to redirect the page to homepage. I didn't found any solution anywhere on internet. Can anybody help me, how to do that. I desperately want a solution to do this project and in very urgent need. I read many articles but doesn't understand how to handle this problem. I'm executing these all tasks with XAMPP on Win 7. The patterns I am using are,
RewriteRule ^([a-zA-Z0-9_]+)/?$ index.php?levelone=$1 [NC]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$ index.php?levelone=$1&leveltwo=$2 [NC]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$ index.php?levelone=$1&leveltwo=$2&levelthree=$3 [NC]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$ index.php?llevelone=$1&leveltwo=$2&levelthree=$3&levelfour=$4 [NC]
URL example : (say localhost/myproject/index.php is my homepage)
localhost/myproject/levelone/
localhost/myproject/levelone/leveltwo/
localhost/myproject/levelone/leveltwo/levelthree/
localhost/myproject/levelone/leveltwo/levelthree/levelfour
When I use these four Urls then it's ok, but if I use
localhost/myproject/index.php/levelone/leveltwo/levelthree/levelfour/levelfive/levelsix/
then my index.php page return some html without style and layout, I trying many types of RewriteCond and commands in .htaccess but all in vain.
I want that query string arguments must be in the range from 1 to 4 if it exceeds the range say five arguments then page must redirect to homepage (/index.php) again otherwise match to pattern. It would help me if anyone know if there is way to combine all these four long patterns in one short line for matching any of four.
Upvotes: 1
Views: 1547
Reputation: 41
This rewrite rule will only kick in if there is not a file or directory that already exists.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /path/to/index.php [L,QSA]
The -f flag will make the rewrite only kick in if the file doesn't exist, and the -d flag will ensure the rewrite won't kick in if a directory exists.
Upvotes: 3
Reputation: 29188
I suggest avoiding a redirect to the home page. In index.php, display the home page content by default if none of the level variables are set.
Here's a one-line rule (maybe it can be simplified even more):
RewriteRule ^(?:([a-zA-Z0-9_]+)\/)?(?:([a-zA-Z0-9_]+)\/)?(?:([a-zA-Z0-9_]+)\/)?(?:([a-zA-Z0-9_]+)\/)?$ index.php?level1=$1&level2=$2&level3=$3&level4=$4 [L]
And the PHP:
$levels=array();
foreach (range(1,4) as $i) {
if (isset($_GET['level'.$i])) {$levels[$i]=$_GET['level'.$i];}
}
if (!empty($levels)) {
// show level content
} else {
// show home page
}
Upvotes: 1