Welcome to WebmasterWorld Guest from 54.197.90.95

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)

Junior Member

10+ Year Member

joined:May 23, 2005
posts: 93
votes: 0


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 June 11, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:May 23, 2005
posts: 93
votes: 0


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 June 11, 2011 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


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 June 11, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:May 23, 2005
posts: 93
votes: 0


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 June 11, 2011 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13266
votes: 363


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 June 11, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:May 23, 2005
posts: 93
votes: 0


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 June 11, 2011 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


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 June 13, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:May 23, 2005
posts: 93
votes: 0


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 June 13, 2011 (gmt 0)

Senior Member

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

joined:July 3, 2002
posts:18903
votes: 0


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 June 13, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:May 23, 2005
posts: 93
votes: 0


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!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members