在 Apache 2.4 上使用 Joomla 重新启动后重定向旧 URL

Redirect old URL's after relaunch with Joomla on Apache 2.4

这或多或少是一个关于在 Apache 上重定向 [301] 的问题,但 Joomla CMS 有一些其他必需的规则。

我们用 Joomla 3 重新启动了一个网站。旧的是其他一些 CMS。 现在我们想将 [301] 旧 URL 重定向到新位置。

问题似乎是这不起作用。 我想要一个可以适合我们所有案例(将近 300 个)的解决方案。

以下是我们喜欢重定向的一些示例(旧 => 新):

/index.php?CID=1 => /
/index.php?CID=2&Kat=1$ => /
/index.php?CID=3&Kat=2&PID=100 => /new/path
/old/path/file.extention => /new/file/path
/old/page.html => /new/page
/show_image.php?image=123456.jpg&text=Very%20Long%TEXT => /new/path/location
/show_image.php?image=654321.jpg&text=Very%20Long%TEXT => /other/path/location

我已经尝试了一些重写规则,例如:

RewriteRule ^/index\.php\?CID=1$ http://%{HTTP_HOST}/ [R=301,L]

RedirectMatch 301 ^/index\.php\?CID=1$ /

对于某些 URLs,RedirectMatch 版本有效,但不适用于大多数,特别是不是所有 URLs 都用 /index.php 声明,我假设是因为这是入口点对于 Joomla。

有没有办法在 Joomla RewriteRule 激活之前重定向具体的 URL,包括以 /index.php 开头的一次?

RewriteEngine 已打开并使用 Joomla 附带的默认 htaccess:SEF URLs 和 URL-Rewrite 已启用。

请给我一些正确方向的提示。谢谢

这里是默认的 Joomla .htaccess 配置,如果你不知道的话:

<IfModule autoindex>
  IndexIgnore *
</IfModule>

Options +FollowSymlinks
Options -Indexes

RewriteEngine On

RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]

RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

同时我自己找到了解决方法,在这里分享给大家。

所以这里的主要问题是,如果你想用查询字符串重定向 URLs,你不能使用 Redirect 或 RedirectMatch。 您必须使用带有查询字符串重写条件的重写规则,没有办法解决它。 这意味着对于某些 URL(不是我希望的),您最终将至少得到两行,对于没有任何查询字符串的 URL,它将在一行内工作。

下面是针对给定示例的一些解决方案:

/index.php?CID=1 => /
/index.php?CID=2&Kat=1$ => /

因为只有查询字符串不同,但 file/path (index.php) 和目标 (/) 相同,我们可以用 OR 标志连接重写条件:

RewriteCond %{QUERY_STRING} ^CID=29$ [OR]
RewriteCond %{QUERY_STRING} ^CID=2&Kat=1$
RewriteRule ^index\.php$ /? [R=301,L]

无能为力的是最后一行的?。如果您只将 / 放到目标位置,apache 将重定向到例如/?CID=29 不只是 /

对于任何其他带有查询字符串的示例,您必须非常相似地执行它

/index.php?CID=3&Kat=2&PID=100 => /new/path
/show_image.php?image=123456.jpg&text=Very%20Long%TEXT => /new/path/location
/show_image.php?image=654321.jpg&text=Very%20Long%TEXT => /other/path/location

解决方法如下:

RewriteCond %{QUERY_STRING} ^CID=3&Kat=2&PID=100$
RewriteRule ^index\.php$ /new/path? [R=301,L]

RewriteCond %{QUERY_STRING} ^image=123456\.jpg&text=Very%20Long%TEXT$
RewriteRule ^show_image\.php$ /new/path/location? [R=301,L]

RewriteCond %{QUERY_STRING} ^image=654321\.jpg&text=Very%20Long%TEXT$
RewriteRule ^show_image\.php$ /other/path/location? [R=301,L]

请注意,您还必须在末尾添加 ?

对于没有查询字符串的所有示例,这将照常工作。 这里有一些我提到的例子:

/old/path/file.extention => /new/file/path
/old/page.html => /new/page

我们只是像这样重定向它:

RewriteRule ^old/path/file\.extention$ /new/file/path [R=301,L]
RewriteRule ^old/page\.html$ /new/page [R=301,L]

一些补充说明:

因为重写规则模式(原始 file/path)或重写条件模式(在本例中为查询字符串)总是被解释为正则表达式,其中 doth 表示 "any character" 我把它前面总是一个 \,所以我实际上只匹配正确的文件或查询字符串,而不是一些类似的文件,如 "index-php" 或 "index7php" 等。 如果您有其他 RegEx 特定字符(例如 + 或 *),您也应该这样做。

我也有一些重写规则模式,其中原始 URL 包含一些 space,这将导致 500 错误,但可以通过将孔重写规则模式放在双引号内来非常简单地解决.

这将在 Apache 2.4 下工作。希望对您有所帮助!

对于你们所有人如何将其放入 Joomla 附带的 .htaccess 文件中,您应该将其放在 RewriteBase / 之前 这是自定义重定向应该在的地方