Welcome to WebmasterWorld Guest from 23.23.53.177

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 rewrite rule needed to simplify url's

301 rewrite rule needed to simplify url's

   
3:17 pm on Jun 11, 2011 (gmt 0)

10+ Year Member



I need to rewrite (301) a few hundred url's and it would be good if this was possible with just one rule. I had a look at some instructions / explanations, as well as an old htaccess file which had something in it related to this and have come up with something (see below). I have to admit I don't understand half of what I have written, so am not at all sure that it is correct.

All the old url's are like this:
www.example.com/oldconstant-somekeyword-someotherkeyword-sometimesanotherkeyword-sometimes...-unique#.html

The new url's are supposed to be like this:
www.example.com/newconstant/unique#.html

I hope this makes sense. I basically want to change the first term (constant), keep the unique# (which has between one and three digits), and replace whatever is in between with '/'.

So far I got the following:

^/newconstant/([0-9]*).html$ http://www.example.com/oldconstant(.*?)-([0-9]*).html$ [R=301,L]

Any help would be much appreciated!
5:05 pm on Jun 11, 2011 (gmt 0)

10+ Year Member



seems too late to edit my post: I had the rule the wrong way round. I now tried this:

RewriteRule ^oldconstant(.*?)-([0-9]*).html$ http://www.example.com/newconstant/([0-9]*).html [R=301,L]

Unfortunately it results in this:

"www.example.com/newconstant/([0-9]*).html", it doesn't deliver the id-number.
6:12 pm on Jun 11, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



You should try this on the left:

First grouping: ([^-]+-)*

Second grouping: ([0-9]+)

Don't forget to escape the literal period too.

On the right, you'll need $2 in the target URL.
8:01 pm on Jun 11, 2011 (gmt 0)

10+ Year Member



Thanks a lot! I tried the following:
RewriteRule ^/oldconstant([^-]+-)*-([0-9]+).\html$ http://www.example.com/newconstant/$2.html [R=301,L]
but it didn't work (address in the browser stayed the same).

It does seem to work when I use this:
RewriteRule ^oldconstant(.*?)-([0-9]*).html$ http://www.example.com/newconstant/$2.html [R=301,L]
(it does not work when I use .\html&)
It seems to 'think' for a little while, so it probably isn't quite right.

Would it be ok to use, or should I try something else?
8:09 pm on Jun 11, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



but it didn't work (address in the browser stayed the same).

It's supposed to :) That's one of the differences between rewrite and redirect. The question is, are you physically on the correct page, independent of what the browser's address bar says?

Whoops!
.\html$

If that's really what your fingers typed in the .htaccess, it might explain a lot. Go back and fix it to say
\.html$

so you're escaping the period, not the letter "h" :)
8:33 pm on Jun 11, 2011 (gmt 0)

10+ Year Member



Thank you!
I corrected the mistake and now the second version is working with the 'escape'!
It didn't make a difference to the first one though.
I thought I was doing a 301 redirect using a rewrite.? The address does change - when I type in the old one it changes to the new one.
9:14 pm on Jun 11, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The first one will work if you happen to have a double hyphen in your URL request (try it), as there are too many hyphens in the RegEx pattern.

Remove the hyphen "on its own" from the pattern to make it work with single hyphens in URL request.


There are several related directives for use on Apache servers. Redirect and RedirectMatch are very basic and can be used to generate 301 and 302 redirects. RewriteRule is much more versatile. It can be used to generate 301 and 302 redirects or it can be used to rewrite external URL requests to a non-default internal filepath.

You should never mix Redirect or RedirectMatch directives with RewriteRule directives within the same site. If you use RewriteRule for any of your rules you should use it for all of your rules.
8:38 am on Jun 13, 2011 (gmt 0)

10+ Year Member



Not sure what I did wrong that day, but now it works with the rule originally suggested (and also without the 'hyphen on its own')! Thanks again!
All the rules I am using are Rewrites, so that should be ok. Another thing I learned by searching through the forum is that the '301 rewrites' should go in front of the other ones, which I never knew!
11:30 am on Jun 13, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Yes, it's too late to externally redirect the user to a different URL if you have already internally rewritten the request and the server is therefore just about to pull the content from the server hard drive. Invoking an external redirect at this point exposes the internal server path back out on to the web as a new URL; and that would lead to a very big problem for site indexing and ranking.
11:50 am on Jun 13, 2011 (gmt 0)

10+ Year Member



Wow, I guess I've been lucky! I should probably try and find some time to do a bit more research on the forum, there might well be some other 'time bombs' my sites are sitting on.... Thanks again for the help!