Welcome to WebmasterWorld Guest from 54.198.62.217

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

redirect 301 problem

     
7:39 am on Jul 14, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:May 13, 2004
posts: 122
votes: 0


Hi guys,

I have been googling this for hours, but the solutions I am finding are not working for me.

I need to redirect a directory http://www.example.com/subdir to a new site in the root: http://www.example.org/

redirect 301 /subdir/ http://www.example.org/


only works for the index file, all the other files return a 404 error on the .org site.

Both sites use etomite CMS and have rewrite rules in the htaccess file, but I dont think this is the problem, or could it be?

Anyone any ideas what could be wrong?
8:21 am on July 14, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Use a RewriteRule for this. Use [R=301,L] flags too.

There's a couple of thousand previous threads with example code.
8:23 am on July 14, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


redirect 301 /subdir/ http://www.example.org/

only works for the index file, all the other files return a 404 error on the .org site

It's not totally clear what you're trying to do: send each individual file to its counterpart in the other domain, or send everything to the index page in that other domain?

As written, the redirect [httpd.apache.org] takes

http://www.example.com/subdir/somestuff
http://www.example.com/subdir/somemorestuff
http://www.example.com/subdir/someotherstuff

and sends it to

http://www.example.org/somestuff
http://www.example.org/somemorestuff
http://www.example.org/someotherstuff

This is exactly the way to do it if you have relocated the entire contents of example.com/subdir/ to the top level of example.org.

But if what you are trying to do is send

http://www.example.com/subdir/anythingwhatsoever

to

http://www.example.org/index.html
(written as simply www.example.org/)

then you need a RedirectMatch:

RedirectMatch 301 /subdir/.* http://www.example.org/

in order to take anything within /subdir/ and send it all to the same place.
10:20 am on July 14, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:May 13, 2004
posts: 122
votes: 0


@lucy: it is the first option i need: i transfered the entire subdirectory to a new domain.
All the pages have there coutnerpart on the .org domain.
Unfortunately the redirect does not work (only works for the index page - the others are redirected to a 404).
From what I have found the line in my first post should transfer it to the right page, no?
I did try :
RedirectMatch 301 /subdir/ http://www.example.org/

to no avail.

I have tried
RewriteRule ^/subdir/$ http://www.example.org/ [L,R=301]


but this redirects to the 404 on the current domain (example.com)
12:53 pm on July 14, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


That's because the pattern you need is
^subdir/


Leading slashes are removed before pattern matching takes place.

The rule with incorrect pattern does not "redirect to a 404". It returns a 404 response at the originally requested URL. 4xx codes are NOT redirects.

Additionally, you do NOT need the $ end anchor as you DO want to match all paths and files under this folder.

If you use RewriteRule for any of your rules, you should use it for all of your rules. That is, do not have Redirect or RedirectMatch directives anywhere else within the same site.
1:27 pm on July 14, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:May 13, 2004
posts: 122
votes: 0


ok, thanks g1smd.
i am getting closer....
With

RewriteRule ^subdir/ http://www.example.org/ [L,R=301]

all pages get redirected to the index page on .org.
This is still not what i need, I need the pages to redirect to the corresponding pages on the new domain

If you use RewriteRule for any of your rules, you should use it for all of your rules. That is, do not have Redirect or RedirectMatch directives anywhere else within the same site.


Why is this?

PS: I do understand the 404 header is not a redirect in the same sense as the other redirect in this discussion, my choice of words was probably not entirely correct.
6:43 pm on July 14, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


I need the pages to redirect to the corresponding pages on the new domain.

I scan read the question.
Add (.*) to the end of the pattern and add $1 to the end of the target.

Why is this?

Redirect and RedirectMatch are processed by mod_alias. RewriteRule is processed by mod_rewrite. The .htaccess file is processed in "per module" order. If a previously rewritten (by mod_rewrite) request is then redirected by mod_alias it will expose the rewritten filepath back out on to the web as a new URL.
8:38 pm on July 14, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13210
votes: 347


Redirect and RedirectMatch are processed by mod_alias. RewriteRule is processed by mod_rewrite. The .htaccess file is processed in "per module" order.

Is there a place in the Apache docs that lists processing order of modules? I swear they used to have a Search and now it's disappeared. (This is not complete delusion. They really did change parts of the site just recently.)

vampke, just to confuse you: the act of redirecting can be performed either by the Redirect (or RedirectMatch) command within mod_alias, or by the Rewrite command within mod_rewrite. Conversely, the act of rewriting or aliasing... well, let's not go there. This is why g1 will always redirect haha .htaccess queries to mod_rewrite. Do it all with the same module and you've got one less thing to worry about. I do not follow this rule.

From the user's POV, the difference between Redirect and Rewrite is that in a redirect their address bar changes and in a rewrite it doesn't.
9:00 pm on July 14, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Apache module processing order is the reverse order of the module list in the httpd.conf file.

There's an "optimum" order, but also some variation from host to host.

Some configurations can be "problematical".