Htaccess rewrite problem

Hi all.

I have a modx site already, which I want to add an online store to.
I have used ECWID in the past, which is a really good cost-effective platform.

But for some reason, MODX is throwing out the redirect rules, so it won’t work.

This is the code they suggest adding to the htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^shop/.*$ shop,html
</IfModule>

Amd this is the ECWID integration code:

<script>
    window.ec = window.ec || {};
    window.ec.config = window.ec.config || {};
    window.ec.config.storefrontUrls = window.ec.config.storefrontUrls || {};
     
    window.ec.config.storefrontUrls.cleanUrls = true;
    window.ec.config.baseUrl = '/shop';
    </script>
    

<div id="my-store-76255255"></div>
<div>
<script data-cfasync="false" type="text/javascript" src="https://app.ecwid.com/script.js?XXXXXXXX&data_platform=code&data_date=2022-06-27" charset="utf-8"></script><script type="text/javascript"> xProductBrowser("categoriesPerRow=3","views=grid(20,3) list(60) table(60)","categoryView=grid","searchView=list","id=my-store-XXXXXXXX");</script>
</div>

This works as a static html file outside of modx (but in the same directory as modx)
but I can’t use the above in MODX, because my site doesn’t use .html extensions (it just uses ‘/’),

So I changed the .htaccess to:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^shop/.*$ shop
</IfModule>

And what happens is the page loads when I preview it in Modx - but when I try to reload the page, it throws a 404.

I guess it’s hard to explain the issue if you haven’t used ECWID before, but their support hasn’t come up with any solutions so wondered if anyone here knows what do to.

Sorry for the vagueness! Not sure how else to explain it.

Thanks in advance!

Hi there, if I understand correctly, when you see it while logged into the manager, it works but doing it in a different browser or incognito you get a 404?

Make sure that the page is published, and also see if there are any underliying errors on the console

Hi there.

No it doesn’t matter if I’m logged in or not. The page is published and there’s no errors in the console.

The page loads fine. But when I choose a product it also loads fine e.g shop/product1. But it’s when I reload THAT url (or paste it into a browser) I get a 404 error.

I’ve managed to work out that it’s something to do with modx. Because if I crest the identical html outside of modx but in the same domain, it works fine.

Should that maybe be:

RewriteRule ^shop/.*$ shop.html

And is .html your suffix in MODX (rather than .htm, or nothing, which might be the case if shop is a folder).

Sorry bob. The comma was a typo. It is shop.html.

But in content types I have html set as the suffix / (so that urls appear without .html).

So I tried using simply ‘shop’ instead of ‘shop.html’ but it doesn’t work.

I’m not sure I understand the problem. Is it correct, that you have a resource in MODX with the alias shop (url = www.yourdomain/shop and you want that all requests like www.yourdomain/shop/product1 and www.yourdomain/shop/product2 are redirected to this resource?


Maybe try something like this

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shop/.*$ index.php?q=shop [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

or maybe try adding the DPI flag to the rule.

RewriteRule ^shop/.*$ shop [QSA,DPI]

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

It’s also possible to write a custom plugin that runs on the event OnPageNotFound and redirects to correct resource ($modx->sendForward()). With a plugin it’s easier to debug the behaviour.

Thanks Harry - I apologise that I haven’t managed to get across what I’m trying to do.

So the third party ecommerce platform called ECWID is loaded into an html page using an integation script (js).

out of the box, the URLs are horrible with hashbangs - but in their docs, they give additional code to add to the integration to allow for SEO friendly URLs.

They give some additional js code, and also code for the htaccess file.

Their software loads the various products all into the one resource, but updates the URl to reflect the product name.

But the code they give looks like it redirects to itself.

I tried the suggestions you kindly suplied, but they didn’t work. It’s very odd because I can use the same code outside of MODX but with the .html extension and it works as expected. It removed the extension, and adds the product name as pat of the url, so it transforms:

example.com/shop.html

to

example.com/shop/

and if you click a product:

example.com/shop/product-1/

I tried creating a new content type for this page with the .html extension, to fully replicate the functionality of the static html file, but this didn’t work either.

I have even removed the entire htaccess content (apart from what they supplied) to see if something there was breaking it, but it still doens’t work.

So all i can think is that something within modx itself is causing the issue.

I didn’t try the custom plugin you suggested, as not sure what that entails.

Thanks again for your suggestions/time.

Very frustrating!
Andy

So - playing around with the integration code - I have made a little progress - but it’s not right.

I have created a new content type for HTML to have the .html file extension.

All product urls have this structure:

example.com/product1-p12345
example.com/product2-p67890

The htaccess file now looks like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^.*-p[\d]+$ shop.html
    </IfModule>

So it redirects any url with the ‘-pxxxxx’.

But in order for this to work, I had to modify the integration code a little. This is what they supply:

<script>
    window.ec = window.ec || {};
    window.ec.config = window.ec.config || {};
    window.ec.config.storefrontUrls = window.ec.config.storefrontUrls || {};
     
    window.ec.config.storefrontUrls.cleanUrls = true;
    window.ec.config.baseUrl = '/shop';
    </script>
    

but with the baseURL specified, it doens’t work.
If I remove the BaseURL so the code looks like this:

<script>
    window.ec = window.ec || {};
    window.ec.config = window.ec.config || {};
    window.ec.config.storefrontUrls = window.ec.config.storefrontUrls || {};
     
    window.ec.config.storefrontUrls.cleanUrls = true;
    </script>
    

it works!! BUT - we lose the ‘shop/’ part of the url to the site struture is wrong.

Now I have the shop at:

example.com/shop.html (but I don’t really want the .html suffix - id’ prefer example.com/shop/)

Then the products are at:

example.com/product1-p123456

So I need to get ‘shop’ back into the product URLs, and lose the .html from the main shop page

As I write this, I’m sensing that this is not really a MODX problem I suppose - but it’s just strange that it works outside of MODX perfectly.

Obviously it’s tricky because we don’t know what their javascript is actually doing.

I’m not sure this will help, but this JavaScript code will change the current URL, so in theory, it could be used to add the shop/ back. You’ll probably have to mess with it to get the ‘/shop’ where you want it in the URL.

currentUrl = window.location.href;
newUrl = 'shop/' + currentUrl;
window.history.pushState("object or string", "Title", newUrl);

It should probably execute after their code is finished.

Thanks Bob - that actually changes the URL to:

https://example.com/shop/https://example.com/shop.html

I will have a chat with the software company - I might have to hire them to fix this as there’s defintely a clash between modx and the shop software.

Thanks again though

Andy

So for anyone who is interested, or has had the same issue with Ecwid - it turned out to be an apache issue. For some reason the way it rewrites URLs is different to how LiteSpeed does.
We upgraded our server it use LiteSpeed instead of Apache, and it all works perfetly now.

Got the added bonus if faster loading times - but the downside of an addtitional £40 per month for the User license!

Andy

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.