PHP RewriteRule检测url是否包含“string”&替换,& .php之后的丢弃序列?

时间:2013-07-25 22:33:42

标签: .htaccess mod-rewrite

我们最近完全重新设计了我们的网站并转移到了另一个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”中,但我不确定如何强制将这些特殊字符作为纯文本而非代码读取...

  3. 我错过了什么?我确定它显而易见,但我仍然是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
    

1 个答案:

答案 0 :(得分:0)

可以通过使用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匹配(并且只有该网址),您可以使用:

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

您当前的尝试(^.*xdc.*$与匹配xdc几乎相同)。我不知道_字符的任何特殊含义,但是如果你需要转义一个字符,你必须添加一个\(例如\.将匹配一个字面点,而不是每个角色的#39;)。以上规则对我来说很好。另请参阅the documentation for mod_rewritesome basic information about htaccess regex