Forum Moderators: phranque

Message Too Old, No Replies

htaccess domain/subdomain 301 redirect

         

bataleon

6:25 pm on Jul 18, 2011 (gmt 0)

10+ Year Member



Need a bit of assistance! Trying to accomplish 301 redirects here via the htaccess file. I moved the shopping cart to a subdomain, and parked the landing page where the cart used to be located.


http://www.example.com/products/part_number
(old shopping cart(new landing page)

to

http://www.subdomain.example.com/products/part_number
(new shopping cart)

Need this to redirect 285 urls. 92 of these are for products, the rest point to images, etc. Figured, its best to redirect everything and keep it all uniform. however! There are 4 top level urls that I need to stay the same! Is there a way to exclude these from being redirected?

http://www.example.com
http://www.example.com/contact
http://www.example.com/about
http://www.example.com/Store

Please pardon my ignorance! I'm completely new to this. I ran searches, and haven't found what I'm looking for...but that might be due to my lack of understanding. I apologize in advance though If this particular issue has been revisited time and time and time again.

Thanks all!

[edited by: tedster at 12:37 am (utc) on Jul 19, 2011]
[edit reason] switch to example.com [/edit]

g1smd

10:44 pm on Jul 18, 2011 (gmt 0)

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



Use example.com to stop forum auto-linking.

Post your example code for discussion.

A preceding RewriteCond can be used to filter the requests.

bataleon

10:57 pm on Jul 18, 2011 (gmt 0)

10+ Year Member



Well, it seems Ive made quite the mockery of myself! Apologies! I'll look into RewriteConditions and hope I can piece the puzzle together.

I don't have any example code to post. I'm here to find out and decipher what that code is supposed to be.

Thanks!

g1smd

11:02 pm on Jul 18, 2011 (gmt 0)

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



There's tens of thousands of prior posts with a lot of code examples to get started.

Mod_rewrite is something you have to learn. It's not possible to simply cut and paste code from a forum and hope it will work.

bataleon

11:15 pm on Jul 18, 2011 (gmt 0)

10+ Year Member



That's why I'm here g1smd. To learn.

Thanks

lucy24

1:11 am on Jul 19, 2011 (gmt 0)

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



Whew. I sent forth a plea for the nearest passing moderator to change that whole first post to example.com so people can see what you're asking about. The normal reaction to the word ".htaccess" is to remember an urgent appointment elsewhere, but eventually someone got brave.

So you want to redirect all occurrences of

www.example.com
to
www.subdomain.example.com

excluding four specific urls? I assume www.example.com means www.example.com/index.something, but do "contact", "about" and "store" refer to directories with index pages, or to extensionless page urls that you're rewriting elsewhere?

Bookmark this page [httpd.apache.org] if you haven't already done so. The tricky part in your case is that by default rewrites operate on the part of the address after the domain name (and before the query string) so you need to put the old name into a condition:

RewriteCond ${HTTP_HOST} www.example.com
RewriteCond %{REQUEST_URI} !^(index\.\w+)?$
RewriteCond %{REQUEST_URI} !^contact/(index\.\w+)?$
RewriteCond %{REQUEST_URI} !^about/(index\.\w+)?$
RewriteCond %{REQUEST_URI} !^Store/(index\.\w+)?$

replacing the "index\.\w+" part with the actual name of your index files-- htm, php or whatever-- which may or may not be part of the request, depending on how it arrived (link, type-in etc).

The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.


(Sorry, I had to quote that because I always forget it myself. No leading slash!)

RewriteRule (.+) http://www.subdomain.example.com/$1 [R=301,L]


:: prolonged detour here as I send a typo report to Apache and then find a page blasphemously headed "When not to use mod_rewrite" :o ::

bataleon

6:03 pm on Jul 19, 2011 (gmt 0)

10+ Year Member



@lucy24 WOW! Let me start by thanking you for the assistance here. It's much appreciated!

Yes, all 280 urls, except www.example.com/index, www.example.com/contact, www.example.com/about.... in my haste, store is actually located at www.subdomain.example.com.

I've been playing with RewriteRule (.+)http://www.subdomain.example.com/$1 [R=301,L]

But (.*) was in the the string, and caused www.example.com to redirect to www.subdomain.example.com/product/part number as I was testing.

All this info is really going to help me out and I thank you for taking the time to have my original post edited and posting a reply. Looking forward to getting home and making this work tonight.

Will respond here as I go.