Forum Moderators: phranque

Message Too Old, No Replies

Redirecting URLs using scripts or Patterns

         

krish1

7:54 am on Jul 30, 2012 (gmt 0)

10+ Year Member



Hi Everyone

I have a requirement where i have to redirect 1000's of URLs(4000) in Apache HTTP Server with in the same domain(i.e, the site stucture is being changed and hence this requirement)

Can anyone suggest me what is the best way to do it(should we use something like patterns or Scripts ?)


Thanks in Advance

incrediBILL

8:30 am on Jul 30, 2012 (gmt 0)

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



Telling us how your URLs are structured would help.

krish1

8:51 am on Jul 30, 2012 (gmt 0)

10+ Year Member



[XYZ...] needs to be redirected to [XYZ...]

lucy24

9:30 am on Jul 30, 2012 (gmt 0)

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



OK, now try that again, using www.example.com so we can all see what you're talking about :)

Some things can be done directly in mod_rewrite. Other things require rewriting to a php script that Does Things with a database in order to match up Old URL with New URL. Or Old Filepath with New Filepath. Or both, depending on just how much you haven't told us.

krish1

10:05 am on Jul 30, 2012 (gmt 0)

10+ Year Member



www.example.in/a/b/c/d/e needs to be redirected to www.example.in/a/b/1/2/3

krish1

11:08 am on Jul 30, 2012 (gmt 0)

10+ Year Member



Do we use any CGI Scripts for scenarios like this? (I mean when we want to redirect 1000's of URL's)

phranque

11:54 am on Jul 30, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



one possible option for you is Using RewriteMap:
http://httpd.apache.org/docs/current/rewrite/rewritemap.html [httpd.apache.org]

lucy24

3:23 pm on Jul 30, 2012 (gmt 0)

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



Note that many of these examples won't work unchanged in your particular server configuration, so it's important that you understand them, rather than merely cutting and pasting the examples into your configuration.

(Words that could just as well be applied to this Forum as a whole ;))

The RewriteMap directive may not be used in <Directory> sections or .htaccess files. You must declare the map in server or virtualhost context. You may use the map, once created, in your RewriteRule and RewriteCond directives in those scopes. You just can't declare it in those scopes.

Consider yourself warned. Here it sounds as if you are in control of your own server, so a RewriteMap is an option.

You will have to use either a map, or a script of some sort, because you are not simply capturing elements and rearranging them.

In: /a/b/c/d/e
Out: /a/b/1/2/3

mod_rewrite by itself has no way of knowing the values of 1, 2 and 3. And you are definitely not going to write a thousand separate Rules!

g1smd

7:38 pm on Jul 30, 2012 (gmt 0)

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



I've just set up a PHP script for something similar. If the URL relationship is simple the old and new data could be stored in an array. If the site is large, using a database is much more usable.

You should internally rewrite specific requests to the PHP script. The script looks up what the new URL will be and then builds a 301 header pointing to the new URL and then sends it.

krish1

5:22 am on Jul 31, 2012 (gmt 0)

10+ Year Member



Thanks for the reply g1smd

Seems like this is going to help me, but i don't have any idea about using PHP Script for rewriting, I have searched for it on the web but couldn't find anything useful.
It would be very great of you, if you can provide me with some links or example for that

Thanks in Advance

g1smd

6:18 am on Jul 31, 2012 (gmt 0)

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



Detect the requested URL, look up the new URL (either in an array or in a database table) and then use the HEADER directive to send the redirect.

Not including the array, the whole thing is just a few lines of code.

phranque

9:46 am on Jul 31, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Simple PHP redirect script:
http://www.webmasterworld.com/forum88/12025.htm [webmasterworld.com]