Forum Moderators: bakedjake
Also, I'd like to do the same for [widgets.org...] to [widgets.com...]
Try this:
Introduction to mod_rewrite [webmasterworld.com]
Also, follow the links in there to the Apache docs and to the Regular Expressions tutorial.
HTH,
Jim
That thread...
An Introduction to Redirecting URLs and mod_rewrite on an Apache Server [webmasterworld.com]
was definately outstanding. Maybe that link above with some nice anchor text will help for searching (both here and outside WebmasterWorld?)
Thanks for pointing this one out.
I have a large number of web pages here : All of those web pages are now located here [2]: I'm looking for a mod_rewrite rule that will redirect users from [1] to [2]. I'm guessing the rule will use regular expressions, but not knowing much about mod_rewrite or regular expressions, I can't say for sure. Can anyone help. Thanks very much in advance! [1][edited by: rcjordan at 7:59 pm (utc) on May 21, 2003]
somedomain.com/dir/archives/9999/99/file_name.shtml
where 9999 and 99 are the year and month of publication, respectively
somedomain.com/archives/9999/99/file_name.shtml
[edit reason] no references to your site, please [/edit]
Essentially, you're asking for someone to write some code for you. Understand that many of our regulars are pros and get paid for this kind of work. Someone may choose to write some code for you, but our preference on this board is to help people learn to solve their own problems. There are just too many members with too many problems for us to address them all!
My suggestion is to dig into the resources listed, find some examples that are close to what you need, and create your own code. If you need help debugging it, we have some wizards here that will be happy to help.
Again, welcome aboard!
To rewrite internally:
RewriteRule /dir/archives/([0-9]+)/([0-9]+)/([^/]+)\.shtml$ /archives/$1/$2/$3.shtml [L]
To redirect the user's browser (temporary redirect 302)
RewriteRule /dir/archives/([0-9]+)/([0-9]+)/([^/]+)\.shtml$ /archives/$1/$2/$3.shtml [R,L]
To redirect the user's browser (permanent redirect 301)
RewriteRule /dir/archives/([0-9]+)/([0-9]+)/([^/]+)\.shtml$ /archives/$1/$2/$3.shtml [R=301,L]
Peter
Peter: Thanks for your help! I found a great tutorial [sitescooper.org] to help me understand that code. However, it's not working as I expected it to; no redirect happens.
Here's the code I'm using:
RewriteEngine on
RewriteRule /dir/archives/([0-9]+)/([0-9]+)/([^/]+)\.shtml$ /archives/$1/$2/$3.shtml [R=301,L]
From my understanding, $ is used to indicate the end of a line, so why is it used before 1, 2 and 3? The examples in the tutorial above use \1 instead of $1. What's the difference here?
On the left side of a RewriteRule, the dollar sign is an "end anchor" used by regular expressions.
On the right side, the $1, $2, and $3 symbols are mod_rewrite backreferences which refer back to the parenthesized sub-patterns in the left side of the rule. This is how mod_rewrite can rearrange the "pieces" of your URL.
If you have installed this code into a .htaccess file in your directory structure, rather than into your server configuration file (httpd.conf), the rule needs to be modified as follows, by removing the preceding slash for the pattern. I also suggest a "tweak" to the pattern for $3 as well:
RewriteEngine on
RewriteRule ^dir/archives/([0-9]+)/([0-9]+)/([^\.]+)\.shtml$ /archives/$1/$2/$3.shtml [R=301,L]
Jim
RewriteRule ^dir/archives/([0-9]+)/([0-9]+)/([^\.]+)\.shtml$ /archives/$1/$2/$3.shtml [R=301,L]
I have a number of files named:
^dir/archives/([0-9]+)\.shtml$
that I want to redirected to the same place as specified on the right side of the last RewriteRule (/archives/$1/$2/$3.shtml). Is this possible?
Thanks again.
If I understand your question, the answer is "No." HTTP is a stateless protocol; Each request for each page, image, script, etc., exists on its own, and the server has no "memory" of any previous request.
It would be possible to redirect somedomain.com//dir/archives/<article_number>.shtml to somedomain.com/archives/9999/<article_number>.shtml, but not to "remember" any previous user association with /archives/<some_year>/. That part would be invariable.
Adding this second rule below the one we've discussed would accomplish a rewrite to a fixed <someyear> subdirectory:
RewriteRule ^dir/archives/([0-9]+)/([^\.]+)\.shtml$ /archives/9999/$1/$2.shtml [R=301,L]
The best way to learn this stuff is to experiment with it (when your server is offline or not very busy!) and I encourage you to do so.
Jim
Good question. But I don't think a simple Redirect can use regular expressions. In that case, maybe I should use RedirectMatch, which can use Regex? Hmm, now I have too many optioins ;)
I guess it comes down to how much power I need? In my case, relatively little. RedirectMatch might make my life easier.
RedirectMatch is a fine alternative to mod_rewrite for many applications, but it is not as powerful. The major advantages of mod_rewrite are that it can produce server-internal rewrites, and that it can redirect conditionally, based upon many environment variables -- including user-defined variables.
However, for simple URL redirects, RedirectMatch is sufficient in the majority of practical cases, since it does include back-reference capability.
Jim