Forum Moderators: phranque

Message Too Old, No Replies

301s... correct code for Google?

         

whatson

10:38 am on May 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this the correct code?

RewriteRule ^oldurl/ newurl/ [R=301,L]

will that work? or do you need the full url in there?

g1smd

5:20 pm on May 19, 2012 (gmt 0)

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



The redirect target should contain the protocol and domain name.

Is it intentional there's no end anchoring in the pattern and nothing captured and reused in the rule?

lucy24

1:49 am on May 20, 2012 (gmt 0)

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



By "in there" do you mean the "pattern" (the part on the left)? Yes, that's one of the differences between mod_rewrite (RewriteRule) and mod_alias (Redirect). The rest of the path is not automatically reappended so you have to capture and reuse it.

FROM
http://www.example.com/olddirectory/blahblah.html
TO
http://www.example.com/newdirectory/blahblah.html

In mod_alias:

Redirect 301 /olddirectory http://www.example.com/newdirectory

In mod_rewrite:

RewriteRule olddirectory(/blahblah\.html) http://www.example.com/newdirectory$1 [R=301,L]

[edited by: tedster at 10:59 am (utc) on May 23, 2012]
[edit reason] member request [/edit]

whatson

8:32 am on May 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So it should be:

RewriteRule ^http://www.mydomain.com/oldurl/ http://www.mydomain.com/newurl/ [R=301,L]

Then its fine?
.

[edited by: Robert_Charlton at 1:29 am (utc) on May 21, 2012]
[edit reason] disabled auto-linking to display example url [/edit]

phranque

9:12 am on May 20, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule:
On the first RewriteRule, it is matched against the (%-decoded) URL-path (or file-path, depending on the context) of the request.

so that means (since it is matching the path) you leave the protocol and hostname out of the pattern.

and later in the above referenced doc in the Per-directory Rewrites section:
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.

so that means if you are using the RewriteRule directive in .htaccess you also leave out the leading slash.
and if your .htaccess is in a subdirectory of the document root directory that means you leave out the parent directory level(s) and the slash that follows.

whatson

9:28 am on May 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks can you please give me a perfect example of how the code show be then?

lucy24

12:09 pm on May 20, 2012 (gmt 0)

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



Whoops. Can I blame it on the cat?

Should be:

RewriteRule olddirectory(/blahblah\.html) http://www.example.com/newdirectory$1 [R=301,L]

Sorry.

There are some arcane rules about when and where mod_rewrite will ignore a leading slash in the pattern. But the short version is: Don't chance it.

rlange

2:48 pm on May 21, 2012 (gmt 0)

10+ Year Member



RewriteRule ^/oldurl/(.*) http://www.example.com/newurl/$1 [R=301,L]

This will match...

  • /oldurl/
  • /oldurl/foo.htm
  • /oldurl/subdirectory/bar.htm

...and redirect to...

  • http://www.example.com/newurl/
  • http://www.example.com/newurl/foo.htm
  • http://www.example.com/newurl/subdirectory/bar.htm

--
Ryan

lucy24

4:38 pm on May 21, 2012 (gmt 0)

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



This will match...

Was that a response to my post? If so, it's supposed to match all of those. It was meant as an illustration of how mod_alias and mod_rewrite would do the same thing.

whatson

1:32 am on May 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, so can someone write the actual code that should be in the htaccess file please.

g1smd

11:44 pm on May 22, 2012 (gmt 0)

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



There's too few volunteers here to provide a free code writing service. You're the seventh person today to ask for their code to be written for them. We don't do that here. Intead we use the 'teach a man to fish' method.

As per forum charter, post your code and the problems you are having with it and someone will offer some suggestions for fixing. And you learn far more that way.

jimbeetle

3:01 pm on May 23, 2012 (gmt 0)

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



correct code for Google?

Okay, I'll bite: What the heck does an Apache rewrite have to do specifically with Google?