Link to home
Start Free TrialLog in
Avatar of Adam Jones
Adam Jones

asked on

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?
Avatar of David Favor
David Favor
Flag of United States of America image

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

Avatar of Dr. Klahn
Dr. Klahn

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
Avatar of Adam Jones

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Adam Jones
Adam Jones

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial