Forum Moderators: phranque

Message Too Old, No Replies

Redirect 301 htaccess question

Redirect 301 htaccess question

         

cguedez

8:20 pm on Jun 26, 2011 (gmt 0)

10+ Year Member



Hi all,

I am a new member. I don't know much about htaccess and I hope somebody can help me.

My website is: www.carmenguedez.com

I have moved all my .html (paintings) to .php from the gallery folder:

redirect 301 /gallery/tres_llaves.html [carmenguedez.com...]
redirect 301 /gallery/vintage.html [carmenguedez.com...]
redirect 301 /gallery/true_story.html [carmenguedez.com...]
redirect 301 /gallery/peace-and-love.html [carmenguedez.com...]

I have many paintings and I would like to know if I can reduce all these codes to only one code using either redirect 301 or mod_rewrite, whatever is best.

I would appreciate your help.

Thanks,

Carmen

g1smd

8:40 pm on Jun 26, 2011 (gmt 0)

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



Use example.com here to stop forum auto-linking.

You can use a RewriteRule, with a specific pattern to match those requests, capture the wanted parts in the $1 backreference and then externally redirect to a new URL that way.

However, just because you have changed over to using .php files does not mean you have to change the URLs. An internal rewrite could be used to link .html URLs to .php files and no-one need ever know you changed the way the internal functions of your website now work.

cguedez

8:49 pm on Jun 26, 2011 (gmt 0)

10+ Year Member



I'm not sure what you mean ...."You can use a RewriteRule, with a specific pattern to match those requests, capture the wanted parts in the $1 backreference and then externally redirect to a new URL that way"

Can you please provide an example of that?

Thanks for the fast response!

lucy24

9:14 pm on Jun 26, 2011 (gmt 0)

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



Edit: Darn, g1, but you always type faster than me ;)

Rule #1: Use example.com in your examples. This will prevent them from turning into active links. People want to see your exact wording, not go to the site.

Now then. If you're moving everything in the /gallery/ directory, and the word "gallery" doesn't occur in any other addresses, you don't even need a preceding RewriteCond, simply

RewriteRule (gallery/\w+)\.html?$ http://www.example.com/$1.php [R=301,L]

The Redirect version goes:

RedirectMatch 301 (/gallery/\w+)\.html?$ $1.php

If your server doesn't like \w, replace it with [A-B_] and add [NC] at the end.

Your server may demand a full "http ://et cetera" (space added due to Rule #1) in the redirect; mine doesn't seem to care.

Of course you could include the period . in the capture, but I find it intuitively more comfortable not to.

g1smd

9:18 pm on Jun 26, 2011 (gmt 0)

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



I commend the mod_rewrite manual and a good tutorial on Regular Expressions.

There's not enough volunteers here to provide a free code writing service, but there's 50 000 previous threads here in this forum, most of which contain examples of redirects.

Matching folders:
^(([^/]+/)*)


Matching filenames:
([^/.])\.html$


However, you should be thinking about the suggested internal rewrite, and not using an external redirect.

cguedez

10:10 pm on Jun 26, 2011 (gmt 0)

10+ Year Member



@lucy24 and @g1smd thank you so much!

Your information was very helpful. Unfortunately, I won't be able to use the code due to other folders/files contains the word "gallery".

I have to think about using internal rewrite instead of external redirect.

Have a great day!

Carmen