Mobile app version of vmapp.org
Login or Join
Jessie594

: Htaccess 301 redirect subdirectory one level deeper - why is this not working? it is probably a stupid question, but I have real troubles figuring out, how to redirect /products to /products/item

@Jessie594

Posted in: #301Redirect #Htaccess #Wordpress

it is probably a stupid question, but I have real troubles figuring out, how to redirect

/products to /products/item

It is a simple redirect, no regex needed. The toplevel site /products should just always redirect to /products/item

I tried:

<IfModule mod_rewrite.c>
RedirectMatch 301 /products(.*) /products/item/
</IfModule>


I receive the error, that the webserver is redirecting in an infinite loop. That might be because of the rest of the htaccess file. Which looks like that:

# Custom Rules
<IfModule mod_rewrite.c>
RedirectMatch 301 /products(.*) /products/item/
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


But the strangest thing: When I load the page, I get the error. When I then hit reload, it works???

Thanks for your help!
ole

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Jessie594

2 Comments

Sorted by latest first Latest Oldest Best

 

@Rambettina238

If you just want to redirect /products to /products/item, this should work:

RedirectMatch 301 ^/products/?$ /products/item


The ^ and $ characters anchor the regular expression to the beginning and end of the path, so that it won't match /foo/products or /products/bar. The /? allows it to match both /products and /products/; you can remove it if you don't want that.

10% popularity Vote Up Vote Down


 

@Sue5673885

The (.*) after products is what's causing the loop. It redirects
/products/something to /products/item/something, but then that has to be redirected to /products/item/item/something etc.

You need to exclude item so this may work:

RedirectMatch 301 /products/(?!^item)(.*) /products/item/


This means redirect /products/something except something beginning /products/item.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme