Search code examples
angularapache.htaccesshttp-redirecturl-rewriting

.htaccess 301 redirect parent URL except children URLs


I need to redirect parent URL to new parent URL but children URLs remain same.

www.example.com/blog -> www.example.com/xyx?cat=11

But below URLs remain same for all children URLs

www.example.com/blog/xyz -> www.example.com/blog/xyz

I have tried below solution but children URLs redirect as well

Redirect 301 /blog /xyx?cat=11

My child URL look like below after add above line in .htaccess file

www.example.com/blog/xyz -> www.example.com/xyx?cat=11/xyz

Update:

my .htaccess file look like below:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Rewrite everything else to index.html
    # to allow html5 state links
    RewriteRule ^ index.html [L]

    RedirectMatch 302 ^/?blog/?$ /xyz?cat=11
</IfModule>

Solution

  • Don't use Redirect directive for this redirect because Redirect just matches starting part of URI to cause the redirect.

    You should use RewriteRule for this to get control of full URI using regular expressions like this:

    RewriteEngine On
    
    RewriteRule ^/?blog/?$ /xyx?cat=11 [L,NC,R=302]
    

    This redirect rule will match /blog or /blog/ but will not affect /blog/xyz.

    Remember to:

    1. Completely clear your browser cache before testing
    2. Change 302 (temporary redirect) to 301 (permanent redirect) after testing