views:

162

answers:

2

My .htaccess looks like this:

RewriteEngine on 
RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)|(\.swf)|(\.xpi)|(\.ico)|(\.src)$ 
RewriteCond %{REQUEST_URI} ^(.*)$
#RewriteCond %{REQUEST_FILENAME} ! -f

RewriteRule (.*)$ view.php?picid=$1 [L]

Problem is that when I visit www.example.com, it’s sending me to view.php. There is an index.php file in my root directory. How do I get .htaccess to ignore the index file?

+1  A: 

Remove the # from the fourth line and remove the space between the exclamation mark (!) and the -f

RewriteCond %{REQUEST_FILENAME} !-f

This line says that if the file doesn't already exist on the server, in this case, index.php, then continue and do the rewrite that follows.

Also check you've got both a DirectoryIndex as well as checking against any valid directories.

DirectoryIndex index.php

RewriteCond %{REQUEST_FILENAME} !-d

Which would leave you with this cleaner version:

DirectoryIndex index.php

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ view.php?picid=$1 [L]

This first sets up index.php as the default file. Then you run the Rewrite engine, and if the requested file does not already exist on the server as either a file or a directory, will pass things on to the view.php file and run its magic.

random
i changed it but no effect. I tried adding an if(isset($picid){}else{header('Location:http://website.com');} but it still shows error
Patrick
You tried adding php to a .htaccess file?
Mark
+1  A: 

Try this rule:

RewriteCond %{REQUEST_URI} !.*\.(gif|jpg|png|css|js|php|swf|xpi|ico|src)$ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) view.php?picid=$1 [L]

The important changes I made:

  • The regular expression in your first RewriteCond is faulty. The $ anchor is only applied to the last option in the alteration.
  • The second RewriteCond directive is useless.
  • The third RewriteCond that’s commented out will do just what you want: Check if the requested URI path can be mapped to an existing file.
  • Altered the quantifier from zero or more to one or more characters. That will exclude the empty URI path when / is requested.
Gumbo
works great, thanks.
Patrick