Forum Moderators: phranque

Message Too Old, No Replies

Redirecting a number of pages to one in one line

Can it be done?

         

rubenski

6:06 pm on Jul 1, 2004 (gmt 0)

10+ Year Member



I am pretty sure it can be done, but I can;t get it to work.

Here is what I have:

Redirect /c1/9/id/3.*.html http: //www.myexamplesite.com/c1/9/id/3.superwidgets.html

(the space between http: and // is not there in the real htaccess file)

As you can see I want to send users that request

/c1/9/id/3.oldwidgetarticle1.html
/c1/9/id/3.widgetnonsene.html
/c1/9/id/3.morewidgetnonsense.html
/c1/9/id/3.whattheheck.html
etc..

to /c1/9/id/3.superwidgets.html

With the above line in my .htaccess file forwarding doesn't work. Is this caused by bad syntax or by another line in my .htaccess, namely this one:


Rewriterule ^c([0-9]+)/([0-9]+)/id/([0-9]+).*.html id.php?id_id=$3

jdMorgan

7:31 pm on Jul 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your Redirect syntax is invalid.

Use RedirectMatch [httpd.apache.org], not Redirect, and then use either mod_alias or mod_rewrite to keep things simple.

Jim

rubenski

10:16 pm on Jul 1, 2004 (gmt 0)

10+ Year Member



Phew. I got it working for one page, partly. It redirects to the right page, but?id_id=3 is put at the end of the URL. This is caused by another htaccess rule.

I used this:

RedirectMatch (3\.oldwidgetnonsense)\.html$ http: //www.websiteaddress.net/c1/9/id/3.newwidgetnonsense.html

What I want is actually more advanced than I indicated in my first post.

I use static URLs on my pages and rewrite them to dynamic ones with the following lines:

RewriteEngine on
Options FollowSymlinks
RewriteRule ^c([0-9]+)$ artcat.php?article_type=$1
RewriteRule ^c([0-9]+)/([0-9]+)/([0-9]+).*.html article.php?article_type=$1&article_id=$2&pagenumber=$3
Rewriterule ^c([0-9]+)/([0-9]+)/id/([0-9]+).*.html id.php?id_id=$3

With these lines in the .htaccess file

[websiteaddress.net...] will load the same page as [websiteaddress.net...]

This is not good, because whenever I make a typo in a URL (newidgetnonsense) I create a double page on my site for which you can get penalized by Google / Adsense.

What I need is a .htaccess rule that will redirect (permanent) all typo's and old pages to the right page. I would have to use one line for every page, but that's ok. So /c1/9/id/3.anything.html should go to /c1/9/id/3.the_right_page_specified_by_me.html and after that the correct URL must be converted to a dynamic URL with the rewriterules above.

Would that be possible in this order? Do you perhaps know an article that explains this situation in depth? I find it quite difficult to build this from small examples and code snippets.

jdMorgan

1:26 am on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To solve the trailing query string problem, use mod_rewrite instead of mod_alias:

RewriteRule /3\.oldwidgetnonsense\.html$ http://www.example.com/c1/9/id/3.newwidgetnonsense.html? [R=301,L]

The trailing "?" on the rule causes any query string present to be cleared.

The [L] flag stops mod_rewrite processing if the rule matches and the rewrite is performed. You should use [L] on all rules for URLs that do not require processing by the rules that follow them. Otherwise, you'll slow down your server unnecessarily.

As to the rest, almost anything is possible using mod_rewrite, but you have to be very specific about how your old URLs map to new URLs.

The following references may be useful:
Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular Expressions Tutorial [etext.lib.virginia.edu]

Jim