.Htaccess重写。 Magento相关

时间:2014-02-06 23:21:32

标签: .htaccess magento

我有一个像这样的网址需要重定向:

http://www.mydomain.com/Category/Product-Page.html

重定向到:

http://www.mydomain.com/product-page.html

这里要提到的事情:

1. "Category" must gone.
2. Uppercase letters must become lowercase letters.
3. I have couple thousand of these kind of urls need to be redirected.

我正在使用一个众所周知的.htaccess代码,可以在网上找到,用条件循环将所有大写字母替换为小写字母。即使不建议在此代码之上添加额外的重写。我已经设法在最顶层添加了一个额外的代码。

RewriteRule ^Category/(.*)$ http://www.mydomain.com/$1 [R=301,L]

这是我的.htaccess文件。

RewriteRule ^Category/(.*)$ http://www.mydomain.com/$1 [R=301,L]

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2

# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]

RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]

</IfModule>

它按预期工作,但我的VSP提供商发送了一个紧急通知,说我的.htaccess重写规则会导致某种内存泄漏,从而占用共享服务器上的所有内存。我的第一反应就是这个。

RewriteRule ^Category/(.*)$ http://www.mydomain.com/$1 [R=301,L]

所以我尝试将此规则放在初始大写后面的小写循环中,并确保Category为类别。一切都停止了。甚至不是大写到小写的规则。

不确定您是否完全理解我的问题,但请尽量帮助我。

1 个答案:

答案 0 :(得分:1)

如果您正在使用当前的.htaccess进行主机操作时遇到困难。也许你可以使用这个PHP/htaccess混合方法。

使用此.htaccess规则代替您当前的.htaccess。

RewriteEngine on
RewriteBase /

# check if Category is in the path and route it through the php function
RewriteCond %{REQUEST_URI} /Category
RewriteRule ^Category/(.*)$ rewrite-strtolower.php?url=$1 [QSA,L]

# if upper case route it through the php function
RewriteCond %{REQUEST_URI} [A-Z] 
RewriteCond %{REQUEST_URI} !/Category
RewriteRule ^(.*)$ rewrite-strtolower.php?url=$1 [QSA,L]

RewriteRule ^(.*)$ $1 [L] 

然后在网站根目录中创建一个名为rewrite-strtolower.php的PHP文件,并将此代码放入其中。

<?php

if(isset($_GET['url'])) {
    $url = $_GET['url'];
    unset($_GET['url']);
    $params = http_build_query($_GET);
    if(strlen($params)) {
        $params = '?' . $params;
    }
    header('Location: http://' . $_SERVER['HTTP_HOST'] . '/' . strtolower($url) . $params, true, 301);
    exit;
}
header("HTTP/1.0 404 Not Found");
die('Unable to convert the URL to lowercase.');

?>

看看这是否适合您并且可以解决内存问题。

我从here借用了部分解决方案。