htaccess自定义重定向不起作用 [英] htaccess Custom Redirects not working

查看:65
本文介绍了htaccess自定义重定向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在到处搜索,但似乎无法正常工作.

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

此代码运行良好:

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)

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之后设置自定义重定向吗?

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

ps.甚至@anubhava的答案也不起作用( htaccess重定向不起作用)

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

推荐答案

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

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]

顺便说一句,我认为此问题与链接的答案无关(该答案用于添加wwwhttps)

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 :)

这篇关于htaccess自定义重定向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