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?
-
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.– LazyOneCommented Jul 13, 2012 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-"– user249493Commented Jul 13, 2012 at 18:39
Add a comment
|
1 Answer
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.
-
Thanks - that worked! And I appreciate the 302 tip, too. Commented Jul 15, 2012 at 2:01