Welcome to WebmasterWorld Guest from 34.204.189.171

Forum Moderators: bakedjake

Message Too Old, No Replies

mod_rewrite Lesson Wanted

Any good tutorials out there (SIMPLE)?

     
2:47 am on May 17, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 5, 2003
posts:636
votes: 0


I'd like to know how to take page_one.html and cause it to push a 301 error to the visitor and redirect them to pageone.html. How do I do that?

Also, I'd like to do the same for [widgets.org...] to [widgets.com...]

3:02 am on May 17, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Oaf357,

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

4:33 pm on May 17, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:June 9, 2002
posts:41
votes: 0


here is another one:
[engelschall.com...]
4:53 pm on May 17, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 5, 2003
posts:636
votes: 0


Thanks for the assistance. The thread jdMorgan pointed me to was OUTSTANDING.

Now how come I couldn't find that thread with the site search?

11:29 am on May 18, 2003 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 11, 2003
posts:1381
votes: 0


Wow,

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.

7:44 pm on May 21, 2003 (gmt 0)

New User

10+ Year Member

joined:May 21, 2003
posts:26
votes: 0


Those tutorials have been helpful to familiarize myself with mod_rewrite, but I haven't been able to find the answer to my question. What I'm trying to do is this:

I have a large number of web pages here :
somedomain.com/dir/archives/9999/99/file_name.shtml
where 9999 and 99 are the year and month of publication, respectively

All of those web pages are now located here [2]:
somedomain.com/archives/9999/99/file_name.shtml

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]
[edit reason] no references to your site, please [/edit]

1:45 pm on May 29, 2003 (gmt 0)

New User

10+ Year Member

joined:May 21, 2003
posts:26
votes: 0


I'm still trying to figure out a solution to the above example. If anyone can help, I'd really appreciate it! Thank you.
2:27 pm on May 29, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 21, 1999
posts:2141
votes: 0


Welcome to WebmasterWorld joshuakaufman! :)

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!

2:30 pm on May 29, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 31, 2003
posts:386
votes: 0


Hi joshuakaufman:

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

7:08 pm on May 29, 2003 (gmt 0)

New User

10+ Year Member

joined:May 21, 2003
posts:26
votes: 0


DaveAtIFG: Understood. In the future, I'll try to do my best before asking any questions.

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?

7:21 pm on May 29, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


joshuakaufman,

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

9:21 pm on May 29, 2003 (gmt 0)

New User

10+ Year Member

joined:May 21, 2003
posts:26
votes: 0


jdMorgan: Thanks, that worked great! One more question and I'm set. Is there a way to backreference to the right side of the last RewriteRule? For example, I'm using this directive:

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.

9:38 pm on May 29, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


joshuakaufman,

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

8:07 pm on May 30, 2003 (gmt 0)

New User

10+ Year Member

joined:May 21, 2003
posts:26
votes: 0


jdMorgan, everyone else,

Thanks so much for your help. I solved all of my problems with your guidance.

Thanks again,
Joshua Kaufman

3:33 pm on May 31, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 6, 2002
posts:46
votes: 0

Why does he need all that mod_rewrite stuff?

Use this:

Redirect permanent /page_one.html http://www.widgets.com/pageone.html

Redirect permanent /small/green/ http://www.widgets.com/small/lime/

3:50 pm on June 1, 2003 (gmt 0)

New User

10+ Year Member

joined:May 21, 2003
posts:26
votes: 0


spagmoid,

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.

2:32 am on June 2, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 6, 2002
posts:46
votes: 0


No I'm pretty sure it doesn't allow anything fancy like that, so you may need rewrite..
12:34 am on June 3, 2003 (gmt 0)

New User

10+ Year Member

joined:May 21, 2003
posts:26
votes: 0


spagmoid,

Are you talking about Redirect or RedirectMatch? Redirect does not allow regular expressions, but RedirectMatch does, and I've gotten it to do the exact same thing as mod_rewrite.

1:14 am on June 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


The title of this thread was "mod_rewrite Lesson Wanted", so I answered using mod_rewrite.

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

1:20 am on June 3, 2003 (gmt 0)

New User

10+ Year Member

joined:May 21, 2003
posts:26
votes: 0


Thanks, Jim.

New question: if I wanted to send all files below a certain directory to the same URL, how would I do it using mod_rewrite?

My best guess is:
RewriteRule ^dir/.+$ /newURL/ [R=301,L]

2:01 am on June 3, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


joshuakaufman,

That will redirect them to the index of /newURL, if that's what you want to do. It will not redirect requests for the index file of /dir, unless the request is to dir/index.html, dir/index.htm, or similar.

Jim