: 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
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?
More posts by @Rivera981
3 Comments
Sorted by latest first Latest Oldest Best
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.
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.