1

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?

user1301430
  • 179
  • 1
  • 1
  • 11

1 Answers1

2

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.

Sumurai8
  • 18,213
  • 9
  • 58
  • 88
  • Just a quick question I assume I need to add: RewriteEngine on +FollowSymLinks is there anything else I would need to add? – user1301430 Aug 11 '14 at 08:47
  • Assuming mod_rewrite is enabled, that should be sufficient. If it isn't enabled, see [this question](http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2) – Sumurai8 Aug 11 '14 at 08:50
  • Okay thanks, I did a quick test with your code using: http://htaccess.madewithlove.be/ didn't seem to do anything, but once I am home I will upload to my server and try it that way. – user1301430 Aug 11 '14 at 08:56
  • @user1301430 I bet you copied the url from your question and didn't change "id" to an actual id, for example `123`? ;-) – Sumurai8 Aug 11 '14 at 09:19
  • Ah I think I know why It didn't work. I tried it with gallery using a URL which had a number for the page title. EG: domain.com/50-amazing-places/10.html Any idea how you would get around that? I tested your code using one without a number and it works perfectly :) Thanks!! – user1301430 Aug 11 '14 at 09:51
  • `domain.com/50-amazing-places/10.html` should redirect to `domain.com/50-amazing-places/`. If that is not the url you want to get, you'll have to alter the regex. I'll however not change my answer, because this answer answers the question as it is asked. – Sumurai8 Aug 11 '14 at 09:58
  • Ah you are correct this is my mistake. I have an extra parameter within my URL which I didn't mention:domain.com/picture/50-amazing-places/10.html – user1301430 Aug 11 '14 at 10:19
  • Having a `[0-9]+-` before the first capture group would exclude the number from the title. – Sumurai8 Aug 11 '14 at 10:26
  • Ah the number in the URL is fine, don't really mind that. What would you suggest with the middle part of the URL, I assume its just adding another expression to your code. Redirect: domain.com/picture/url-title/id.html to domain.com/picture/url-title/ – user1301430 Aug 11 '14 at 10:45