Dave Reeder
Dave Reeder

Reputation: 63

301 redirect: redirect urls with .php to folder

In .htaccess I want to redirect urls like this:

/products.php/a7-frames
/products.php/a6-frames

to this:

/picture-frames/a7-frames
/picture-frames/a6-frames

So need to substitute products.php with picture-frames. After a lot of Googling I tried this:

RewriteBase /
RedirectMatch 301 (.*)\.php/?$ https://www.domainname.com/picture-frames$1

But it doesnt work, if I enter this url: /products.php/a7-frames the browser says there are too many redirects and goes to:

/picture-frames/index

It's substituting the products.php for picture-frames which is great, but I'm not sure why it adds "index" on the end rather than the /a7-frames part of the url? How can I fix this?

Upvotes: 0

Views: 41

Answers (1)

anubhava
anubhava

Reputation: 786091

You can use these rules in your site root .htaccess:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+products\.php/([^\s?]+) [NC]
RewriteRule ^ /picture-frames/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^picture-frames/([^/]+)/?$ products.php/$1 [L,NC]

Make sure to clear your browser cache before testing this change.

Upvotes: 1

Related Questions