Forum Moderators: phranque

Message Too Old, No Replies

Dynamic URL Redirect with Query String to Dynamic Static

Dynamic URL Redirect with Query String to Dynamic Static

         

uhjustcallmepete

3:38 am on Dec 10, 2010 (gmt 0)

10+ Year Member



Very basic question. My url is:

http://www.example.com/index.php?test=1

I have set it to static with:

Options +FollowSymLinks
RewriteRule test/(.*) ?test=$1

Hence, the following urls work:
http://www.example.com/test/1
http://www.example.com/test/2
http://www.example.com/test/3
etc...

However, I'm trying to redirect any calls from

http://www.example.com/index.php?test=1
http://www.example.com/index.php?test=2
http://www.example.com/index.php?test=3
etc...

to

http://www.example.com/test/1
http://www.example.com/test/2
http://www.example.com/test/3
etc...

I've figured out how to do this with a single url:

RewriteCond %{QUERY_STRING} ^test=1$
RewriteRule ^index\.php$ http://www.example.com/test/1? [R=301]

However, I'm looking for a dynamic solution. In short, a dynamic PHP query based url to a dynamic static url... hope that made sense!

Thanks in advance!

uhjustcallmepete

5:33 am on Dec 10, 2010 (gmt 0)

10+ Year Member



Unfortunately, I discovered a deeper issue... my htaccess was set to:

1. redirect http:// to [www....]
2. remove all .php extensions
3. redirect all .php extensions to extensions removed
4. set static url to example/test/(number)

However, I was still left with two issues
1. http://www.example.com/index (without redirecting)
2. http://www.example.com/index?test=# (without redirecting)

After trying with much difficulty to solve this with htaccess, I gave up and whip up a quick fix with PHP 301's. Not the most efficient, but I don't imagine many people will be hitting the site using the two issues above... especially in a few weeks from now (as the site is very new).

htaccess, you have defeated me! However, my redemption will come in the form of having a beer.

*Curious: if anyone can solve the problems above, I'd be interested in what kind of solutions are available (due to the inner dork in me).

Cheers folks!

g1smd

8:11 am on Dec 10, 2010 (gmt 0)

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



This redirect/rewrite pair of rules is one of the most popular questions here, and as such there are literally a thousand prior threads with example code. I know the solution has been posted 4 or 5 times this month alone, the last time only days ago. Check recent threads for details.