Mobile app version of vmapp.org
Login or Join
Rivera981

: How to remove a prefix from the old filenames and redirect to the new filename? I have a bunch of old files prefixed with old- (e.g. old-abcde.php). I need an htaccess rule to set up a 301

@Rivera981

Posted in: #301Redirect #Htaccess

I have a bunch of old files prefixed with old- (e.g. old-abcde.php). I need an htaccess rule to set up a 301 redirect so that any request for a file starting with old- goes to its corresponding new version (e.g. abcde.php).

To be clear, I have many files, not just one, so I can't do a literal filename match. I basically just need to strip off the old- from the request and redirect to the version without it.

I know I probably just need a simple regular expression, but I'm not good at writing them. Can anyone provide assistance?

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Rivera981

3 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

Using mod_rewrite in the .htaccess file in the document root:

RewriteEngine On
RewriteRule ^(.+/)?old-(.*) / [R=301,L]


This will handle old- files in the document root and anywhere on the filesystem (not explicitly stated in your question, but the other answers assume these files are all located in the document root). It assumes that abc.php is in the same directory as old-abc.php.

The slash prefix on the substitution (ie. /) is required in the case of an external redirect, otherwise the directory prefix will be added and the redirect will break, exposing your directory structure.

10% popularity Vote Up Vote Down


 

@Sent6035632

I would use this in the .htaccess file :

RewriteEngine on
RewriteRule old-(.*)$ [L,R=301]


Add QSA between the brackets if you have to keep the additional query string (like ?ID=xxxxx).
Specify R=301 to make a 301 redirect and specify the redirect is permanent.

10% popularity Vote Up Vote Down


 

@XinRu657

Try ModRewrite:

RewriteEngine On
RewriteRule old-(.*)$ [NE,R]


This sends an 301 to the browser and redirects it.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme