.htaccess 使用htaccess将Joomla文章的url重定向到K2文章

.htaccess 使用htaccess将Joomla文章的url重定向到K2文章,.htaccess,mod-rewrite,joomla,url-rewriting,joomla-k2,.htaccess,Mod Rewrite,Joomla,Url Rewriting,Joomla K2,我从Joomla进口了我的文章!保持ID不变。现在我想重写《Joomla》!指向K2项的URL。我正在使用Joomla2.5.9,支持SEF和modrewrite。而且,我从来没有使用过modrewrite规则,我认为这是最好的选择,因为我有成千上万的文章 我要更改的url为: http://www.mysite.com/news/component/content/article/9-society/19771-the-alias-of-the-article 同一K2项生成的url为 htt

我从Joomla进口了我的文章!保持ID不变。现在我想重写《Joomla》!指向K2项的URL。我正在使用Joomla2.5.9,支持SEF和modrewrite。而且,我从来没有使用过modrewrite规则,我认为这是最好的选择,因为我有成千上万的文章

我要更改的url为:

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

为了得到这个url,我创建了菜单项
society

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

RewriteRule ^news/component/content/article/9-(.*)$ /news/$1 [R=301,L]
但我得到一个404错误页面。注意,这个例子是一个试图理解modrewrite是否工作的例子。我没有考虑id类别(本例中为9)

问题是:重写规则遗漏了什么

此外,我还尝试了在SO中看到的其他规则,并通过谷歌搜索试图了解它们是如何工作的,但也尝试了404错误页面。这个问题提供的规则是我所知不多的,应该发挥魔力的规则

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

##
#@Joomla
# ...
选项+以下符号链接-多视图
##Mod_正在使用中重写。
重新启动发动机
##开始-重写规则以阻止一些常见的漏洞攻击。
#阻止任何试图在URL中对数据进行base64_编码的脚本。
RewriteCond%{QUERY_STRING}base64_encode[^(]*([^)]*)[或]
#阻止在URL中包含标记的任何脚本。
重写cond%{QUERY_STRING}(|%3E)[NC,或]
#阻止任何试图通过URL设置PHP全局变量的脚本。
重写cond%{QUERY_STRING}全局(|[|%[0-9A-Z]{0,2})[或]
#阻止任何试图通过URL修改_请求变量的脚本。
重写cond%{QUERY_STRING}_请求(|[|%[0-9A-Z]{0,2})
#返回403禁止标题并显示根主页的内容
重写规则。*index.php[F]
#
##结束-重写规则以阻止一些常见的漏洞攻击。
##开始-自定义重定向
#我的网站在新闻文件夹中。如果我使用RewriteBase,我需要调整它
编辑基地/新闻/
重写cond%{REQUEST_FILENAME}!-f
重写cond%{REQUEST_FILENAME}!-d
重写规则^news/component/content/article/9-([^/]+)/([^/]+)/?/news/$1/$2[R=301,L]
##结束-自定义重定向
##
#如果您的Web服务器的URL
#与物理文件路径没有直接关系。
#更新您的Joomla!目录(仅适用于根目录)。
##
#重写基/
##开始-Joomla!核心SEF部分。
#
重写规则。*-[E=HTTP\U授权:%{HTTP:AUTHORIZATION}]
#
#如果请求的路径和文件不是/index.php,则
#尚未在内部重写为index.php脚本
重写cond%{REQUEST_URI}!^/index.php
#请求的是组件文件夹中的内容,
#或用于站点根目录,或用于无扩展URL,或
#请求的URL以列出的扩展名之一结尾
重写cond%{REQUEST_URI}/component/|(/[^.]*|(php | html |提要| pdf | vcf |原始))$[NC]
#请求的路径和文件与物理文件不直接匹配
重写cond%{REQUEST_FILENAME}!-f
#请求的路径和文件与物理文件夹不直接匹配
重写cond%{REQUEST_FILENAME}!-d
#在内部将请求重写为index.php脚本
重写规则。*index.php[L]
#
##结束-Joomla!核心SEF部分。

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

已更新文件夹
/news
中的.htaccess文件:
Options+FollowSymlinks-多视图
重新启动发动机
编辑基地/新闻
重写cond%{REQUEST_FILENAME}!-f
重写cond%{REQUEST_FILENAME}!-d
重写规则^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

假设所有字符串都是固定的,除了
社会
19771文章的别名

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


注意:
RewriteCond%{REQUEST\u FILENAME}…
都是用来防止循环的。它们会检查文件或目录是否存在,以防它们存在时跳过规则。

我认为您需要一个
RewriteCond
和一个
RewriteRule
…尽管在.htaccess文件前面可能已经有了一个
RewriteCond
(我想可能它们必须出现在相邻的行上),在这种情况下,也可以在此处发布。我在根目录中的一个.htaccess文件中尝试了您的答案,并清除了所有浏览器历史记录,但未找到
404页“仍然存在。我还尝试了Joomla!2.5.9中提供的
.htaccess`中的规则,得到了相同的结果。有任何建议吗?是的,请复制粘贴完整集规则。可能您缺少选项行,我刚刚用
多视图更新了该行。我缺少多视图选项。添加了,但仍然出现404错误。我将htaccess文件添加到问题中。我在根目录中的文件中尝试了您的独立答案。问题中的附加文件位于
news
文件夹w中这里安装了Joomla!。@pQB我看到您在问题中向文件添加了此规则集,重写库是
/news
。我相应地更新了我的答案。应该可以。如果仍然失败,很可能与文件中的其他规则有冲突。我明白了。问题基本上是重写库。它正在工作。您确定了吗.
##
# @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.