0

I have problems getting an URL to work WITHOUT entering trailing slash.

It's:

www.domain.com/shop/buy/products/show/range/

.htaccess Rewrite Rule is:

RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)/?$  _shop/products.php?trg=${productmap:$1}&range=$2 [L]

It works with trailing slash (which I don't want in the URL) but not without. I should also add that if I was to remove '/show/' from the URL (which I can't do though), it works without trailing slash, or if 'range' contains a dash '-', as in 'new-product', it also works.

However, this URL works with or without trailing slash:

www.domain.com/shop/buy/products/show/range/color

.htaccess Rewrite Rule for this URL is:

RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)/([A-Za-z0-9\-\,]+)/?$    _shop/products.php?trg=${productmap:$1}&range=$2&color=$3 [L]

How can I get the first URL to work without trailing slash? This might be something really obvious as I'm a recent newbie to using .htaccess but I have now spent hours staring at the code and reading forum posts about rewrites but not been able to resolve this. Thank you!

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
Noak
  • 13
  • 1
  • 4
  • Start by clearing the browser's cache, making sure it does, before each test, because both rules have the optional `/?` at the end of the regex. – Felipe Alameda A Jan 24 '13 at 12:02

2 Answers2

0

T'm not so sure, but have You try to put /? in braces: (/)?, I think this will work:

RewriteRule ^shop/buy/([A-Za-z0-9]+)/show/([A-Za-z0-9\-\,]+)([/]?)$  _shop/products.php?trg=${productmap:$1}&range=$2 [L]
bumerang
  • 1,776
  • 21
  • 30
  • Hi Bumerang, thanks for the suggestion but unfortunately it didn't work :( – Noak Jan 24 '13 at 09:11
  • @Noak how about now? `([/]?)` I'm not much familiar with regular expressions. I've update main answer code. – bumerang Jan 24 '13 at 09:15
  • Hey why You don't just skip that: `/?$` unless You realy have to check that it will end after `show/([A-Za-z0-9\-\,]+)` Mayby You can just give: `show/([A-Za-z0-9\-\,\/]+)` – bumerang Jan 24 '13 at 09:45
  • Thanks Bumerang I'd already tried that as well but no joy :-/ – Noak Jan 24 '13 at 11:00
0

You Can Test This Code

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase   /
RewriteRule ^shop/buy/(.*?)/show/(.*)/(.*)$ _shop/products.php?trg=$1&range=$2&color=$3 [S,L,QSA]
RewriteRule ^shop/buy/(.*?)/show/(.*)$ _shop/products.php?trg=$1&range=$2 [S,L,QSA]

sample:

www.domain.com/shop/buy/products/show/range/

www.domain.com/shop/buy/products/show/range

redirect to:

www.domain.com/_shop/products.php?trg=products&range=range

and:

www.domain.com/shop/buy/products/show/range/color

www.domain.com/shop/buy/products/show/range/color/

redirect to:

www.domain.com/_shop/products.php?trg=products&range=range&color=color

one Another way is you Can Use Only This code

RewriteRule ^shop/buy/(.*?)/show/(.*)$ _shop/products.php?trg=$1&range=$2 [L,QSA]

after That split range in php By

list($range,$coler)=explode("/",$_GET['range']); 

It is work too.

mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
  • Thanks Mohammad I can't redirect www.domain.com/shop/buy/products/show/range as I can't find a way to refer to it. I've tried ending the expression with (.*)$ but that didn't help. Thanks though. – Noak Jan 24 '13 at 09:35
  • This Code Redirect With Out Change URL If you Want URL Change You Can Put [R=301,S,L,QSA] instead of [S,L,QSA] – mohammad mohsenipur Jan 24 '13 at 10:20
  • This didn't work unfortunately. To be honest I would prefer for the Regex to work so that the URL could be accessed with and without the trailing slash, rather than to start explicitly redirecting from one page to another. Thanks for your help though. – Noak Jan 24 '13 at 11:00
  • Hi Mohammad, I can see what you are saying and wouldn't mind redirecting if it gets around the problem. However the problem with that is that I can't find a way to insert the url WITHOUT the trailing slash in .htaccess to redirect it. Does that make sense? – Noak Jan 24 '13 at 11:19
  • please put Your sample URL that not work correctly that I now exactly where the problem is – mohammad mohsenipur Jan 24 '13 at 12:04