homepage Welcome to WebmasterWorld Guest from 50.19.33.5
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 rewrite rule needed to simplify url's
301 rewrite rule needed to simplify url's
joergnw10




msg:4325002
 3:17 pm on Jun 11, 2011 (gmt 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!

 

joergnw10




msg:4325021
 5:05 pm on Jun 11, 2011 (gmt 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.

g1smd




msg:4325033
 6:12 pm on Jun 11, 2011 (gmt 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.

joergnw10




msg:4325066
 8:01 pm on Jun 11, 2011 (gmt 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?

lucy24




msg:4325071
 8:09 pm on Jun 11, 2011 (gmt 0)

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" :)

joergnw10




msg:4325078
 8:33 pm on Jun 11, 2011 (gmt 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.

g1smd




msg:4325087
 9:14 pm on Jun 11, 2011 (gmt 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.

joergnw10




msg:4325374
 8:38 am on Jun 13, 2011 (gmt 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!

g1smd




msg:4325419
 11:30 am on Jun 13, 2011 (gmt 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.

joergnw10




msg:4325422
 11:50 am on Jun 13, 2011 (gmt 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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved