1

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>
MrWhite
  • 12,647
  • 4
  • 29
  • 41
Coach
  • 11
  • 4
  • "google has some URL's indexed i don't want (404 Error) in webmastertools..." - what do you mean by that? Google won't index URLs that return a 404 error. Or if they are already indexed then they will eventually drop from the index. On the hand, do you mean that there are some incorrect URLs that are being accessed (appear in the 404 report) and these need correcting so they can be indexed? – MrWhite Aug 31 '16 at 14:16
  • Do you have any other directives in your .htaccess file? Oh, and define "not working" - does _anything_ happen? Your first rule (`--` to `-`) looks like it should work. – MrWhite Aug 31 '16 at 14:23
  • Can you post an example URL that needs to be redirected. From your "answer" (below) it looks like you might be referring to the query string, rather than the URL-path? – MrWhite Aug 31 '16 at 15:43
  • sorry im pretty new to this system here.. i try to follow your advices.. eg.: eg1: http://domain.com/some-url/stupid--url-notworking => should be redirected to http://domain.com/some-url/stupid-url-notworking eg2: http://domain.com/some-url/stupid--url-notworking- schould be http://domain.com/some-url/stupid-url-notworking eg3: http://domain.com/some-url/-stupidurl-notworking should be http://domain.com/some-url/stupidurl-notworking eg4 domain.com/product_info.php?xx=some--product--name should remain domain.com/product_info.php?xx=some--product--name – Coach Aug 31 '16 at 15:48
  • I'm not sure that you need the `RewriteCond` directives? The "stuff after the `product_info.php`" (the query string) wouldn't get replaced by these directives anyway. The `RewriteRule` _pattern_ only matches against the URL-path, this notably excludes the query string. – MrWhite Aug 31 '16 at 15:55
  • 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. Thanks for your help! – Coach Aug 31 '16 at 15:37

1 Answers1

0

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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41