jens
jens

Reputation: 534

Only redirect the parent folder and not sub folder .htaccess

I am struggling with a redirect rule for the following scenario:

Parent folder/site (landing page)

/about/leaders

Sub sites (link to profile)

/about/leaders/name1 /about/leaders/name2

I need to deactivate the /about/leaders page and redirect to / but, the profiles should still be online.

It must be similiar to this =

RedirectMatch 301 /about/leaders/(.*) /
RedirectMatch 301 /about/leaders(.*) /$1

But this doesn't work. On subprofiles, the part /about/leaders gets remove and only /nameX stays. I couldn't find an answer for that. Maybe other people would like to achieve something similiar.

Anyways, I am glad for every help

Upvotes: 3

Views: 4748

Answers (2)

terryjbates
terryjbates

Reputation: 312

Greeings Jens,

Think you need one line of code

RedirectMatch 301 ^/about/leaders/?$ /

I am anal about using anchors. So first anchor to leading portion of URL with ^. Then you optionally match the trailing "/" with ?. Then anchor that to tail end of URL via $. That should be it. No need for second directive.

Since the more extended URLs of /about/leaders/name do not match the pattern, they should be accessible as normal.

Hope this helps!

Upvotes: 10

anubhava
anubhava

Reputation: 785156

You might be better off using mod_rewrite. Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^about/leaders(/.*|)$ /$1 [L,R=302,NC]

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Upvotes: 1

Related Questions