ReWrite problem - long query string to completely static URL

Discuss practical ways rearrange URLs using mod_rewrite.

ReWrite problem - long query string to completely static URL

Postby lockwood77 » Mon Aug 10, 2009 6:26 am

Hi, I'm struggling to get a long search query URL rewritten as a nice simple, static .html URL.

The code I've used is below, which generates a 404. If I add R=301 as a flag, I can use the static URL and get redirected to the long search query URL page, but without the 301, it fails (obviously I would like the URL in the address bar to remain as the friendly one).

Code: Select all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule  ^aeg-fridge-freezers\.html$ /index.php?target=products&mode=search&subcats=Y&type=extended&avail=Y&pshort=Y&pfull=Y&pname=Y&pkeywords=Y&cid=31&mid=56&seotitle=AEG+Fridge+Freezers,+AEG+Integrated+Fridge+Freezers,+AEG+Larder+Fridges&seodescription=AEG+fridge+freezers,+AEG+integrated+fridge+freezers,+AEG+larder+fridges+and+AEG+American+fridge+freezers.&seokeywords=AEG+fridge+freezers,+AEG+integrated+fridge+freezers,+AEG+larder+fridges,+AEG+American+fridge+freezers [L]


There are many other RewriteRules in the .htaccess, this is just the first (I've tried it with just one, same issue, so I'm sure it's not a conflict).

Thanks for your help!
lockwood77
 
Posts: 4
Joined: Mon Aug 10, 2009 6:08 am

Postby richardk » Wed Aug 12, 2009 11:02 am

Do you have other rules that send requests to "/index.php?..."?
Does it work with a shorter query string?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby lockwood77 » Thu Aug 13, 2009 1:29 am

Hi Richard, thanks for your attention.

Shorter query strings don't appear to make any difference.

However, there is another rewrite rule on index.php and maybe this is where the problem lies? It's a rule that the shopping cart (CS-Cart) requires and comes after the list of URL rewrites as per my original example:

Code: Select all
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]

</IfModule>


Do you think that's the problem? Is there a way to preserve the above rule whilst also managing to rewrite URLs as I want to?
lockwood77
 
Posts: 4
Joined: Mon Aug 10, 2009 6:08 am

Postby richardk » Fri Aug 14, 2009 1:13 pm

Try replacing
Code: Select all
/index.php?target=

with
Code: Select all
index.php?target=


Are they your only rules?
Does the rule work on it's own?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby lockwood77 » Sat Aug 15, 2009 1:33 am

OK, tried without the forward slash, makes no difference. With the R=301 flag, it works, without, it fails.

I also tried without the Rewrite Base etc. code, but that didn't fix anything either.

There is a long list of 301 redirects (redirect 301 URL URL) after the Rewrite rules, mapping old URLs to new ones line by line, but they don't conflict with the URLs in the Rewrite section.

Can't work this one out!
lockwood77
 
Posts: 4
Joined: Mon Aug 10, 2009 6:08 am

Postby richardk » Sun Aug 16, 2009 7:15 am

Try a simple rule and a different file.
Code: Select all
RewriteRule abc /afile.php [QSA,L]

Code: Select all
RewriteRule ^aeg-fridge-freezers\.html$ /afile.php [QSA,L]

Code: Select all
RewriteRule abc /index.php [QSA,L]

Code: Select all
RewriteRule ^aeg-fridge-freezers\.html$ /index.php [QSA,L]

Code: Select all
RewriteRule abc /index.php?q=s [QSA,L]

Code: Select all
RewriteRule ^aeg-fridge-freezers\.html$ /index.php?q=s [QSA,L]

(Where afile.php exists.)
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby lockwood77 » Wed Aug 26, 2009 10:40 am

Hi Richard, had some FTP problems, apologies for the delay in response.

The problem comes when you introduce index.php as the URL being rewritten - RewriteRule ^aeg-fridge-freezers\.html$ /afile.php [QSA,L] works fine, but try RewriteRule abc /index.php and it throws an error.

So, I wonder why this is happening and how I can structure RewriteRules to get around it?
lockwood77
 
Posts: 4
Joined: Mon Aug 10, 2009 6:08 am

Postby richardk » Sat Aug 29, 2009 2:01 pm

So, I wonder why this is happening and how I can structure RewriteRules to get around it?

Without seeing your whole mod_rewrite/.htaccess file i don't know what's wrong.

You could create a file with
Code: Select all
<?php include('./index.php');

in it and rewrite to that file instead.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 27 guests

cron