UGH, another 301 redirect that should be simple

I must not have gotten a good nights sleep last night because what I thought should be a simple 301 redirect isn’t quite doing what I wanted.

Long story short a customer had their site hijacked by a lunatic webmaster so we scraped the old CFM site and created an HTML version until we can get him redesigned. All the file names are exactly the same except what was .cfm is now .html

I tried this:

RewriteRule ^/?([a-z/]+)\.cfm$ $1.html [R=301, L]

and it works but it isn’t redirecting them, so then I tried:

RedirectMatch 301 ^/(.*)\.cfm$ $1.html

Same thing, the file stays .cfm while rendering the page correctly

So what the heck am I doing wrong? Know it’s something stupid, maybe shoveling 2’ of snow melted my brain :smiley:

Welcome back, DC!

Okay, .cfm to .html, eh? Why not:

RewriteEngine on
RewriteRule ^(.*)\\.cfm$ $1.html [R=301,L]
# NOTE:  no space in the flags!

Alternatively,

RewriteEngine on
RewriteRule ^([^.]+)\\.cfm$ $1.html [R=301,L]
# NOTE:  no space in the flags!

Just be sure to change the DirectoryIndex, too!

Regards,

DK

Hey DK, actually been back a while now.

So technically my code was right except for the space in the flags but it is acting as a rewrite, not a redirect … what I am looking for (and maybe I didn’t explain it correctly) is to actually send them to the .html page when they request the cfm page.

Oh and no worries on the DirectoryIndex … different server

DC,

Yup! However, you really should run the test I show in the tutorial to see whether mod_rewrite’s enabled on your new server, too. Aw, if it isn’t, you’d be throwing 500 errors all over the place!

The R=301 tells SEs that this change is permanent, i.e., update their db’s.

Regards,

DK

OK the site is now live and I have tried some tests of both your lines and the damn server is freaking out.

When I use one of the cfm extensions it’s throwing a 404 and the URL it is showing is:

http://{domain}/home/zenwindo/public_html/competitors.html

that is returned in the location bar when I try competitors.cfm

What’s weird is it works on my local machine and not on the live server and yes mod_rewrite is enabled on the server

Any ideas?

DC,

Did you use a / before the redirection? That’s when Apache starts to freak out (can’t tell whether it’s a physical or domain address - and that can be cleared by a restart of Apache).

If you don’t have mod_rewrite enabled, you’d only be getting 500’s.

Regards,

DK

I used the ones you posted, both did the same thing … here’s the entire .htaccess file with the first one you posted

RewriteEngine on
Options +FollowSymlinks

If the URI doesn’t end with .js and .jpeg and .jpg and .gif and .css or any others

RewriteCond %{REQUEST_URI} !\.(js|jpe?g|gif|css|swf|html|jsp|ico|png|xml|xsl|rdf|txt|gz|php|shtml|pdf|zip)$ [NC]

apply transformations

RewriteRule ^(.*)\.cfm$ $1.html [R=301,L]

DC,

:eek2: That list of file extensions is worthless! All you’d really want to do is to redirect if it’s not a file (!-f) but that’s NOT what you’re doing here. Simply:

RewriteEngine on
RewriteRule ^([^.]+)\\.cfm$ $1.html [R=301,L]

will change ANYTHING with a cfm file extension to the same request with an html extension. Isn’t that what you want to do?

Final check: Are you using Apache 2.x? If not, add a / after the leading ^; if you’re not sure, add /?. I’m sure we’ve been through this before but that’s the only thing that can go wrong with such simple code (when mod_rewrite is enabled).

If you continue to get the physical path to your file, RESTART Apache as it’s an Apache error which (from my limited experience) can only be cured by a restart.

Regards,

DK

The extensions were from a copy paste I did out of another file,

Anyways without the / after the first ^ I still get the whole path deal but when I add it I then get the correct url in the location bar but a file now found

For instance:

http://www.zenwindows.com/virgin_vinyl_article.cfm

should resolve to the html file virgin_vinyl_article.html (which is on the server) but instead I get a 404.

That is using this code:

RewriteEngine on
RewriteRule ^/([^.]+)\.cfm$ $1.html [R=301,L]

and yes I have restarted Apache several time and YES mod_rewrite is enabled (this is one of my dedicated servers and I use mod_rewrite on all of them)

this is really driving me nuts!

BTW just checked, it’s Apache 2.2.10

DC,

Dangerous stuff that copy and paste! KNOW what you’re doing before you put it on your server!

Okay, with Apache 2.x, the leading / in the regex will NOT match so

RewriteEngine on
RewriteRule ^([^.]+)\\.cfm$ $1.html [R=301,L]

will merely strip the .cfm and replace it with .html.

That said, there could be other gremlins in your system. Another thread had Options MultiViews which took a filename from the middle of the path to redirect out of context. If the cfm was supported from within the cgi-bin, that place is outside the webspace (generally) and NOT subject to .htaccess (mod_rewrite).

First, as above, the RED / cannot be matched by Apache 2.x. On the other hand, you’re reporting it as giving the correct redirection which is … :eek: What does it do with ^/? rather than ^ or ^/?

Regards,

DK

Not sure about the gremlins at this point … the request for http://www.zenwindows.com/competitors.cfm is throwing a 404 even though the .htacess file should be pointing it correctly:

RewriteEngine on
RewriteRule ^/([^.]+)\.cfm$ $1.html [R=301,L]

BTW I tried it without the /after the first ^ and it gave me the whole path thing.

DC,

May I assume that you can link directly to the .html version of your pages? If not, that’s why you’re getting the 404s.

If you’re still not able to get to the .html version, then you need to post your ENTIRE .htaccess file (and probably PM me your httpd.conf and httpd-vhosts.conf as there may be some little thing in one of those which is upsetting the apple cart).

Did you try it with ^/? as I’ve suggested a couple of times? What happens with that?

Regards,

DK

Did you try it with ^/? as I’ve suggested a couple of times? What happens with that?

Isn’t that in the original post?

Yes all the files are there and work fine.

When I use the ^/? I get the full path thing

This is the entire .htaccess file:

RewriteEngine on
RewriteRule ^/([^.]+)\.cfm$ $1.html [R=301,L]

I will PM you the httpd.conf

Well while we are figuring this out I had to do it the old fashioned ‘ugly way’ with one RedirectMatch line for each file on the server … customer was freaking out