mod_rewrite 301 重定向不起作用

发布于 2024-11-15 23:50:56 字数 518 浏览 2 评论 0原文

我正在尝试设置一个重写,将此 URL:

/video-2011.php?video=150

重定向到:

/video/150/freeform-title-text-here /

我在 HTACCESS 中使用此行进行了重写:

RewriteRule ^video/([0-9]+)/(.*)$ video-2011.php?video=$1 [L]

但是一旦我将 R=301 添加到混合中,它就会中断。有什么想法吗?

这是 HTACCESS 崩溃时的完整信息:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^video/([0-9]+)/(.*)$ video-2011.php?video=$1 [R=301,L]

非常感谢任何帮助,谢谢!

I'm trying to set up a rewrite that will redirect this URL:

/video-2011.php?video=150

to this:

/video/150/freeform-title-text-here/

I have the rewrite working using this line in my HTACCESS:

RewriteRule ^video/([0-9]+)/(.*)$ video-2011.php?video=$1 [L]

But as soon I add R=301 into the mix, it breaks. Any ideas?

Here's the full HTACCESS when it breaks:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^video/([0-9]+)/(.*)$ video-2011.php?video=$1 [R=301,L]

Any help is greatly appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2024-11-22 23:50:56

我认为您的 .Htaccess 中可能缺少一行。

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^video/([0-9]+)/([a-z0-9-_]+)$ video-2011.php?video=$1 [L]
#New
RewriteCond %{REQUEST_URI} !^/video/([0-9]+)/([a-z0-9-_]+)/?$
RewriteCond %{QUERY_STRING} ^video=([0-9]+)&name=([a-z0-9-_]+)/?$
RewriteRule ^video-2011.php$ video/%1/%2/? [R=301,L]

我假设您想要重写 url(如果是:)

/video/150/freeform-title-text-here/

重定向(如果是:) url:

/video-2011.php?video=150

到:

/video/150/freeform-title-text-here/

因此,这样可以使 url 看起来漂亮整洁。

如果我错了,请纠正我。

编辑

我添加了 RewriteCond 以阻止第二次重写发生。

因为第一条规则显然会重写:

/video/150/freeform-title-text-here/

这意味着您看不到的查询字符串:

/video-2011.php?video=150

也会使第二条规则发生。

I think you might be missing a line from your .Htaccess.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^video/([0-9]+)/([a-z0-9-_]+)$ video-2011.php?video=$1 [L]
#New
RewriteCond %{REQUEST_URI} !^/video/([0-9]+)/([a-z0-9-_]+)/?$
RewriteCond %{QUERY_STRING} ^video=([0-9]+)&name=([a-z0-9-_]+)/?$
RewriteRule ^video-2011.php$ video/%1/%2/? [R=301,L]

I assuming you want to rewrite the url if it is:

/video/150/freeform-title-text-here/

And redirect the url if it is:

/video-2011.php?video=150

to:

/video/150/freeform-title-text-here/

So this way it keeps the urls looking pretty and tidy.

Please correct me if I am wrong.

Edit

I've added in a RewriteCond to stop the second rewrite happening.

As the first rule will obviously rewrite:

/video/150/freeform-title-text-here/

Which means the query string you don't see:

/video-2011.php?video=150

Would of made the second rule happen too.

作业与我同在 2024-11-22 23:50:56

您可以尝试吗:

RewriteRule ^video/([0-9]+)/ /video-2011.php?video=$1 [R=301,L,NC,QSA]

是的,它会将 /video/150/foo 重定向到 /video.php?video=150,而不是您在您的文章中所述的相反方式问题。

Can you try:

RewriteRule ^video/([0-9]+)/ /video-2011.php?video=$1 [R=301,L,NC,QSA]

And yes it will redirect /video/150/foo to /video.php?video=150 not the other way around as you've stated in your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文