htaccess自定义重定向无法正常工作

时间:2016-09-26 19:25:14

标签: php apache .htaccess redirect mod-rewrite

我一直在寻找各地,但似乎无法让它发挥作用。

此代码运行正常:

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]

然而,在此之后我尝试下面这个,并且服务器根本没有响应。它只是加载旧页面(仍然在服务器上)

我的自定义重定向尝试,已尝试但无法正常工作

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

任何人都知道如何在第一个RewriteRules之后设置自定义重定向?

PS。甚至@ anubhava的回答都没有(htaccess Redirects Not Working Right

1 个答案:

答案 0 :(得分:1)

您的正则表达式模式是301规则的问题:

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]

顺便说一下,我发现此问题与关联答案没有关系(答案是添加wwwhttps

希望我的回答对你有用:)