Forum Moderators: phranque

Message Too Old, No Replies

301 redirect for dynamic pages with variables

         

anakein

4:18 pm on Aug 20, 2011 (gmt 0)

10+ Year Member



Hi,
I need help to permanent redirect all forum dynamic URL's to static html.

Example:

http://www.example.com/forum/index.php?topic=45845.msg446396;boardseen#new

to

http://www.example.com/forums/topic45845.html

the bold number change for topic.

I tried all rules and redirect examples into this forum(great), but nothing work for me.

please help me.


thanks

g1smd

6:14 pm on Aug 20, 2011 (gmt 0)

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



I've answered a very similar question in the last 24 hours. Check recent threads. This type of question is asked several times per week, and as such there's several thousand prior examples in this forum.

Let's see your code so we can see what you actually tried.

anakein

7:19 pm on Aug 20, 2011 (gmt 0)

10+ Year Member



I searched in your messages and others but i donīt to see same problem.

i tried:

RedirectMatch 301 ^/portal/modules/smf/index.php([^=]+)(.*)$ http://example.com/forumc/topic$2.html Dont work

Redirect 301 ^/portal/modules/smf/index.php?topic=(.*)$ http://example.com/forumc/topic$1.html donīt work by question mark


Redirect 301 ^/portal/modules/smf/index.php(.*)$ http://example.com/forumc/topic$1.html work but i want only the number topic.

only need extract topic number.


thanks again.

g1smd

8:20 pm on Aug 20, 2011 (gmt 0)

anakein

8:39 pm on Aug 20, 2011 (gmt 0)

10+ Year Member



thank but is not same problem. I have 500000+ topic number, and this example is for static url with same parameter always, "language=ro"
I dont know the parameters before.

g1smd

9:00 pm on Aug 20, 2011 (gmt 0)

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



Sure, it's not exactly the same problem, it's never going to be "exactly" the same problem, but it's a framework you can start with.

In place of "ro" you'll create a backreference and then re-use it so it works for any value, not just one static value.

anakein

9:18 pm on Aug 20, 2011 (gmt 0)

10+ Year Member



sure, but I probe all and dont work.

RewriteRule ^/forumold/portal/modules/smf/index.php([^=]+)([0-9]+)\. http://example.com/forumc/topic$2.html [L] (copy other post by you).

and I'm desperate.
Always read this forum and what I read could solve problems, not now.

g1smd

9:43 pm on Aug 20, 2011 (gmt 0)

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



You'll see the need for a RewriteCond looking at QUERY_STRING, because RewriteRule sees only the path part of the URL request.

A redirect also requires the R=301 flag.

lucy24

10:17 pm on Aug 20, 2011 (gmt 0)

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



Your magic words are

RewriteCond %{QUERY_STRING} blahblah

Read the QueryString boilerplate in the linked thread. Not all of it applies to any one Rewrite, but it's where you need to be looking.

anakein

2:38 pm on Aug 21, 2011 (gmt 0)

10+ Year Member



g1smd, please, if you don want help me, not posting more please. I dont like playing with me.

I have no time for riddles, I have a serious problem. thanks

g1smd

4:46 pm on Aug 21, 2011 (gmt 0)

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



You might have a serious problem, but the type of question you are asking has been asked literally several thousand times before in this forum.

There are not enough volunteers in this forum to provide a "free code writing service" so this forum is all about education. Simply, you put in the hard work to read previous threads and the Apache and RegEx manuals, post some actual code you're having a problem with and then ask specific questions about your code and you'll find lots of help here.

As for the last code posted, you'll see the need for a preceding RewriteCond looking at QUERY_STRING, because RewriteRule sees only the path part of the URL request.

In place of "ro" you'll create a backreference and then re-use it so it works for any value, not just one static value.

A redirect also requires the R=301 flag, not just the [L] flag.

lucy24

7:37 pm on Aug 21, 2011 (gmt 0)

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



Today's Analogy:*

You probably fit into one or more of the following groups:

