bwoogie
bwoogie

Reputation: 4427

"Ignoring" files with .htaccess

Is it possible to ignore all actual files in a directory with RewriteRules? What I mean is, I'm creating "flatlinks" or "permalinks" (http://example.com/not/an/actual/file/path/) that page.php handles. This only works if I 'ignore' all other files with RewriteCond. Instead of creating a RewriteCond for every real file in the directory in my .htaccess file is there a way to tell it to check if there is an actual file show that and if not let page.php take care of it?

RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^/somefile\.php$
RewriteCond %{REQUEST_URI} !^/someotherfile\.php$
RewriteCond %{REQUEST_URI} !^/page\.php$
RewriteRule ^([^/]*)$ /page.php?p=$1 [L] [NC]

Upvotes: 0

Views: 2372

Answers (2)

piouPiouM
piouPiouM

Reputation: 5037

The following indicates that the redirect should be executed only when the requested file (-f) or folder (-d) does not exist (!) on disk:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /page.php?p=$1 [L,NC]

Upvotes: 1

anubhava
anubhava

Reputation: 785128

Yes it is very much possible. Add these lines before your existing code:

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

Upvotes: 2

Related Questions