2

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

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 - https://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

  • To be absolutely clear, here: an Apache VirtualHost is nothing similar to a virtual machine. All a VirtualHost block does is tell Apache "the site in this directory will handle these types of requests with this configuration". In all likelihood your dedicated server already has a configuration for your site in a <VirtualHost> block. If it does not, all it takes to add one is two to four additional lines in a configuration file - that's all there is to it. That in mind, entirely switching webservers would be the more difficult and time-consuming solution. – bosco Feb 15 '16 at 10:48
1

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:$0} [R=301]
  RewriteRule ^(.*)/shopping-categories/(.*)$ $1/category/$2 [R=301]
  RewriteCond %{HTTPS} !=on
  RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [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:$0} [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/(.*)$ $1/category/$2 [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 ^(.*) https://%{SERVER_NAME}/$1 [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.

| improve this answer | |
  • Thank you for your input - I much appreciate the effort, but this site is on a dedicated server and so it is not set up as a virtual host. Any other ideas? Would this be easier if were using NGINX? – jdeb901 Feb 15 '16 at 9:43
  • @jdeb901 I think you're misunderstanding what an Apache <VirtualHost> block is. I've clarified in a comment on your question. However, my posted solution should work in the top-level server configuration as well. It's possible that it is easier to achieve your desired outcome with Nginx, but the time and effort required to switch web-servers far exceeds that required to make the change in Apache. – bosco Feb 15 '16 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.