Htaccess 301 redirect with variables

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?
Adam JonesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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

0
Dr. KlahnPrincipal Software EngineerCommented:
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
0
Adam JonesAuthor 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
0
Adam JonesAuthor Commented:
I solved it by removing the / before the $. This was my mistake from when I think the URLs used to have .html at the end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
htacces

From novice to tech pro — start learning today.