Forum Moderators: phranque

Message Too Old, No Replies

301 redirect of directory to new directory

         

zeus

11:27 pm on Mar 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have now tried all kind of solutions nothing works, I need to redirect 301 a folder with pages to new folder so page rank goes to new folder.

latest test was

RewriteEngine on
RewriteRule ^/gallery/old-gallery(.*)$ http://www.domain.com/new-Gallery/ [L,R=301]

it just wont work, I have placed it in root, in gallery folder, in old-gallery folder nothing works.

[edited by: incrediBILL at 12:12 am (utc) on Mar 23, 2012]
[edit reason] fixed URL formatting [/edit]

g1smd

11:39 pm on Mar 22, 2012 (gmt 0)

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



The / after the ^ is your problem.

The (.*)$ part is redundant since you don't re-use the created $1 back-reference anywhere.

It belongs in root.

Use example.com in this forum to prevent URL auto-linking.

zeus

12:04 am on Mar 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteEngine on
RewriteRule ^gallery/old-gallery http://www.example.com/new-gallery/ [L,R=301]

thanks, but still dont work when I place it in root or gallery

incrediBILL

12:14 am on Mar 23, 2012 (gmt 0)

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



Try:

RewriteRule /gallery/old-gallery http://www.example.com/new-gallery/ [L,R=301]

g1smd

1:30 am on Mar 23, 2012 (gmt 0)

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



The leading slash will not be present in the request.

The code belong in root.

Since it is a "specific" redirect it needs to be listed before the majority of any other redirects you have in your htaccess file.

incrediBILL

2:04 am on Mar 23, 2012 (gmt 0)

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



The leading slash will not be present in the request.


So why do all the Apache samples show the leading slash?
[httpd.apache.org...]

... and people wonder why everyone is confused about Apache when everything you read says one thing and then people tell you just the opposite.

Just say'n...

phranque

8:17 am on Mar 23, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule:
Per-directory Rewrites
* The rewrite engine may be used in .htaccess files and in <Directory> sections, with some additional complexity.
...
* When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitions.
...
* 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.

g1smd

8:30 am on Mar 23, 2012 (gmt 0)

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



The slash is needed when the code is located in httpd.conf.

The slash must be omitted when the code is used in htaccess.

From the wording the OP used, I believe he is using htaccess.

phranque

9:27 am on Mar 23, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The slash is needed when the code is located in httpd.conf.
The slash must be omitted when the code is used in htaccess.

the slash must also be omitted when the code is used in <Directory> sections in httpd.conf.
i.e. ANY directory-specific context

lucy24

9:55 am on Mar 23, 2012 (gmt 0)

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



If you scroll down all the way to the bottom of the 2.2 version:
[httpd.apache.org...]
you get an excruciatingly detailed list of every possible permutation of ^ and / along with all possible targets, with and without [P] and [R]. I guess you could pin it to your bathroom wall and after a few months it would sink in.

The form

/gallery/blabla

with leading slash (but no anchor) will work fine in htaccess if /gallery/ isn't a top-level directory. (That is, there would be other stuff before the / anyway.)

Zeus, you can't put your RewriteRule in the /gallery/ folder because then it's too late. You have to intercept requests before they get that far. "Not so fast there, request, you're not going anywhere."

zeus

10:33 am on Mar 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule /gallery/old-gallery http://www.example.com/new-gallery/ [L,R=301] did also not work, I know I had something that worked for this a Month ago, but it is gone no idea how, but I have no clue how i did it.

phranque

10:49 am on Mar 23, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



if you have access to your server config you could turn on rewrite logging and see if that shows you anything helpful.

incrediBILL

10:56 am on Mar 23, 2012 (gmt 0)

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



From the wording the OP used, I believe he is using htaccess.


Yup, reread from start again, missed that subtlety first pass.

I'm allowed to be dense every now and then :)

zeus

11:05 am on Mar 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



phranque - no idea how to do that, i do have a server with cpanel

phranque

1:04 pm on Mar 23, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



how you do it depends on the version of apache but you will likely need to be able to edit the server config file and restart the server.

RewriteLog Directive:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog

g1smd

8:40 pm on Mar 23, 2012 (gmt 0)

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



@incrediBILL Sure you're allowed to misread the question. If you look back through various threads you'll see I do that at least several times every month. :)

lucy24

9:50 pm on Mar 23, 2012 (gmt 0)

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



Zeus, still with us?

Backtrack a bit please. Give the full url of #1 the old location and #2 the new location. You can say "subdomain.example.com" so long as you don't put http:// in front. The idea is to say in English what you want to do. And then you work out the rule. This is usually more productive than doing it the other way around (first the rule, then saying what you want to do).