Forum Moderators: phranque

Message Too Old, No Replies

Redirection Dynamic URLs to Static via htaccess

         

asson

8:14 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



Hi,
I have a Dynamic URL that looks like this,

/file.php?id=$1
and i have created an ReWrite rule that converts it to

^file/([^/]*)\.html$

How can i set it up so all Dynamic URLs get redirected to the new Static URLs?

Any help will be apreciated.
Thanks.

jdMorgan

9:44 pm on Jun 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Make sure your problem isn't the same as is being discussed here: [webmasterworld.com...]
The problem in that thread sounds very similar. If after reading it, your question changes, please post back here.

Thanks,
Jim

asson

9:58 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



Thanks Jim,
No it was not the same question.
Let me ask in another way,
I use this code

Redirect 301 /vb http://example.com/forums

and ofcourse the effect will be if anyone typs www.domain.com/vb will automaticly be redirected to www.domain.com/forums.

I want to do the same while my links are dynamic,


Redirect 301 /file.php?id=$1 ^file/([^/]*)\.html$

But it doesn't work. is 301 the right option?

Thanks.

[edited by: jdMorgan at 11:38 pm (utc) on June 26, 2007]
[edit reason] example.com [/edit]

jdMorgan

10:12 pm on Jun 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just wanted to make sure, since this is the exact opposite of what is needed to create and use search-engine friendly URLs. If combined with the proper code to make search-engine friendly URLs, it will create an 'infinite' rewrite/redirect loop. In other words, I want to make sure that's not what you're trying to do because if so, then your requested function is backwards. The tutorial cited in the other thread describes that process.

Since this is not a search-engine-friendly URL problem, then assuming that you have other working RewriteRules, the following will redirect dynamic URLs to their static equivalents:


RewriteCond %{QUERY_STRING} ^id=([^&]+)
RewriteRule ^file\.php$ http://www.example.com/file/$1.html [R=301,L]

If you have no other working rules, you'll need to enable mod_rewrite before using it. This requires either the second of these two directives, or both, and there's usually no way to tell except by testing:

Options +FollowSymLinks
RewriteEngine on

Jim

asson

10:18 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



I tried it, the output is this,
file/.html?id=57

and it should be,

file/57.html

what should I modify?

Thanks

jdMorgan

10:21 pm on Jun 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, typing too fast:

RewriteCond %{QUERY_STRING} ^id=([^&]+)
RewriteRule ^file\.php$ http://www.example.com/file/[b]%1[/b].htm[b]l?[/b] [R=301,L]

Jim

asson

10:31 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



Thank you, you are getting me closer,
now my code looks like this,


RewriteCond %{QUERY_STRING} ^id=([^&]+)
RewriteRule ^file\.php$ http://www.example.org/file/%1.html? [R=301,L]
RewriteRule ^file/([^/]*)\.html$ /file.php?id=$1 [L]

The URL looks as it should in the address bar,
but I get this error in the browser,

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

If I remove the last line of the code, I'll get error 404.

Thanks.

[edited by: jdMorgan at 11:37 pm (utc) on June 26, 2007]
[edit reason] example.com, example.org [/edit]

jdMorgan

11:43 pm on Jun 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, well the code is doing exactly what you told it to do -- Redirect index.php?id=xyz to file/%1.html, and then rewrite file/xyz.html back to index.php?id=xyz ... which will match the first rule and so redirect, and then get rewritten by the second rule ad infinitum, thus creating a nice loop.

The link to the solution was given in the thread cited above. But here it is again: Changing Dynamic URLs to Static URLs [webmasterworld.com]

Jim

asson

11:57 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



Great, hehe.
Thank you very much. i'll go on...
Have a nice time.