Forum Moderators: phranque
We've just made a few adjustments to the mod_rewrite on our forum. I now want to serve a 301 and new URL to any bot or anything requesting an old format URL.
Rewrite rule looks something like this:-
RewriteRule ^widgets_index.html index.php?name=phpBB2&file=index
RewriteRule ^widgets_index_([0-9]*).html index.php?name=phpBB2&file=index&c=$1
RewriteRule ^widgets_view_([0-9]*).html index.php?name=phpBB2&file=viewforum&f=$1
RewriteRule ^widgets_topic_([0-9]*).html index.php?name=phpBB2&file=viewtopic&t=$1
So, I guess what I need is something that serves a 301 and re-written URL to anyone coming in on the unwritten URL, eg, a request for:-
example.com/index.php?name=phpBB2&file=viewforum&forum=20
...gets a 301 "page has moved to":-
example.com/widgets_view_20.html
Can I do that within the mod-rewrite code, or does this need a seperate rule of some kind?
Many thanks,
TJ
The solution is to use the server variable {THE_REQUEST}, which tests only the original URL requested by the client. It is not affected by any previous internal rewrites for this HTTP request. As an example, to 301 the directly-requested dynamic URLs rewritten by this rule:
RewriteRule ^widgets_view_([0-9]*[b])\.h[/b]tml$ index.php?name=phpBB2&file=viewforum&f=$1 [b][L][/b]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?name=phpBB2&file=viewforum&f=([0-9])*\ HTTP/
RewriteRule ^index\.php$ /widgets_view_%1.html [R=301,L]
An example of the form of the original request header from the browser is:
GET index.php?name=phpBB2&file=viewforum&f=1234 HTTP/1.1
Also note that I added an [L] flag to your original rule, and escaped the literal period in the requested URL. Always use the [L] flag unless you need the output of a given rule to be processed through other rules that follow it. Otherwise, you may waste quite a bit of CPU time, and slow down your server unnecessarily.
Jim