# Htaccess rewrite problem

**URL:** <https://community.modx.com/t/htaccess-rewrite-problem/5519>\
**Category:** Support\
**Created:** [June 28, 2022, 2:05pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519 "2022-06-28T14:05:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 28, 2022, 2:05pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/1 "2022-06-28T14:05:28Z")

</div>

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:

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

```

Amd this is the ECWID integration code:

```auto
<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:

```auto
<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!

---

<div class="post-metadata">

**Author:** ![camicase82](https://community.modx.com/user_avatar/community.modx.com/camicase82/32/2033_2.png) [@camicase82](https://community.modx.com/u/camicase82)\
**Post date:** [June 28, 2022, 9:23pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/2 "2022-06-28T21:23:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 28, 2022, 9:46pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/3 "2022-06-28T21:46:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [June 29, 2022, 6:04am UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/4 "2022-06-29T06:04:41Z")

</div>

> [@tm2000](#):
>
> `RewriteRule ^shop/.*$ shop,html`

Should that maybe be:

```auto
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).

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 29, 2022, 6:25am UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/5 "2022-06-29T06:25:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![halftrainedharry](https://community.modx.com/user_avatar/community.modx.com/halftrainedharry/32/1063_2.png) [@halftrainedharry](https://community.modx.com/u/halftrainedharry)\
**Post date:** [June 29, 2022, 9:04pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/6 "2022-06-29T21:04:33Z")

</div>

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

```auto
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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 30, 2022, 9:22am UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/7 "2022-06-30T09:22:55Z")

</div>

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](http://example.com/shop.html)

to

[example.com/shop/](http://example.com/shop/)

and if you click a product:

[example.com/shop/product-1/](http://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

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [June 30, 2022, 9:41am UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/8 "2022-06-30T09:41:22Z")

</div>

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](http://example.com/product1-p12345)  
[example.com/product2-p67890](http://example.com/product2-p67890)

The htaccess file now looks like this:

```auto
<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:

```auto
<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:

```auto
<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](http://example.com/shop.html) (but I don’t really want the .html suffix - id’ prefer [example.com/shop/](http://example.com/shop/))

Then the products are at:

[example.com/product1-p123456](http://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.

---

<div class="post-metadata">

**Author:** ![bobray](https://community.modx.com/user_avatar/community.modx.com/bobray/32/46_2.png) [@bobray](https://community.modx.com/u/bobray)\
**Post date:** [June 30, 2022, 10:43pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/9 "2022-06-30T22:43:52Z")

</div>

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.

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

```

It should probably execute after their code is finished.

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [July 1, 2022, 2:06pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/10 "2022-07-01T14:06:01Z")

</div>

> [@bobray](#):
>
> ```auto
> currentUrl = window.location.href;
> newUrl = 'shop/' + currentUrl;
> window.history.pushState("object or string", "Title", newUrl);
> 
> ```

Thanks Bob - that actually changes the URL to:

[https://example.com/shop/https://example.com/shop.html](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

---

<div class="post-metadata">

**Author:** ![tm2000](https://community.modx.com/user_avatar/community.modx.com/tm2000/32/178_2.png) [@tm2000](https://community.modx.com/u/tm2000)\
**Post date:** [July 5, 2022, 2:56pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/11 "2022-07-05T14:56:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://community.modx.com/user_avatar/community.modx.com/system/32/2_2.png) [@system](https://community.modx.com/u/system)\
**Post date:** [July 7, 2022, 2:57pm UTC](https://community.modx.com/t/htaccess-rewrite-problem/5519/12 "2022-07-07T14:57:41Z")

</div>

Closed on July 7, 2022, 2:57pm UTC

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”.
