1

In the .htaccess file I would like to redirect (301):

From: http://www.example.com/photo<anything>
To: http://www.example.com/blog/

Basically, any URL starting with /photo should be redirected to the single URL /blog/. What is the regular expression for this?

1

I found out:

RedirectMatch 301 ^/photo/ http://www.example.com/blog/

1

This will work if your server is apache with mod_rewrite enabled:

RewriteEngine On
RewriteRule ^photo(.*)$ http://www.example.com/blog/ [R=301,L]

and if upper or lower case characters are acceptable, then use this:

RewriteEngine On
RewriteRule ^photo(.*)$ http://www.example.com/blog/ [R=301,L,NC]

Because of the redirect, I'd recommend using the full absolute URL to redirect to instead of a relative URL because an rfc document (I forgot the rfc number) once stated that the Location header on a redirect page must be a full absolute URL but then another rfc document later stated the URL can be relative, but if you want to increase compatibility for every web browser, I'd suggest using absolute URLs for all redirects.

3
  • I do use mod_rewrite. This solution seems similar to the one I've posted above. I guess yours would not limit to photo/ but to anything photo like photography/ for instance.
    – Sam
    Nov 23 '15 at 8:01
  • thats correct. You did state "Basically, any URL starting with /photo should be redirected to the single URL /blog/" Which means "/photo" and "/photograph" and "/photography". Nov 23 '15 at 19:40
  • One of both should be set as valid answer.
    – Froggiz
    Nov 24 '15 at 12:44
0

You don't even need a regular expression. The Redirect directive handles that case easily:

redirect permanent /photo/ http://www.example.net/blog/
redirect permanent /photo http://www.example.net/blog

in the .htaccess file for the example.com server. By default, the redirect directive saves anything additional on the path (/photos/hello) and appends it to the destination (/blog/hello).

6
  • That's not what I want to do. I want to redirect anything starting with /photo to /blog/ . I don't want to append to /blog
    – Sam
    Nov 22 '15 at 13:06
  • /photo to /blog or /photo to /blog/... Nov 22 '15 at 13:14
  • /photo to /blog/ (my urls have a traling slash, if that's the question)
    – Sam
    Nov 22 '15 at 13:23
  • my urls have a traling slash so you mean /photo/ to /blog/? Nov 22 '15 at 13:25
  • I usually use a pair of redirects for that. One for without the slash, and one with the slash Nov 22 '15 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.