Forum Moderators: phranque

Message Too Old, No Replies

i need to make loop for this htaccess rediect

htacess redirect

         

Mohamad

2:33 pm on Sep 26, 2008 (gmt 0)

10+ Year Member



Hello

i have been changes my site URLs

and a manually make the redirect to old links in google to access a new links

such like this

########### Custom Fourms Redirect ############################
Redirect /f1.html /forumdisplay.php?f=1
Redirect /f2.html /forumdisplay.php?f=2
Redirect /f3.html /forumdisplay.php?f=3
Redirect /f4.html /forumdisplay.php?f=4
Redirect /f5.html /forumdisplay.php?f=5
Redirect /f6.html /forumdisplay.php?f=6
Redirect /f7.html /forumdisplay.php?f=7
Redirect /f8.html /forumdisplay.php?f=8
Redirect /f9.html /forumdisplay.php?f=9
##########################################

etc ..

i need to make this redirect for unlimited links as a loop

would you tell me the write method to do that?

Ragards

jdMorgan

3:00 pm on Sep 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is no efficient "looping" available in .htaccess. It contains server config directives, and not "scripting code" per se.

However, the concept you are looking for is "back-references," available in mod_alias and mod_rewrite. You use a regular-expressions pattern (see our Charter for link to a tutorial) to match and capture a value, and then back-reference that value in the substitution URL.

Unless you want your pages listed in search engine results by the "/forumdisplay.php?f=1"-style URLs, you are probably wanting to use an internal rewrite instead of an external redirect. An external sends a response to the client (browser or robot) telling it to ask for the requested resource again, using the new URL provided in that redirect response. The browser changes its address bar and issues a second request using the URL provided in the server's initial redirect response. Search engines will interpret that redirect to say, "Discard the old URL and use this new one." Therefore, the result of your code above will be to replace the 'search-engine-friendly' URLs with the dynamic URLs in the search results.

An internal rewrite, in contrast, simply says to the server, "If you get a request for this URL, serve the contents from that filepath." Therefore, there is only one HTTP request/response transaction, the browser's address bar does not change, and search engines will keep the original 'friendly' URL.

Here's an example of an internal rewrite using a back-reference:


Options +FollowSymLinks
RewriteEngine on
#
RewriteRule ^f([0-9]+)\.html$ /forumdisplay\.ph?f=$1 [L]

This internally rewrites all requests for "f<NN>.html" to /forumdisplay\.ph?f=<NN>, where <NN> is the number (one or more digits) in the originally-requested URL.

See the links in our Forum Charter (see links at the top of this page) for more information and links to tutorials. There are also several useful threads in our Forum Library.

Jim