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:
/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.
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.
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:The reason why the
(.*)
groupings didn’t work in yourRedirect
is because that directive doesn’t use regular expressions. You’d have to useRedirectMatch
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.