.htaccess 301 重定向仅适用于 1 个新链接

发布于 2025-01-12 19:33:34 字数 1399 浏览 2 评论 0原文

我有一个带有重定向 301 的 .htaccess 文件,并将其放置在重写引擎的正下方,

<IfModule mod_rewrite.c>
RewriteEngine On
#now this is the redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah

Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah

</IfModule>

只有第二个重定向有效,带有 get 方法的文件没有重定向,我做错了吗?

编辑:

这是我的整个模组重写:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# Permanent URL redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
# Permanent URL redirect
Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah

Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions /blog/2021/02/11/pengertian-konsolidasi-tanah
RewriteRule ^blog/2021/02/11/pengertian-konsolidasi-tanah   /blog.php?slug=konsolidasi-tanah-frequently-asked-questions

I have .htaccess file with redirect 301 on it and placed right under the rewriteengine

<IfModule mod_rewrite.c>
RewriteEngine On
#now this is the redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah

Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah

</IfModule>

only 2nd redirect works, the one with the get method are not redirected, am i doing wrong?

EDITED :

this is my entire mod rewrite :

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# Permanent URL redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
# Permanent URL redirect
Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah

Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions /blog/2021/02/11/pengertian-konsolidasi-tanah
RewriteRule ^blog/2021/02/11/pengertian-konsolidasi-tanah   /blog.php?slug=konsolidasi-tanah-frequently-asked-questions

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

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

发布评论

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

评论(1

十六岁半 2025-01-19 19:33:35

混合使用 RewriteRuleRedirect 301 指令通常不是一个好主意。他们可能会以意想不到的方式相互冲突。您依赖于 RewriteRule,因此您应该使用更多的重定向来实现重定向。

Redirect 301 无法根据 URL 中的查询字符串 (?...) 进行重定向,因此您无论如何都需要为该重定向实现 RewriteRules。

当您有重定向特定 URL 的规则时,它们应该位于 .htaccess 文件的顶部,以便它们优先于其他更通用的规则。

我建议禁用目录索引,因为我担心它会与您的 RewriteRule ^index\.php$ / [R=301,L] 规则冲突。

我在您的 .htaccess 文件中没有看到 RewriteEngine On,尽管您发布的开头代码片段中有它。

尝试将此作为您的 .htaccess

# Disable index.html, index.php default functionality
DirectoryIndex disabled

RewriteEngine On

# Permanent URL redirect
RewriteCond %{QUERY_STRING} ^slug=konsolidasi-tanah-frequently-asked-questions$
RewriteRule ^blog\.php$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah [R=301,L]

RewriteRule ^blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah  [R=301,L]

# Forward URLs without .php extension to existing PHP file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

# Redirect index.php URLs to the directory
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]

# Use index.php as a front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

It is usually not a good idea to mix RewriteRule and Redirect 301 directives. They can conflict with each other in unexpected ways. You depend of RewriteRule so you should implement your redirects with more of them.

Redirect 301 can't redirect based on query strings (?...) in the URL, so you need to implement RewriteRules for that redirect anyway.

When you have rules for redirecting specific URLs, they should go at the top of the .htaccess file so that they take precedence over the other more general rules.

I would recommend disabling the directory index because I fear it would conflict with your RewriteRule ^index\.php$ / [R=301,L] rule.

I don't see RewriteEngine On in your .htaccess file, despite that your snippet you posted to start with has it.

Try this as your .htaccess:

# Disable index.html, index.php default functionality
DirectoryIndex disabled

RewriteEngine On

# Permanent URL redirect
RewriteCond %{QUERY_STRING} ^slug=konsolidasi-tanah-frequently-asked-questions$
RewriteRule ^blog\.php$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah [R=301,L]

RewriteRule ^blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah  [R=301,L]

# Forward URLs without .php extension to existing PHP file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

# Redirect index.php URLs to the directory
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]

# Use index.php as a front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文