Apache 301 Redirects where original URL contains special characters

I’m looking for some .htaccess Redirect or RewriteRule assistance.

My client has a site that use to have poorly formatted URLs that are being reported by Google Web Tools as 404 errors, for example:

Read More
/calendar-of-events/wanda-hollberg’s-fused-glass-jewelry-trunk-show/

Notice the bad apostrophe, ” ’ ” character?


Here’s another example:

/dining club.cfm

Notice the space, ” “?


One more example:

/calendar-of-events/“a-viticultural-voyage”-wine-dinnerstar/

Notice the bad quotes, ” “ ” and ” ” “?


I’ve tried redirect rules with no success, such as UTF-8 encoding…

Redirect 301 /calendar-of-events/wanda-hollberg%E2%80%99s-fused-glass-jewelry-trunk-show/ /calendar-of-events/
Redirect 301 /dining%20club.cfm /cuisine/
Redirect 301 /calendar-of-events/%E2%80%9Ca-viticultural-voyage%E2%80%9D-wine-dinnerstar/ /calendar-of-events/

As well as trying to use RegExp…

Redirect 301 /calendar-of-events/wanda-hollberg(.*)s-fused-glass-jewelry-trunk-show/ /calendar-of-events/
Redirect 301 /dining(.*)club.cfm /cuisine/
Redirect 301 /calendar-of-events/(.*)a-viticultural-voyage(.*)-wine-dinnerstar/ /calendar-of-events/

But I’m guessing RegExp doesn’t work in Redirects?!

So I tried a RewriteRule, and still no success…

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.*)/calendar-of-events/wanda-hollberg(.*)s-fused-glass-jewelry-trunk-show/$ /calendar-of-events/ [R=301,L]
</IfModule>

Please advise, what am I missing or doing wrong?

Should I use a Redirect or RewriteRule? And how should I write out the rule?


I should also note that this is now on a WordPress site thus the .htaccess file also has…

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# redirect none-www to www
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

# redirect m. to www
RewriteCond %{HTTP_HOST} ^m.domain.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

# redirect legacy. to www
RewriteCond %{HTTP_HOST} ^legacy.domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

</IfModule>

Thank you very much in advance for your suggestions and effort. Cheers.

Related posts

1 comment

  1. You need to get rid of the leading slash in your rule. If you want to match a request for /calendar the regex needs to start with ^calendar, without the leading slash. The slash is removed by mod_rewrite when matching against rules in an htaccess file.

    And like Justin Iurman mentioned, all of your redirects must be before the wordpress routing rule. The order of the rules is very important and the routing rule gobbles up all requests and thus will make your redirects break.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # redirect none-www to www
    RewriteCond %{HTTP_HOST} ^domain.com$
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
    
    # redirect m. to www
    RewriteCond %{HTTP_HOST} ^m.domain.com$
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
    
    # redirect legacy. to www
    RewriteCond %{HTTP_HOST} ^legacy.domain.com
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
    
    RewriteRule ^calendar-of-events/wanda-hollberg`s-fused-glass-jewelry-trunk-show/$ /calendar-of-events/ [R=301,L]
    RewriteRule ^dining club.cfm /cuisine/ [R=301,L]
    RewriteRule ^calendar-of-events/“a-viticultural-voyage”-wine-dinnerstar/ /calendar-of-events/ [R=301,L]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    

    The escape sequences are unescaped before mod_rewrite processes the URI, so you want to add the actual unescaped characters in the regex. Or you can use x##, like:

    RewriteRule ^calendar-of-events/xE2x80x9Ca-viticultural-voyageE2x80x9D-wine-dinnerstar/ /calendar-of-events/ [R=301,L]
    

    The reason why the (.*) groupings didn’t work in your Redirect is because that directive doesn’t use regular expressions. You’d have to use RedirectMatch for that. But you want to stick with mod_rewrite anyways. Since you have wordpress rules, both mod rewrite and mod alias end up getting applied to the same requests, and that will end up breaking your redirects.

Comments are closed.