【问题标题】:Redirect url from Joomla articles to K2 articles using htaccess使用 htaccess 将 Joomla 文章中的 URL 重定向到 K2 文章
【发布时间】:2013-02-09 20:13:02
【问题描述】:

我已经从 Joomla! 导入了我的文章!到 K2 保持 ID 相同。现在我想重写 Joomla!指向 K2 项目的 url。我正在使用带有 SEF 和 modrewrite 启用的 Joomla 2.5.9。另外,我从未使用过 modrewrite 规则,我认为这是最好的选择,因为我有成千上万的文章。

我要更改的网址为:

http://www.mysite.com/news/component/content/article/9-society/19771-the-alias-of-the-article

而同一个K2项生成的url是

http://www.mysite.com/news/society/19771-the-alias-of-the-article

我创建了菜单项society 以获取此网址。

所以,我编辑了 .htaccess 以创建如下规则

RewriteRule ^news/component/content/article/9-(.*)$ /news/$1 [R=301,L]

但是我得到一个 404 错误页面。请注意,此示例是尝试了解 modrewrite 是否正常工作。我没有考虑 id 类别(本例中为 9)。

问题是:重写规则我错过了什么?

此外,我还尝试了我在 SO 中看到的其他规则,并尝试通过谷歌搜索了解它们的工作原理以及 404 错误页面。就我所知,这个问题中提供的规则是应该发挥作用的规则。

答案中建议修改的htaccess文件如下(我的网站在新闻文件夹中。如果我使用RewriteBase我需要调整它 ):

##
# @package    Joomla
# ...
Options +FollowSymlinks -MultiViews

## Mod_rewrite in use.
RewriteEngine On

## Begin - Rewrite rules to block out some common exploits.
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*([^)]*) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
# My site is in the news folder. If I use RewriteBase I need to adjust it
RewriteBase /news/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/component/content/article/9-([^/]+)/([^/]+)/? /news/$1/$2 [R=301,L]
## End - Custom redirects
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.

【问题讨论】:

  • 我认为你需要 RewriteCondRewriteRule...虽然你可能已经在 .htaccess 文件的前面有一个 RewriteCond (我认为它们可能必须出现在相邻行不过),在这种情况下,也可以在这里发布。

标签: .htaccess mod-rewrite joomla url-rewriting joomla-k2


【解决方案1】:

您可以在根目录中的一个 .htaccess 文件中尝试此操作:

更新文件夹/news中的.htaccess文件:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /news
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^component/content/article/9-([^/]+)/([^/]+)/?  $1/$2 [R=301,L]

重定向

http://www.mysite.com/news/component/content/article/9-society/19771-the-alias-of-the-article 带或不带斜杠

http://www.mysite.com/news/society/19771-the-alias-of-the-article

假定所有字符串都是固定的,society19771-the-alias-of-the-article 除外。

对于静默映射,请从 [R=301,L] 中删除 R=301

注意:RewriteCond %{REQUEST_FILENAME}... 都是为了防止循环。他们检查文件或目录是否存在,以防万一跳过规则。

【讨论】:

  • 我在根目录的一个 .htaccess 文件中尝试了您的答案,并清除了所有浏览器历史记录,但 Joomla 中提供的 404 page not found' persist. I also tried the rule in the .htaccess 除外! 2.5.9 结果相同。有什么建议吗?
  • 是的,请复制粘贴整套规则。也许您错过了我刚刚更新为MultiViews 的选项行。
  • 我错过了 MultiViews 选项。添加但我仍然收到 404 错误。我将 htaccess 文件添加到问题中。我在根目录的文件中尝试了您的独立答案。问题中的附件位于 Joomla! 所在的 news 文件夹中。已安装。
  • @pQB 我看到您将此规则集添加到您问题中的文件中,并且 RewriteBase 是 /news。我相应地更新了我的答案。那应该这样做。如果仍然失败,很可能与文件中的其他规则有冲突。
  • 我明白了。问题基本上与重写库有关。它现在正在工作。你摇滚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 2019-07-10
  • 1970-01-01
  • 2012-12-17
  • 2013-05-15
  • 2017-12-05
  • 2011-02-14
相关资源
最近更新 更多