KJS KJS - 2 months ago 10
Apache Configuration Question

htaccess Custom Redirects not working

I've been searching all over the place, but can't seem to get it working.

This code is running perfectly:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^/?artists/[\w-]+/([\w-]+)/([\w-]+)/?$ /dir1/artist.php?artist_id=$1&page_id=$1 [L,QSA,NC]
RewriteRule ^/?tour/[\w-]+/([\w-]+)/?$ /dir2/index.php?artist_id=$1 [L,QSA,NC]
RewriteRule ^/?video/[\w-]+/([\w-]+)/?$ /dir3/index.php?artist_id=$1 [L,QSA,NC]
RewriteRule ^/?news/[\w-]+/([\w-]+)/?$ /dir4/article.php?artist_id=$1 [L,QSA,NC]


However, after this I try this below, and there is no response at all from the server. It just loads the old page (still on the server)

my custom redirect attempts, tried and not working



RedirectMatch 301 ^/old_dir//old_file.html /artists/subdir/21/biography/
RewriteRule /old_dir//old_file.html /artists/subdir/21/biography/ [L,R=301]


Anyone knows how to set the custom redirects after the first RewriteRules?

ps. not even @anubhava's answer works (htaccess Redirects Not Working Right)

Answer

Your regex pattern is the problem for 301 rule:

RewriteEngine On

RewriteRule ^old_dir/old_file\.html$ /artists/subdir/21/biography/ [L,R=301,NC]

RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^/?artists/[\w-]+/([\w-]+)/([\w-]+)/?$ /dir1/artist.php?artist_id=$1&page_id=$1 [L,QSA,NC]
RewriteRule ^/?tour/[\w-]+/([\w-]+)/?$ /dir2/index.php?artist_id=$1 [L,QSA,NC]
RewriteRule ^/?video/[\w-]+/([\w-]+)/?$ /dir3/index.php?artist_id=$1 [L,QSA,NC]
RewriteRule ^/?news/[\w-]+/([\w-]+)/?$ /dir4/article.php?artist_id=$1 [L,QSA,NC]

btw I see no relation of this problem with the linked answer (that answer is for adding www and https)

Hope my answer works this time for you :)