homepage Welcome to WebmasterWorld Guest from 54.167.177.180
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 redirection (with 2 directories)
I need help with a 301 redirection. The URL contains 2 directories.
eugenie_848



 
Msg#: 4646801 posted 9:52 pm on Feb 19, 2014 (gmt 0)

Hello,

I am having trouble with a 301 redirection set up.

I need to redirect

http://mysite.com/directory1/directory2

to
http://myotherwebsite.com/en/adifferentdirectory



I wrote this in the .htaccess :

Redirect 301 /directory1/directory2/
http://myotherwebsite.com/en/adifferentdirectory



And when I submit the .htaccess file there is an error. The Page is not found and I am redirected to
http://myotherwebsite/en/adifferentdirectory/directory2 . This makes no sense because the redirected URL and the destination URL are mixed.



This code works when there is just one directory :

Redirect 301 /directory1/
http://myotherwebsite.com/en/adifferentdirectory



Any advices?



Thanks

Eugénie

 

lucy24

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



 
Msg#: 4646801 posted 11:45 pm on Feb 19, 2014 (gmt 0)

The Page is not found and I am redirected to http://example.com/en/adifferentdirectory/directory2

I think you mean this the other way around: first you are redirected, and then you end up on a different page.

Are you quoting the rules exactly as they appear in htaccess? I would expect complications when one side ends in a directory slash and the other doesn't.

Fine-tooth-comb your htaccess. It sounds as if there is an earlier rule naming /directory1/ alone, which executes before the request ever gets a chance to see the longer rule. Always put specific rules before general ones, so a rule involving
/directory1/directory2/
comes before anything naming
/directory1/
alone.

Important! Does your htaccess file currently have any RewriteRules at all? If so, you need to change all rules using mod_alias (Redirect or RedirectMatch by that name) to RewriteRules, because mod_rewrite always executes before mod_alias in htaccess.


Now do you see why the Forums rules insist on "example.com"? It can be example dot anything if you're discussing more than one domain.

eugenie_848



 
Msg#: 4646801 posted 3:12 pm on Feb 20, 2014 (gmt 0)

Hello,

Thank you for your answer.

To answer your questions :

I am quoting rules exactly as they appear in the htaccess.

As you recommended, I put the specific rules before general ones.

That works but not for all the URL. For example, I have now in my htaccess :

Redirect301 /directory1/directory-2A http://example.com/directory3
Redirect301 /directory1/directory-2B http://example.com/directory3
Redirect301 /directory1/directory-2C http://example.com/directory3
Redirect301 /directory1/directory-2D http://example.com/directory3
Redirect301 /directory1/ http://example.com/directory3

RedirectMatch 301 ^/directory1/directory2E.*$ http://example.com/directory3

Redirect 301 /directory1/directory-2F http://example.com/directory4



The URL with directory 2A and directory 2C works. The /directory1/ works too. The others redirections don't work.

I don't have any Rewrite Rules and I am using Redirect and RedirectMatch.

For the redirections that don't work, they bring me to a URL that is a mix of the directories of redirected URL and those of the destination URL.

That means that at the ends my URL is : http://example.com/directory4/directory2B.

I hope it is clearer.

Thank you for your help.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4646801 posted 4:26 pm on Feb 20, 2014 (gmt 0)

welcome to WebmasterWorld, eugenie_848!


have you cleared your browser cache before testing server directive edits?
you may be seeing some old cached redirects.

eugenie_848



 
Msg#: 4646801 posted 5:20 pm on Feb 20, 2014 (gmt 0)

Hi,

Thanks for the welcoming.

So, finally, the answer was :

1 - The general URL was before the specific URLs. Thanks Lucy21 for that tip.

2 - A cache problem. Thanks Phranque :)

lucy24

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



 
Msg#: 4646801 posted 11:15 pm on Feb 20, 2014 (gmt 0)

RedirectMatch 301 ^/directory1/directory2E.*$ http://example.com/directory3

directory3-what? When using RedirectMatch, you have to spell out the target using captures. Otherwise the target is taken as-is, with nothing reappended. If it's a real, physical directory, call it /directory3/ since that's its real name. Otherwise every redirect will be followed by a second redirect for the directory slash.

For the redirections that don't work, they bring me to a URL that is a mix of the directories of redirected URL and those of the destination URL.

That's why I suspected a rule-ordering problem. For example:

rules
Redirect 301 /directory http://www.example.com/dir2
Redirect 301 /directory/innerdirectory http://www.example.com/dir3

request:
/directory/innerdirectory/filename.html

result:
http://www.example.com/dir2/innerdirectory/filename.html

because the rules execute in the order you list them.

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