Forum Moderators: phranque

Message Too Old, No Replies

Redirect Conditional Problem

Redirect to new directories based on filename

         

Simsi

9:45 am on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys

I am having a problem implementing a rewrite. Can anyone help?

I want to move some files into a set of new directories. I want to redirect users to the new directory/file but depending on the filename, the directory will differ.

The Old filenames were like this:

[somedomain.com...]
[somedomain.com...]
...etc

The new dir/file structure is:

[somedomain.com...]
[somedomain.com...]
...etc

Basically, if the filename contains "-blue.php", it's moved to the /products/blue/ directory but kept the same filename.

I have tried this:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^.*-blue\.php
RewriteRule ^.*$ /blue/%1.php [R=301,L]

But I get the error....


Warning: Unexpected character in input: 'x' (ASCII=15) state=1 in /usr/bin/php4-cgi on line 3016

Parse error: parse error, unexpected '*' in /usr/bin/php4-cgi on line 3016

The .htaccess file is in the /products/ directory. I tried taking the hyphen out of the RewriteCond string (ie: *blue\.php) but no difference. Any ideas what I should be putting in there?

Cheers,

Simsi

[edited by: Simsi at 9:47 am (utc) on Aug. 16, 2007]

[edited by: jdMorgan at 4:36 pm (utc) on Aug. 16, 2007]
[edit reason] Tidy-up [/edit]

jdMorgan

4:53 pm on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Warning: Unexpected character in input: 'x' (ASCII=15) state=1 in /usr/bin/php4-cgi on line 3016

Parse error: parse error, unexpected '*' in /usr/bin/php4-cgi on line 3016

According to this error message, the problem is an invalid character in your PHP4-cgi file, and has nothing to do with .htaccess.

If you edited that file with anything other than a plain-text editor, then restore the original file and re-edit it using a plain-text editor. If you don't have a backup, then re-download it or get a fresh copy off the CD, whichever applies.

Once that's done, add a RewriteCond to prevent your rule from recursively rewriting (looping) and ending up quitting at products/blue/blue/blue/blue/blue/blue/blue/blue/blue/blue/blue/bigwidgets-blue.php due to a redirection limit error.

You can also move the URL-path pattern into the RewriteRule, and delete your original RewriteCond:

In example.com/.htaccess:


Options +FollowSymLinks
RewriteEngine on
#
# Prevent recursion
RewriteCond %{REQUEST_URI} !^/products/blue/
# Rewrite products/<something>-blue.php to /products/blue/<something>-blue.php
RewriteRule ^products/(.+-blue\.php)$ /products/blue/$1 [L]

If this code is located in example.com/products/.htaccess, then delete "products/" from the patterns in both lines.

Note that this is now an internal rewrite, not an external redirect. As a result, you can continue to use the original URL, while putting the files into subdirectories to make upkeep easier (or whatever your purpose was). Doing it this way, you won't risk losing the ranking of these pages in search.

If you must use an external redirect, then the rule (in example.com/.htaccess) should read:


RewriteRule ^products/(.+-blue\.php)$ [b]http://www.example.com[/b]/products/blue/$1 [[b]R=301,[/b]L]

Jim

Simsi

9:40 pm on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jd - I'll give that a whirl tomorrow :)