Forum Moderators: phranque

Message Too Old, No Replies

How to redirect this way?

         

ashish2005

5:35 am on Dec 15, 2008 (gmt 0)

10+ Year Member



I want to redirect my old pages to new pages and I do not know how to. My old pages are something like
mydomain.com/old-pages/[urlid]/somepage.html

i want them to redirect to

mydomain.com/pages/[urlid]/somepage.html

I have completely deleted my old-pages and I just want to make sure that the google cache has the new url now and visitors do not get redirect to an error page.

that [urlid] in my url are numbers such as 1, 2, 3 and I have around 300 urls with different [urlid]s

What do I do? Please help me out here. Thanks

g1smd

10:17 am on Dec 15, 2008 (gmt 0)

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



Check out the sticky thread at the top of the page for hints and tips on this.

The code you need will be fairly simple because you are re-using the same number ID.

Use a

RewriteRule
with
[R=301,L]
and make sure that the target URL specifies the full domain name too.

Post your best effort code here as a basis for discussion.

ashish2005

4:21 pm on Dec 15, 2008 (gmt 0)

10+ Year Member



Ok I saw the top most post of this section and found out that the rewriterule with [R=301,L] is

RewriteRule ^(.*)$ %{ENV:askapache}://www.thedomain.com%{REQUEST_URI} [L,R=301]

I have no idea how to convert the above to make it work according to how i need. :(
Which part of it do i edit?
Should I do something like

RewriteRule ^(.*)$ %{ENV:askapache}://www.thedomain.com/old-page/[urlid]/ www.thedomain.com/new-page/[urlid]/[L,R=301]

jdMorgan

4:57 pm on Dec 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like that was copied from another forum, and it's in a thread that I contributed code to... :)

If "somepage" is always the same -- literally "somepage.html", then the following will redirect, copying the "urlid" from the old to the new URL as back-reference $1 :


Options +FollowSymLinks
RewriteEngine on
#
RewriteRule ^old-pages/([^/]+)/somepage\.html$ http://www.example.com/pages/$1/somepage.html [R=301,L]

Change "example.com" to your own domain name.

If "somepage.html" can take other values and you want to redirect them as well, then you'll need to be very, very specific and describe those values. (mod_rewrite is all about "very, very specific")

You may or may not need the first line of code (the Options directive). If you don't need it, your host may not allow you to use it, and it will cause a 500-Server error. If you do need it and don't include it, then you'll also get a 500-Server error. Try with and without that line.

See the resources cited in our Forum Charter, and the threads in the Apache Server section of the WebmasterWorld Library for more background information.

Jim

ashish2005

5:15 pm on Dec 15, 2008 (gmt 0)

10+ Year Member



Wow thank you very much jdMorgan. the last somepage.html was not always the same so i did this and it worked

RewriteRule ^old-pages/([^/]+)/([0-9a-zA-Z?-]+).html$ http://www.example.com/pages/$1/$2.html [R=301,L]

jdMorgan

5:30 pm on Dec 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest this to simplify the second pattern:

RewriteRule ^old-pages/([^/]+)/([^.]+)\.html$ http://www.example.com/pages/$1/$2.html [R=301,L]

Basically, the two subpatterns say, "match until you find a slash" and "match until you find a period" respectively. This simpler pattern will be processed much faster than your three range compares plus character compares.

Note also that "?" will never appear in the URL-path "seen" by RewriteRule; The query string, if any, must be handled separately using a RewriteCond. If you do not append any query string data to the substitution URL in your RewriteRule, then by default, any query string appended to the requested URL-path will be passed through the rule unchanged.

Jim

[edited by: jdMorgan at 5:32 pm (utc) on Dec. 15, 2008]

jdMorgan

5:33 pm on Dec 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And a further simplification would be:

RewriteRule ^old-pages/([^/]+/[^.]+)\.html$ http://www.example.com/pages/$1.html [R=301,L]
:)

Jim

ashish2005

5:55 pm on Dec 15, 2008 (gmt 0)

10+ Year Member



Wow thank you so much. I did the the simple method that you provided and it works like a charm.