Redirect all pages within subdirectory to new page

Redirect all pages within subdirectory to new page - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, wordpress, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


After redoing my webshop, I need to redirect every URL within a certain directoy to a new page.



All URLs beginning ith example.com/shop/... should be redirected to the page located at example.com/products/.



I'm using this wordpress plugin for redirections, but I don't know if it can handle it or I need to edit the htaccess file or something.



.htaccess:



# 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]
RewriteRule ^shop/ /products/ [R=302,L]
</IfModule>

# END WordPress

Solution :

That WordPress plugin should be able to handle this, but otherwise it's just a one-liner in .htaccess (which will also be more efficient):



RewriteRule ^shop/ /products/ [R=302,L]


The above directive should go after the RewriteEngine and RewriteBase directives but before any other WordPress stuff.



This redirects /shop/<anything> to /products/.



Change the 302 (temporary) redirect to 301 (permanent) when you are sure it's working OK. Temporary redirects are easier to test since they are not cached by the browser.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect