Forum Moderators: phranque

Message Too Old, No Replies

Redirect if file does not exist

404 301 mod_rewrite .htaccess

         

skinter

7:46 pm on Nov 19, 2005 (gmt 0)

10+ Year Member



I have a lot of outdated files in my root directory, and Google has cached them all marking the new ones as duplicate content even though the old ones don't exist anymore.
I just want to redirect /*.(s¦html) to /app.php?file=$1

The code I have works, but it does it unconditionally.
For instance, it redirects file1.html to /app.php?file=file1 even though the file exists.

Code:
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+)\.shtml$ /app.php?file=$1 [L,R=301]
RewriteRule ^([^/]+)\.html$ /app.php?file=$1 [L,R=301]

I'm not sure if the!-d and f are necessary, or if this is even possible, though.

EDIT: And there's a topic about the same thing on the same page... Sorry mods, you can delete this. Guess I should have looked harder...

JAB Creations

9:47 am on Nov 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I could be wrong but...

ErrorDocument 404 /404.php
Redirect permanent /404.php http://www.example.com/redir

DeanC

9:37 pm on Nov 20, 2005 (gmt 0)

10+ Year Member




RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f

You're missing a space for one :)

skinter

12:19 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Oh, whoops. Let me try that out.

Well, still doesn't work. It just redirects everything.

jdMorgan

12:34 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One thing you need to be aware of: RewriteConds only affect the single RewriteRule that follows.

If you wish both of your rules to require the NOT(file-exists) condition, then you'll need to construct the code differently.

For example:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.s?html$ /app.php?file=$1 [L,R=301]

to handle both .html and .shtml files.

Jim

[edited by: jdMorgan at 12:37 am (utc) on Nov. 21, 2005]

jd01

12:36 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Conditions only effect the rule immediately following, so .shtml requests will be evaluated for the conditions, but .html requests will not.

You can combine them and use one rule:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.s?html$ /app.php?file=$1 [L,R=301]

I removed the directory check, because you are only evaluating requests with extentions, which should never be a directory... A request that ends in a / will not match your rule.

Also changed the expression to match 'everything that is not a .(dot) followed by a .(dot)'

Hope this helps.

Justin

jdMorgan

12:38 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jd01,

Echo in here! :)

Good on ya!
Jim

jd01

12:43 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, there we go typing the same ruleset at the same time again...

I'll have to start a couple of minutes earlier next time. ;)

Justin

skinter

4:47 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Seriously, you guys are the best. Ever.

+rep