Forum Moderators: phranque

Message Too Old, No Replies

htaccess RedirectMatch with removal of extension

         

eriksmith

1:07 pm on Nov 25, 2009 (gmt 0)

10+ Year Member



Hello,

I am trying to redirect some shtml pages from an old message board to a new message board.

Here is the current RedirectMatch I am using:
RedirectMatch 301 /oldforum/(.*) http://new.example.com/forum/showthread.php?t=$1

Now this works. But lets say you are trying to goto an old thread (12345.shtml) for example, it will send you to:
http://new.example.com/forum/showthread.php?t=12345.shtml

The page loads, but I would like to remove the trailing .shtml from the redirect.

I was reading on this forum, and it seems that using something like a "RewriteRule" with a [0-9] accepting rule would be more efficient than using the .* method. But im still not sure how to pull that off correctly going from shtml files to the showthread.php

Thanks for your advice.

[edited by: jdMorgan at 2:48 pm (utc) on Nov. 25, 2009]
[edit reason] example.com [/edit]

jdMorgan

2:47 pm on Nov 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that your $1 back-reference is defined as "(.*)" which means "anything, everything, or nothing", and so includes the trailing ".shtml".

Improving your pattern to specify ".shtml" requests and/but leave that file extension out of the back-reference will very likely fix this problem:


RedirectMatch 301 ^/oldforum/([0-9]+)\.shtml$ http://new.example.com/forum/showthread.php?t=$1

There is a link to a regular-expressions tutorial in our Forum Charter which may prove useful.

Jim

eriksmith

7:55 pm on Nov 25, 2009 (gmt 0)

10+ Year Member



Thanks for your quick reply.

I understand what your saying and it provides useful insight.

However, when I tried changing my RedirectMatch to your format, the redirect no longer works. I checked apache access and error log and do not see any errors.

I'm not sure if this matters, but let me provide you with some more directory structure of the old folders:
RedirectMatch 301 ^/oldforum/([0-9]+)\.shtml$ [new.example.com...]

For the above directive, the ^/oldforum/ is located at:
/var/www/html/forum/messages/12345/*.shtml
there are also other folders such as:
/var/www/html/forum/messages/12346/*.shtml

I'm not sure how important that information is, so feel free to ignore it :)

THanks.

g1smd

8:18 pm on Nov 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Detail is always important, because it is important that the pattern matches the URL requests that it needs to match and does not match URL requests that it should not match.

jdMorgan

3:00 pm on Nov 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So it sounds like the string "/oldforum/" in the code posted above needs to be changed to "/forum/messages/".

However, if "/forum/messages" does not appear in the URLs you are typing, then that implies that these URLs are being rewritten from the URL "/oldforum/nnn.shtml" to the filepath "/forum/messages/nnn.shtml" by other config code on the server, in which case that's a rather important factor that must be taken into account here...

Jim

eriksmith

7:42 pm on Nov 26, 2009 (gmt 0)

10+ Year Member



Sorry, maybe I was a little unclear.

Where I gave the file path, that is the actual path. But where I mention oldforum, it is because I wanted to make it more clear about which was the old part. When I place it in the htaccess, I of course change it to the correct name.

For "So it sounds like the string "/oldforum/" in the code posted above needs to be changed to "/forum/messages/"
I actually am placing the .htaccess file in: /var/www/html/forum/messages/, so I change the string "/oldforum/" in the code to say "/12345/".

For your second point, /forum/messages is appearing in the URL I am typing, there is no rewriting happening. /forum/messages/12345/nnn.shtml would be the url someone would type.

I hope this clears things up. I apologize for any confusion.

eriksmith

8:04 pm on Nov 26, 2009 (gmt 0)

10+ Year Member



Update, I was staring at the htaccess file and figured out the issue.

I took out the ^ from the beginning of the path, and it started working correctly.

I looked at the one I had that worked, but was leaving the shtml on, and noticed that it did not have the ^ symbol before the file path.

RedirectMatch 301 /oldforum/([0-9]+)\.shtml$ [new.example.com...]

Is what ended up working.

I learned that the ^ is actually an important difference, because if you put the ^, you need to give the full folder path based from the root of the web-site. But if you leave it out, you only need to provide the path from where the .htaccess file is located.

Is there an advantage to doing it one way vs another? I figured it might be more efficient to have the .htaccess file in the /forum/messages/ folder, so apache doesn't have to look at the rule with every hit to the site.

jdMorgan

11:20 pm on Nov 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



By all means, start-anchor the pattern with "^" and provide the full URL-path! Without the anchor, then *any* requested URL that traverses this .htaccess file will trigger the redirect, and this can be a 'time-bomb' when months or years later, you wonder why pages in a subdirectory of /fourm/messages/oldforum are also being redirected.

Be as specific as possible to avoid unexpected results -- now or later. In server configuration, neatness and completeness count!

Jim