301 Redirect Links to Use .html Suffix

150 Views Asked by At

I'm wondering how I could 301 redirect all urls of the format

/search/(.*)

(where (.*) is not terminated by .html)

to

/search/$1.html

Mainly I'm just not quite sure how I can match the url structure not having .html at the end, that's my issue.

Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

You could use %{REQUEST_URI} as a condition to negatively match against it, like this:

RewriteCond %{REQUEST_URI} !^/search/([^.]+)\.html$ [NC]
RewriteRule ^search/(.*)$ /search/$1.html [R=302,NC,L]

I've used 302 in my above example, simple because when testing I always use 302 to make sure its working so I don't get cached, once you confirm its working, you can safely switch it to [R=301,NC,L].

In other words, if the URL is not search/something.html it will redirect to its version with the ending .html.

NOTE: Keep in mind that the order where you place this rule and your system/CMS if any can affect the result you get. If you had previously tried 301 redirects you might also be cached and it will as well affect the results.