0

我们最近完全重新设计了我们的网站并迁移到不同的 CMS 系统。我正在尝试基于旧的 Web 结构设置重定向,通过识别数百个可能的 URL 中的任何一个字符串来将所有内容定向到我们的新站点。出于本文的目的,我将重点关注字符串“_xdc.php”。

示例:我想http://www.mysite.com/_xdc.php?somerandomstring将 (301) 重写为http://www.mysite.com.

目前我正在使用这个: RewriteRule ^.*xdc.*$ http://www.mysite.com [R=301,L]

只要它确实重定向到站点主页,此方法就可以工作。不过有两个问题。

  1. 第一个问题: ? 之后的胡言乱语 正在附加,我不希望它。基本上:http://www.mysite.com/_xdc.php?somerandomstring变成http://www.mysite.com/?somerandomstring但我希望它是唯一的http://www.mysite.com

  2. 第二个问题:如果我创建了一个包含字母“xdc”的新页面,它将被重定向。但是,当我尝试限制^.*_xdc.php*重写时根本不起作用。我确定这与 _ 和 . 在“_xdc.php”中,但我不确定如何强制将这些特殊字符读取为纯文本而不是代码......

我错过了什么?我确信它很明显,但我仍然是 htaccess 的菜鸟,所以我非常感谢你的帮助!

如果需要,我在下面包含了我的完整 htaccess:

RewriteEngine On

## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# 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
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.

# redirect any variations of a specific character string to a specific address
RewriteRule ^calendar http://www.mysite.com [R=301,L]
RewriteRule ^.*xdc.*$ http://www.mysite.com [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 /deloro

## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php [L]


ErrorDocument 404 http://www.mysite.com/index.php?option=com_content&view=article&id=60
4

1 回答 1

0

您的第一个问题可以通过将 a 附加?到重写的 url使用 QSD(查询字符串丢弃)标志(docs;可从 Apache 2.4.0 及更高版本获得)来解决。

RewriteRule ^.*xdc.*$ http://www.mysite.com? [R=301,L]
RewriteRule ^.*xdc.*$ http://www.mysite.com [R=301,L,QSD]

对于第二个问题,如果你想http://www.mysite.com/_xdc.php匹配(并且只有那个 url),你可以使用:

RewriteRule ^_xdc\.php$ http://www.mysite.com [R]

您当前的尝试(^.*xdc.*$与匹配几乎相同xdc)。我不知道该_字符的任何特殊含义,但如果您需要转义一个字符,则必须在前面加上 a \(例如\.,将匹配一个文字点,而不是“每个字符”)。以上规则对我来说很好。另请参阅mod_rewrite 的文档有关 htaccess regex 的一些基本信息

于 2013-07-25T22:58:34.193 回答