2

I have the below code in my htaccess file:

RewriteCond %{REQUEST_URI} !\.(gif|css|png|js|jpg|jpeg|html)$ [NC]
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^deals/(.+)/(.+)/$ deals.php?make=$1&model=$2 [L,QSA]

This means I have SEO friendly urls - rather than going to: URL.com/deals?make=Samsung&model=500CC (Old SEO unfriendly URL)

I can use: URL.com/deals/Samsung/500CC/

The issue I have is if a user has bookmarked one of my old urls, it does not automatically redirect them to the new style. I have tried adding R=301 but that takes users from the new url to the old.

I've then tried switching them around with the R-301 in place but that still does not work for me.

Appreciate if somebody can help me get any hits to my old url, redirected to the new SEO friendly structure.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Will
  • 35
  • 2
  • I should have added, the excluded filetypes after the REQUEST URI seemed to speed up new SEO friendly urls loading. When i removed the exlusions, it was taking an ice age to load. – Will Nov 17 '14 at 13:46

1 Answers1

1

You can have another rule to redirect old URL to new one:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+deals(?:\.php)?\?make=([^\s&]+)&model=([^\s&]+) [NC]
RewriteRule ^ /deals/%1/%2? [R=302,L]

RewriteCond %{REQUEST_URI} !\.(gif|css|png|js|jpg|jpeg|html)$ [NC]
RewriteRule ^deals/(.+)/(.+)/$ deals.php?make=$1&model=$2 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Why would I use a 302 redirect here? I looked it up and this suggests it is temporary unless I have misunderstood .... – Will Nov 17 '14 at 16:18
  • 302 is good for testing, once you've tested it change to 301. – anubhava Nov 17 '14 at 16:19
  • Unfortunately not @anubhava . The bottom half works fine with mapping to new SEO friendly urls. it is the redirect which isnt functioning as expected. Currently with your code when user navigates to: URL.com/deals?make=Samsung&model=500CC they are redirected to URL.com/deals/?make=Samsung&model=500CC notice the additional / has been added but variables are left as is....Any ideas? – Will Nov 17 '14 at 18:14
  • deals is a php file 'deals.php' when i use the directory /deals/ for my SEO friendly URL, this is not a real directory, just for the purpose of the URL. the .htaccess file is i suppose the level above the virtual deals directory, it is in the same home folder as my index.php and deals.php pages. – Will Nov 17 '14 at 19:04
  • The updated rule worked perfectly. Thanks for sticking with it. – Will Nov 17 '14 at 20:38
  • For anyone that reads this at a later date - be sure to change the typo in the first line of the answer - RewriteEgine On should be RewriteEngine On. Will mark it as answer accepted now. – Will Nov 17 '14 at 20:49