rewrite url containing pointing to specific dir only if 404

New to mod_rewrite? This is a good place to start.

rewrite url containing pointing to specific dir only if 404

Postby urmel1960 » Wed Aug 26, 2009 12:14 am

Hello:

I want to rewrite any URL pointing to a specific directory on my server - but only if the url would lead to a 404 error.

Example:

Code: Select all
http://example.dom/directory/existing.html  -> no rewrite
http://example.dom/directory/not_existing.html  - would lead into 404 error but should rewrite (because in /directory/) to http://example.dom/specialpage.html
http://example.dom/does_not_exist_too.html  - no rewrite will lead into 404 error

I assume this may have to do something with look ahead variables (couldn't find valid documentation on this so far), but I don't have any idea to solve this.

Any help is very much appreciated. Thanks in advance
urmel1960
 
Posts: 3
Joined: Wed Aug 26, 2009 12:05 am

Postby richardk » Sat Aug 29, 2009 2:16 pm

Try the following in /.htaccess
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^directory/.+$ /specialpage.html [QSA,L]


I assume this may have to do something with look ahead variables (couldn't find valid documentation on this so far), but I don't have any idea to solve this.

Checking if the file/directory (%{SCRIPT_FILENAME}) exists (-f or -d respectively) should work.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby urmel1960 » Sun Aug 30, 2009 12:27 am

Will try - Thanks a lot so far
urmel1960
 
Posts: 3
Joined: Wed Aug 26, 2009 12:05 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 26 guests

cron