Will
Will

Reputation: 35

htaccess mod rewrite not redirecting automatically

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.

Upvotes: 2

Views: 69

Answers (1)

anubhava
anubhava

Reputation: 785128

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]

Upvotes: 1

Related Questions