Htaccess rewrite cond for rewriting 404 errors on url’s excluded

.htaccessmod-rewriterewritecond

I have this problem with my .htaccess file.

Google has some URL's indexed I don't want (404 Error) in Webmaster Tools.

I need to rewrite URL's that include --, /- or an ending in - like so:

  1. replace -- with -
  2. remove the - after the /
  3. remove the ending -

…but only if the URL doesn't contain product_info.php.

I tried with this but it's not working…

RewriteCond %{REQUEST_URI} !^/product_info\.php(.*)$
RewriteRule ^(.*)--(.*)$ /$1-$2 [L,R=301]
RewriteRule ^(.*)/-(.*)$ /$1/$2 [L,R=301]
RewriteRule ^(.*)-$ /$1$2 [L,R=301]

It's on a Magento Ecommerce Shop with Varnish cache activated.

Edit #1:

Yes, they appear in the 404 error Report!

By not working I mean: The Magento Says "This document has moved here". If I click there, there is an "ESI Block error"

There can be stuff after the product_info.php?product=TestProduct--Green--XXL

In that case, I don't want to replace the -- to - => that's the problem I think.

Edit #2:

Here's the full .htaccess. I removed comments expect the redirects I'm trying to get working because of lot's of space.

DirectoryIndex index.php
<IfModule mod_php5.c>
    php_value memory_limit 6144M
    php_value max_execution_time 18000
    php_flag magic_quotes_gpc off
    php_flag session.auto_start off
    php_flag suhosin.session.cryptua off
    php_flag zend.ze1_compatibility_mode Off
</IfModule>
<IfModule mod_security.c>
    SecFilterEngine Off
    SecFilterScanPOST Off
</IfModule>
<IfModule mod_deflate.c>
</IfModule>
<IfModule mod_ssl.c>
    SSLOptions StdEnvVars
</IfModule>
<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{REQUEST_URI} ^/rma/
    RewriteRule ^ - [L]

#RewriteCond %{REQUEST_URI} !^/product_info(.*)     
#RewriteRule ^(.*)--(.*)$ /$1-$2 [L,R=301]
#RewriteCond %{REQUEST_URI} !^/product_info(.*)     
#RewriteRule ^(.*)/-(.*)$ /$1/$2 [L,R=301]
#RewriteCond %{REQUEST_URI} !^/product_info(.*)     
#RewriteRule ^(.*)-$ /$1$2 [L,R=301]



    RewriteRule ^api/rest api.php?type=rest [QSA,L]
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
    RewriteRule .* - [L,R=405]

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule .* index.php [L]

</IfModule>
    AddDefaultCharset Off
<IfModule mod_expires.c>
    ExpiresDefault "access plus 1 year"
</IfModule>
    Order allow,deny
    Allow from all
    <Files RELEASE_NOTES.txt>
        order allow,deny
        deny from all
    </Files>
    <Files cron.php>
        Order allow,deny
        Deny from all
    </Files>

Best Answer

The RewriteCond directive only applies to the single RewriteRule that follows it. So, you need to repeat this condition for each rule. For example:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/product_info\.php
RewriteRule (.*)--(.*) /$1-$2 [L,R=301]

RewriteCond %{REQUEST_URI} !^/product_info\.php
RewriteRule (.*)/-(.*) /$1/$2 [L,R=301]

RewriteCond %{REQUEST_URI} !^/product_info\.php
RewriteRule (.*)-$ /$1 [L,R=301]

Alternatively, you can include a single exception at the start of the rules to avoid any rewriting of the /product_info.php file. But this is dependent on the rest of your .htaccess file, since it will exclude /product-info.php from being rewritten at all. For example:

RewriteRule ^product_info\.php$ - [L]

I've also removed the trailing (.*)$ on the CondPattern - that is superfluous in this example. As are the anchors when you have a cacth-all .* pattern (greedy by default).

Make sure all your caches are clear before testing. It is often easier to test with a 302 (temporary) redirect, since these are not (or should not be) cached.

Related Topic