简体   繁体   中英

301 Redirect Links to Use .html Suffix

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM