skip to Main Content

I am having an issue with a subdirectory that has many different paths, but on the new website we are no longer using that structure. The old website worked this way:

example.com/photos/photo-1
example.com/photos/photo-2
example.com/photos/photo-3

On our new site we are not using /photos, and my current redirect attempts have not worked

Redirect 301 /photos https://example.com/

ends up redirecting to

example.com/photo-1
example.com/photo-2
example.com/photo-3

Removing the photos subdirectory, but still attempting to access the subdirectory that follow it.

The desired result would be for any attempt to access example.com/photos or example.com/photos/* should redirect to homepage at example.com/

Any help would be appreciated.

2

Answers


  1. This should do it.

    <IfModule mod_rewrite.c>
       RewriteEngine On
       RewriteRule ^photos/(.+) https://example.com/ [R=301,L]
    </IfModule>
    
    Login or Signup to reply.
  2. This probably is the approach that makes most sense:

    RewriteEngine on
    RewriteRule ^/?photos/[^/]+/(.+)$ https://example.com/$1 [R=301]
    

    It uses the rewriting module which means that has to be loaded into the http server. That is the standard setup, though.

    In case it is not all subfolders that should get redirected you can name a pattern instead. This allows for exceptions. For example:

    RewriteEngine on
    RewriteRule ^/?photos/photo-d/+(.+)$ https://example.com/$1 [R=301]
    

    And in case both host are identical for the old and the new "site" you can use an internal redirection to simplify things:

    RewriteEngine on
    RewriteRule ^/?photos/photo-d/+(.+)$ /$1 [R=301]
    

    In general you should try to implement such rules in the actual http server’s host configuration. And only use a distributed configuration file instead (".htaccess") if you have no access to the central configuration. Various reasons.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search