homepage Welcome to WebmasterWorld Guest from 54.161.192.61
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
redirect 301 problem
vampke

10+ Year Member



 
Msg#: 4339394 posted 7:39 am on Jul 14, 2011 (gmt 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?

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4339394 posted 8:21 am on Jul 14, 2011 (gmt 0)

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

There's a couple of thousand previous threads with example code.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4339394 posted 8:23 am on Jul 14, 2011 (gmt 0)

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.

vampke

10+ Year Member



 
Msg#: 4339394 posted 10:20 am on Jul 14, 2011 (gmt 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)

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4339394 posted 12:53 pm on Jul 14, 2011 (gmt 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.

vampke

10+ Year Member



 
Msg#: 4339394 posted 1:27 pm on Jul 14, 2011 (gmt 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.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4339394 posted 6:43 pm on Jul 14, 2011 (gmt 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.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4339394 posted 8:38 pm on Jul 14, 2011 (gmt 0)

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.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4339394 posted 9:00 pm on Jul 14, 2011 (gmt 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".

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved