Jump to content

Customize .htaccess: Change Url Without Sending 301 Twice.


adversor

Recommended Posts

Hello,

 

I am getting crazy on this topic. I'm changing my shop software to Prestashop and need to take care on old URLs. To be most flexible in terms of category and product names, I keep IDs for products and manufacturers. For products internal rewrite works fine, but manufacturers are problematic. Since I have ~ 200 manufacturers in 2 languages, it should be done with some rule in .htaccess.

 

Old URL is something like this:

/shop_content.php?coID=15&manID=76

 

New one should become:

/realmanufacturername-76

 

In rewrite rule, I cannot use {rewrite}={ID}, since '=' equal sign is not allowed, I use {rewrite}-{ID}

 

The problem: Customized rewrite rules in .htaccess work only in advance of the internal Prestashop rule

RewriteRule ^.*$ %{ENV:REWRITEBASE}index.php [NC,L] since the URL will be changed here and header status code will be sent.

 

Not to have my rule overridden by Prestashop I put something like this in advance of  the first line (# ~~start~~ ...):

 

RewriteEngine on
RewriteRule ^.*manID=([0-9]*) /hostname/tempmanufacturer-$1 [R,L]
 
It basically works, but I need to set the flag to [R,L] or [R=301,L]. This causes to send 302 or 301 in the header with the new location of  /hostname/tempmanufacturer-76 in this case. Prestashop will change it to /hostname/realmanufacturername-76 and send header status code 301 again with the finally correct location.
 
Why doesn't it work to pass the modified URL to Prestashop without sending 301 code by changing the flag to [L]? It gives a 404 error.
 
I just want to change a damned '=' to a '-' and pass the URL to Prestashop without sending a status code.
 
Every help is much appreciated. I'm stuck since 2 days with this problem.
Link to comment
Share on other sites

After another day frustration, I finally could solve it by overriding function getController in Dispatcher.php and function canonicalRedirection in FrontController.php

 

In case someone has a similar problem. I do all redirects in php now. Add something like this to function getController :

$manID = Tools::getValue('manID');
if (isset($manID) && is_string($manID) ){
  $controller = "manufacturer";
  $this->request_uri = "/tempmanufacturer-".$manID;
}

You also can work with regular expressions to select the IDs in your old URL, e.g.:

if (preg_match('#^\/.*:\.[0-9]{1,3})\.html$#', $this->request_uri, $w)) {
  $controller = "manufacturer";
  $this->request_uri = "/tempmanufacturer-".$w[1]; 
}

In addition, you have to add the keys after the ? of your old URLs into function canonicalRedirection in FrontController.php, e.g. 'manID', otherwise Prestashop adds the old key after the new friendly URL:

$excluded_key = array('isolang', 'id_lang', 'controller', 'fc', 'id_product', 'id_category', 'id_manufacturer', 'id_supplier', 'id_cms', 'manID');
Edited by adversor (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...