#1 You had children but they are now grown up.
#2 You currently have children.
#3 You don't have children yet, but you will.
#4 You used to be a child.
#5 You are currently a child.

(Best guess: #4.) As such, you know that a parent can clean a child's room in far less time than it takes to teach the child how to do it. Especially when you include standing over the child to ensure that it gets done correctly, and looking under the bed, and checking the drawers and so on. But eventually the child turns into a bigger child who knows how to clean up after himself. And still later, the child becomes a grownup who knows how to prevent your grandchildren from destroying your house. That's when the time investment pays off.

I could write your required code in five minutes. Add another five minutes for g1smd to come around and point out the detail I overlooked and fix it. It will take you more than ten minutes. But next time you will know how.


* Actually last night's analogy, but it was 3AM so I went to bed instead.

anakein

11:11 pm on Aug 21, 2011 (gmt 0)

10+ Year Member



I am a linux coder with 40 years and 5 years ago we were twice coder. Now I understand why people no longer coder. Some work others just want to flatter him in a forum.

lucy24 you should know before you speak, so there would be a stupid.

lucy24

12:21 am on Aug 22, 2011 (gmt 0)

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



So that's nos. 1 and 4, then. The analogy still holds.

anakein

8:39 am on Aug 23, 2011 (gmt 0)

10+ Year Member



for every body without help.
This problem is solved

RewriteEngine On
RewriteCond %{QUERY_STRING} ^topic=([0-9]*)
RewriteRule ^p/modules/smf/(.*) [domaintarget.com...] [R=301,L]

[domainsource.com...]
to
[domaintarget.com...]

is you want complete string then add ? over end of line
RewriteRule ^p/modules/smf/(.*) [domaintarget.com...] [R=301,L]
result:
[domaintarget.com...] [R=301,L]

if you want to ditch anything after the topic=1234 then just remove the $ that indicates the end of the string otherwise add it.


knowledge belongs to the world, if you do not believe that do not use linux


thanks

g1smd

6:49 pm on Aug 23, 2011 (gmt 0)

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



^topic=([0-9]*)
allows
topic=<blank>
and is probably not what you wanted.

The redirect works only if 'topic' is the first parameter, you should redirect for any position.

Since you don't re-use the $1 backreference, the trailing
(.*)
pattern is redundant.

Add a question mark to the end of the target URL to CLEAR the query string.

anakein

11:31 pm on Aug 23, 2011 (gmt 0)

10+ Year Member




^topic=([0-9]*) allows topic=<blank> and is probably not what you wanted.


Perfect, I want redirect always.

The redirect works only if 'topic' is the first parameter, you should redirect for any position
. sure? today redirect always


Since you don't re-use the $1 backreference, the trailing (.*) pattern is redundant.
ok, thanks i need to think more.

Add a question mark to the end of the target URL to CLEAR the query string.
When add ? to the end line, add "topic=434543" to the url and not only the number.

lucy24

3:09 am on Aug 24, 2011 (gmt 0)

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



Just to keep everyone else from going bonkers:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^topic=([0-9]*)
RewriteRule ^p/modules/smf/(.*) http://www.example.com/forums/topic%1.html [R=301,L]

http://www.example.com/p/modules/smf/index.php?topic=434534
to
http://www.example.com/forums/topic434534.html

Here you must have omitted a ? from your example, because the rule as printed would redirect

http://www.example.com/p/modules/smf/index.php?topic=434534
to
http://www.example.com/forums/topic434534.html?topic=434534

The rule does not end in a ? and so by Rewrite default the previous query string is returned unchanged.

If you want complete string then add ? over end of line

RewriteCond %{QUERY_STRING} ^topic=([0-9]*)
RewriteRule ^p/modules/smf/(.*) http://www.example.com/forums/%1? [R=301,L]
result:
http://www.example.com/forums/topic=233434 [R=301,L]

Your example is right but your text is misplaced. Putting a ? at the end of the target eliminates the previous query string.

When add ? to the end line, add "topic=434543" to the url and not only the number.

That can't be right. Better test it again, because it's the exact opposite of what is supposed to happen.