2013-07-25 64 views
0

我們最近完全重新設計了我們的網站並將其移至不同的CMS系統。我試圖根據舊的網絡結構設置重定向,通過在數百個可能的網址中的任意一箇中識別字符串,將所有內容都指向我們的新網站。爲了這篇文章的目的,我專注於字符串「_xdc.php」。我想http://www.mysite.com/_xdc.php?somerandomstring重寫(301)到http://www.mysite.comPHP的RewriteRule檢測如果url包含「字符串」&替換,並下降後的.php序列?

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

此方法,因爲它不會重定向到該網站的主頁工作至今。但有兩個問題。

  1. 1號問題:後的胡言亂語?正在追加,我不希望它。 基本上是:http://www.mysite.com/_xdc.php?somerandomstring成爲http://www.mysite.com/?somerandomstring,但我希望它只是http://www.mysite.com

  2. 第二個問題:如果我創建一個新的頁面,其中包括字母「XDC」將被重定向。但是,當我試圖限制到^.*_xdc.php*重寫根本不起作用。我確信這與_和。在「_xdc.php」,但我不知道如何迫使讀取這些特殊字符以純文本格式,而不是代碼...

我缺少什麼?我確定它的一些明顯的,但我仍然是htaccess的noob,所以我非常感謝你的幫助!

在情況下,它需要的,我包括我的下面全的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 

回答

0

你的第一個問題可以通過使用QSD(查詢字符串丟棄追加?到重寫URL 固定)標誌(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匹配(且只有網址),您可以使用:

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

您當前的嘗試(^.*xdc.*$幾乎是一樣的匹配xdc)。我不知道_字符的任何特殊含義,但是如果您需要轉義字符,則必須預先配置\(例如,\.將匹配字面點,而不是「每個字符」)。以上規則適用於我。另見the documentation for mod_rewritesome basic information about htaccess regex

+0

謝謝!我很欣賞幫助和參考文檔 – tlynnec

+0

有趣的是,在問題1中添加?完美工作,但試圖改爲使用QSD呈現內部服務器錯誤。不知道爲什麼,但自從?作品,這就是我正在使用的路線 – tlynnec

+0

QSD僅在Apache 2.4.0及更高版本中可用。我已將這些信息添加到我的答案中。 – Sumurai8