We help IT Professionals succeed at work.

Htaccess 301 redirect with variables

101 Views
Last Modified: 2018-12-20
I am having some problems. I switched over from one e-commerce system to another a while back and this changed the url structure of some of the pages. I was fine with this and set up some rewrite rules. Somehow this has stopped working and I am not sure why.

I need to change

  • /product/{Category-Name}/{URL} to /{URL}
  • /ecategory/{Category-ID}/{URL} to /{URL}
  • /manu/{URL} to /{URL}
  • /news/{category-name}/{URL} to /blog/{URL}

I am using

RewriteRule   ^/?(product|ecategory)/([^\/]+)/(.+?)\$  /$3  [R=301,L]
RewriteRule   ^/?(manu)/(.+?)\$  /$2  [R=301,L]
RewriteRule   ^/?(news)/([^\/]+)/(.+?)\$  /blog/$2/$3  [R=301,L]
RewriteRule   ^/?(newscategory)/([^\/]+)/(.+?)\$  /blog/$3  [R=301,L]
RewriteRule   ^/?([0-9]+)/(.+?)\$  /$2  [R=301,L]

Open in new window


But this is not working now and I don't have enough knowledge of htaccess redirects to work out what is wrong. I have been making changes and looking online but I feel like I am going in circles and this is very confusing.

Please, can anyone give me some advice?
Comment
Watch Question

David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
I'll attach the template I use for creating sites which forward all QUERY_STRING data, on forwards.

This shows one way for accomplishing what your targeting...

<VirtualHost *:80>
   ServerName  www.WEBSITE
   ServerAdmin support@WEBSITE
   RewriteEngine on
   RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
   RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [NC,L,R=301]
   Include logging.conf
</VirtualHost>

<VirtualHost *:80>
   ServerName  WEBSITE
   ServerAdmin support@WEBSITE
   RewriteEngine on
   RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R=301]
   Include logging.conf
</VirtualHost>

<IfModule mod_ssl.c>

   <VirtualHost *:443>

      ServerName  www.WEBSITE
      ServerAdmin support@WEBSITE

      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
      RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [L,R=301]

      Include logging.conf

      SSLEngine on
      SSLUseStapling on

      SSLCertificateFile    /etc/letsencrypt/live/WEBSITE/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/WEBSITE/privkey.pem

      # Enable HTTP Strict Transport Security with a 2 year duration
      Header always set Strict-Transport-Security "max-age=63072000; preload"

   </VirtualHost>

   <VirtualHost *:443>

      ServerName  WEBSITE
      ServerAdmin support@WEBSITE

      DocumentRoot /sites/OWNER/WEBSITE/TYPE

      <Directory /sites/OWNER/WEBSITE/TYPE>
          Options +Indexes +FollowSymLinks
          AllowOverride All 
          Require all granted
      </Directory>

      Include logging.conf

      SSLEngine on
      SSLUseStapling on

      SSLCertificateFile    /etc/letsencrypt/live/WEBSITE/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/WEBSITE/privkey.pem

      # Enable HTTP Strict Transport Security with a 2 year duration
      Header always set Strict-Transport-Security "max-age=63072000; preload"

   </VirtualHost>

</IfModule>

Open in new window

Dr. KlahnPrincipal Software Engineer
CERTIFIED EXPERT

Commented:
If you are using Apache 2.4, regexes and rewrites should be within double quotes.  Under 2.2 and previous this was not enforced.  The regexes and rewrites shown above would be workable (without regard to whether they are correct or not) under 2.2 but not under 2.4.

See the examples for 2.4 at the Apache project page below:

https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Author

Commented:
Hi

Thanks for the advice but this does not work either. I am pretty sure this was not the problem because there are other rewrites that are not in quotes that are working fine.

I have edited the htaccess so these are the only these rewrites and it still does not work.

RewriteEngine On
RewriteBase /
RewriteRule   "^/ecategory/([^\/]+)/(.+?)\$"  "/$3"  [R=301,L]
RewriteRule   "^product/(.*)$" "/$1" [R=301,NC,L]
RewriteRule   "^/?(manu)/(.+?)\$"  "/$2"  [R=301,L]
RewriteRule   "^/?(news)/([^\/]+)/(.+?)\$"  "/blog/$2/$3"  [R=301,L]
RewriteRule   "^/?(newscategory)/([^\/]+)/(.+?)\$"  "/blog/$3"  [R=301,L]
RewriteRule   "^/?([0-9]+)/(.+?)\$"  "/$2"  [R=301,L]

Open in new window


So, for example, I go to https://www.mysite.com/ecategory/10101/cat-name - I want it to go to https://www.mysite.com/cat-name but it does not change at the moment (keeping in mind that the number will change so it could be https://www.mysite.com/ecategory/999/cat-name for example) - Same with all the others apart from product one (RewriteRule   "^product/(.*)$" "/$1" [R=301,NC,L]) but this does not have a second folder.

The forwarding I have for non-www to www, https and the product one is working fine

Thanks
Adam
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions