Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect

         

painstik

9:21 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



Hi guys and girls, I am very noobish with htaccess, and I am desperatly seeking for help.

I want to change some things in my urls, but I don't know how to redirect that pages, so I don't lose all my search engine indexed pages.

The issue is next:

My urls looks like this now:

http://www.example.com/categories/Ra%C4%8Dunala/Prijenosna-ra%C4%8Dunala/

And i want to change them so they look like this:

http://www.example.com/categories/Racunala/Prijenosna-racunala/

%C4%8D is a special Croatian char which is not able to show in browsers

The thing is that hex code %C4%8D may show once, or maybe 5-6 times, and all of them should be changed so it redirects to proper new url.

Thanks for help

g1smd

12:48 am on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



We have covered this topic multiple times already this month.

Please check recent threads and the forum charter for links to example code and then post your best effort here.

painstik

1:02 am on Nov 20, 2009 (gmt 0)

10+ Year Member



ok, thx

painstik

10:54 am on Nov 20, 2009 (gmt 0)

10+ Year Member



Hi, I have searched as you said to me and i cannot find anything similar in the last 2 month. Can someone give me a tip or a link please?

Thank you.

jdMorgan

2:58 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unfortunately, this is a complicated problem...

First, if you want to change a URL, there's only one place you can do that: You have to change the links in the HTML on your pages. No incoming-request-processing trick on the server can 'change' a URL.

So you must edit your HTML pages or modify the script or database which produces the URLs used in links on your HTML pages.

Having done that, you then need to "re-connect" the new URLs to the same filepath+query string path inside the server that the old URL used to resolve to. This can be done on a URL-by-URL basis if the number of URLs is small, using one RewriteRule per new URL. Example:


RewriteRule ^categories/Racunala/Prijenosna-racunala/$ categories/Ra\%C4\%8Dunala/Prijenosna-ra\%C4\%8Dunala/ [NE,L]

However, if the number of new URLs is large, this method may require too many rules. In that case, the simplest method may be to rewrite *all* URLs that look like new URLs to your script, and modify the script to look up the "correct spelling" of those new URLs (convert the un-accented characters to accented characters, so that the correct content is served.

Then there's a third step: If a client (browser or search engine robot) directly requests an old (accented) URL, then you should redirect that request to the new (un-accented) URL. This will 'recover' the traffic and ranking factors of the old URLs, and pass them to the new, as well as speeding up the process of getting your old URLs out of search engine listings. Example:


RewriteCond %{THE_REQUEST} ^[A-Z]+\ /categories/Ra\%C4\%8Dunala/Prijenosna-ra\%C4\%8Dunala/\ HTTP/
RewriteRule ^categories/Ra.+Dunala/Prijenosna-ra.+Dunala/$ http://www.example.com/categories/Racunala/Prijenosna-racunala/$ [R=301,L]

Note that I replaced the encoded-character sequence subpatterns in the RewriteRule (only) with generic ".+" subpatterns; This may be necessary, but I'm not sure. You *may* able to leave the un-escaped accented characters in that RewriteRule pattern, but I don't know because I haven't done any work with accented character sets.

If these old URLs don't have a lot of inbound links from other sites, this last step may not be needed; It just speeds up the search engines' updating of your old URLs to your new.

As you've likely noticed, I didn't mention using mod_rewrite to do any of the URL-conversion work; Since mod_rewrite is a specialized Apache directive-processing module and not a true 'scripting language,' it is not well-suited to complex jobs like character-set conversion/translation. In fact it's horribly inefficient at doing this kind of job. There is also the problem that if you remove this non-printing character from all URLs, mod_rewrite will have no way to "know" where it should re-insert those characters to form a correct server filepath.

Therefore, the best approach is to pass these URL-requests to a script that has more-powerful functions and that can access your database if necessary to convert/translate the URLs.

Jim

painstik

3:53 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



Thank you for your help.

The thing is next. I can easily rewrite those URLs to look exactly how I want to in PHP. But, those new URLs are not indexed in google, and I want to redirect them.

There is over 1k URLs which should be checked, with various encoded chars, that what i showed is just an example, there could be link like this:

http://www.example.com/products/Prijenosno-ra%C4%8Dunalo-%C4%8Detiri

which will look like this if i make str_replace in PHP:

http://www.example.com/products/Prijenosno-racunalo-cetiri

and I need to redirect user from upper link to the down link.

I am total newb with apache, so the only way I can explain is in logic.

IF in link is %C4%8D, REPLACE %C4%8D with c, REDIRECT to a new link.

jdMorgan

4:48 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please notice the "third step" described above...

This is a complex project, and needs attention to detail. I tried to be very exact and thorough in my description above -- much more so that in an "informal conversation," and you may read it to mean exactly what it says.

Jim