简体   繁体   中英

Creating optimal 301 redirects in .htaccess file

Basically on my website I am re-structuring all the urls to a new format. This means I will need to add some 301 redirects.

Each url is made up of multiple pages, so you have the home page of the URL which would be: http://domain.com/title-of-url/id.html

Then as they increment through the pages: http://domain.com/title-of-url/id/pagenumber.html

The new structure will be this. Home page URL: http://domain.com/title-of-url/ Increment url: http://domain.com/title-of-url/pagenumber

I only have plans to 301 redirect all the home page URLs which stacks up to about 150 URLs roughly.

I know how to do the standard 301 re-direct (i think): Redirect 301 /title-of-url/id.html /title-of-url

Just wondering if there was a better approach instead of adding 150 odd lines into my htaccess?

Since your urls seem to follow a fixed structure, you can use mod_rewrite pretty easily.

RewriteRule ^([^/]+)/[0-9]+\.html$ /$1 [R,L]
RewriteRule ^([^/]+)/[0-9]+/([0-9]+)\.html$ /$1/$2 [R,L]

After you have tested that the rules work as expected, change the R flags to R=301 to make the redirect permanent. Don't test with permanent redirects though, because permanent redirects are cached by the browser and are therefore a pain to use when testing.

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