Mobile app version of vmapp.org
Login or Join
Shanna517

: Creating a Combined 301 Redirect in .htaccess We are being advised by our SEO agency to reduce the number of "chained" redirects we have. So, for example: URL A - http://www.sitename.co.uk/SHOPPING-CATEGORIES/product_name

@Shanna517

Posted in: #Htaccess #ModRewrite #Redirects

We are being advised by our SEO agency to reduce the number of "chained" redirects we have.

So, for example:
URL A - www.sitename.co.uk/SHOPPING-CATEGORIES/product_name
redirects to its https version
(this is redirect 1, changing http to https)

the redirected url then redirects to the same url but all in lower case. So /SHOPPING-CATEGORIES/ becomes /shopping-categories/ (this is redirect 2, changing all urls to lower case as they are case sensitive)

the redirected url then redirects to
URL B - www.sitename.co.uk/category/product_name (this is redirect 3 which removes the string /shopping-categories because this is no longer used in any url)

The above creates three separate redirects where what is actually wanted/needed is

URL A
redirects to
URL B

How can this be best achieved?

ADDITION: For clarity, this site is on a dedicated server and so it is not set up as a virtual host. Would all this be easier if were using NGINX?

Thanks very much for any input

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Shanna517

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

If you have access to the VirtualHost config for the site, I think mod_rewrite would to be up to the task (when placed in said <VirtualHost> directive block):

<IfModule mod_rewrite.c>
RewriteMap lc int:tolower

RewriteEngine On
RewriteBase /

RewriteRule [A-Z] ${lc:[CO]} [R=301]
RewriteRule ^(.*)/shopping-categories/(.*)$ /category/ [R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) %{SERVER_NAME}/ [R=301]
</IfModule>


You may need to restart the Apache service to re-parse the VirtualHost configuration.

The RewriteMap directive maps a "to lowercase" behavior that's packaged with mod_rewrite, and is the only directive which absolutely must be in the <VirtualHost> directive block. All other directives could be placed in a .htaccess file or <Directory> directive block.


RewriteRule [A-Z] ${lc:[CO]} [R=301]: If the requested URI-path contains an uppercase letter, apply the "to lowercase" map against the entire path and set up a permanent redirect.
RewriteRule ^(.*)/shopping-categories/(.*)$ /category/ [R=301]: If the (now most definitely lowercase) URI-path contains "/shopping-categories/", replace it with "/category/" and set up a permanent redirect.
RewriteCond %{HTTPS} !=on: If the request was not made using HTTPS...


RewriteRule ^(.*) %{SERVER_NAME}/ [R=301]: ...rewrite it using the HTTPS schema and set up a permanent redirect.



As no L or END flags are specified, Apache will test all rewrite rules and redirect only once if any rule was applied.



ADDENDUM

The above directives should function equally as well in a server-level configuration (often httpd.conf). However, a VirtualHost block is preferable as it limits the changes to specifically to applicable requests (and thus resource overhead, behavior alterations, etc.). If your dedicated server does not appear to load a <VirtualHost> directive block for your website at any point you can easily implement one, even if you only plan on hosting one website on the server - this is a fairly standard practice and will not introduce any great complexity nor resource overhead to the webserver. Review the Apache documentation on Configuration Files and the VirtualHost examples for more information.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme