Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a bunch of files with a "NEW-" prefix (e.g. "NEW-index.php"). I need to do a 301 redirect to the corresponding file without the prefix. I've tried a few things but I'm not getting the regex syntax correct. Can anyone show me the right way to do this?

share|improve this question
    
You question is way to broad -- you need to be more specific, e.g. from URL => to URL; what rules have you tried; what is working and what is not etc. –  LazyOne Jul 13 '12 at 16:50
    
(OP here...) Sorry, I thought I was specific. It's NEW-abc.php to abc.php, NEW-xyz.php to xyz.php, NEW-whatever.php to whatever.php (where 'whatever' is the same from "NEW-..." to the non-NEW version). The challenge is that there are probably hundreds of files and I'd rather not set up a separate rule for each one individually. I thought I needed something like "RewriteRule NEW-(.*)\.php $1\.php" but I don't think that's quite right. All I really want to do is strip the first four characters from the filename when they equal "NEW-" –  user249493 Jul 13 '12 at 18:39

1 Answer 1

up vote 0 down vote accepted

This does work fine here:

# Some options that _may_ be required for rewrite to work
# (depends on hosting/server configuration)
Options +FollowSymLinks +SymLinksIfOwnerMatch -MultiViews

# Activate Rewrite Engine
RewriteEngine On
RewriteBase /

# actual rewrite rule
RewriteRule ^(.*)NEW-(.+\.php)$ $1$2 [L,R=301] 

P.S. I suggest using 302 code for testing, and if everything works fine -- only then change to required 301. The reason -- modern browsers do cache such 301 redirects, so you may have brand new rule (or no rule at all) but you still will see the same redirect. In such case -- clear browser cache and restart it .. or try it in another browser.

share|improve this answer
    
Thanks - that worked! And I appreciate the 302 tip, too. –  user249493 Jul 15 '12 at 2:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.