Welcome to WebmasterWorld Guest from 54.225.32.164

Forum Moderators: Ocean10000 & phranque

What is the correct mod_rewrite rule for redirecting a parameter url?

     
11:02 pm on Jun 20, 2018 (gmt 0)

New User

joined:Nov 28, 2016
posts: 10
votes: 0


Hi
I have a paratmer url from an old doman i want redirect using mod rewrite to my new domain
http://www.example.com/topsites/in.php?id=name
and need it to go to
https://example.net/product/ 

RewriteEngine on
RewriteRule http://www.example.com/topsites/in.php?id=name https://example.net/product/ [L,R=301]


will this do it? thanks
arturo

[edited by: phranque at 11:14 pm (utc) on Jun 20, 2018]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

11:33 pm on June 20, 2018 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11388
votes: 156


welcome to WebmasterWorld [webmasterworld.com], Anthonyandrews!

are you redirecting a single url from old to new or is there more involved?

RewriteRule http://www.example.com/topsites/in.php?id=name


the Pattern of a RewriteRule directive "is matched against only a partial path".
source: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

therefore it won't match the hostname or the query string specified in the Pattern.
you can use RewriteCond directives prior to the RewriteRule to match these if necessary.
3:50 am on June 21, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14898
votes: 648


will this do it?
Sorry, no, for two separate but related reasons.

The body of a RewriteRule sees only the URLpath. Not the parameters, not the protocol, not the hostname. Therefore

#1 If your pattern includes “http://example.com/” then mod_rewrite will never recognize the URL, hence conditions (if any) won't be evaluated, and the rule will never execute.
#2 Parameters have to go in a RewriteCond, because the body of the rule doesn't see them.

Is it just the one redirect? I was wondering how mod_rewrite knows that /topsites/in.php?id=name should turn into https://example.net/product/ -- but if that's the only URL being redirected, then the answer is obvious: it knows because you're telling it so.

Are there other possible parameters, attached to the same /topsites/in.php path, that are not to be redirected?

Note incidentally that it is perfectly OK to say “http://example.in” when posting here, if that is easiest for you. It just has to be “example”.
10:06 am on June 21, 2018 (gmt 0)

New User

joined:Nov 28, 2016
posts: 10
votes: 0


Yes this is just 1 redirect.
There are no other possible parameters that are not to be redirected.

so will this do it?
RewriteRule /topsites/in.php?id=name https://example.net/product/ [L,R=301]
11:22 am on June 21, 2018 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11388
votes: 156


so will this do it?
RewriteRule /topsites/in.php?id=name https://example.net/product/ [L,R=301]

the Pattern of a RewriteRule directive "is matched against only a partial path".
source: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

therefore it won't match the hostname or the query string specified in the Pattern.
you can use RewriteCond directives prior to the RewriteRule to match these if necessary.

#2 Parameters have to go in a RewriteCond, because the body of the rule doesn't see them.


see: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
3:44 pm on June 21, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14898
votes: 648


so will this do it?
No, for at least one and possibly two reasons.

Unless the rule is lying loose in the config file (most likely in a vhost section), patterns must not start with a / or they will not be recognized.

The other reason has already been covered.

If there are no not-to-be-redirected parameters, why not simply redirect the bare URL, independent of parameters, and discard the parameter? In fact, as the rule is currently written, the redirect would reappend the parameter. I suspect that is not what you want.
4:17 pm on June 21, 2018 (gmt 0)

New User

joined:Nov 28, 2016
posts: 10
votes: 0


Well i have 4 such urls in ahrefs and each one has some google power in it
Google has indexed all 4 with parameters..
I want to pass all the link power in them to the new domain
So a redirect on the base url without parameters would pass that link power to the new url ?
9:31 pm on June 21, 2018 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11388
votes: 156


Yes this is just 1 redirect.
There are no other possible parameters that are not to be redirected.

Well i have 4 such urls in ahrefs and each one has some google power in it
Google has indexed all 4 with parameters..

could you please clarify or rectify these two statements?
9:47 pm on June 21, 2018 (gmt 0)

New User

joined:Nov 28, 2016
posts: 10
votes: 0


The url exists with 4 different parameters
RewriteRule http://www.example.com/topsites/in.php?id=name1 https://example.net/product/ [L,R=301]
RewriteRule http://www.example.com/topsites/in.php?id=name2 https://example.net/product/ [L,R=301]
RewriteRule http://www.example.com/topsites/in.php?id=name3 https://example.net/product/ [L,R=301]
RewriteRule http://www.example.com/topsites/in.php?id=name4 https://example.net/product/ [L,R=301]

Lucy is suggesting redirecting the bare url without the parameters.
I am asking if this would pick them all up and redirect each one to the target
RewriteRule http://www.example.com/topsites/in.php https://example.net/product/ [L,R=301]
10:29 pm on June 21, 2018 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:3834
votes: 212


So you want requests for "/topsites/in.php?id=name(any single digit number)" to go to "https://example.net/product/"?
You will be sending visitors from all 4 pages to one different directory? Just trying to understand, because it does not sound like you will like the result. Has all the content at those old URLs moved to that new one?
11:53 pm on June 21, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14898
votes: 648


All yours, phranque. I'm tired.
12:11 am on June 22, 2018 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11388
votes: 156


RewriteRule http://www.example.com/topsites/in.php https://example.net/product/ [L,R=301]

i'm not going to repost my first response in this thread and apparently lucy24 won't be reposting hers but you should start there.
look for "hostname" while rereading those.
7:31 am on June 22, 2018 (gmt 0)

New User

joined:Nov 28, 2016
posts: 10
votes: 0


So you want requests for "/topsites/in.php?id=name(any single digit number)" to go to "https://example.net/product/"?
You will be sending visitors from all 4 pages to one different directory


Yes to all these points.

Thanks to all for the explanations about how these rules work.
2:37 pm on June 22, 2018 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:3834
votes: 212


<Topic-drift>
If the new URL is not replacing those older pages, it might not be any help even if Google likes those old pages. Google doesn't rank the URL but what is found on that URL and delivering something different is more likely to hurt your new site than to add any value to it. </t-d>

RewriteRule http://www.example.com/topsites/in.php?id=name1 https://example.net/product/ [L,R=301]
is not a valid rule because:
1. The rule has no RewriteCond
2. Your rule uses a full URL and
3. if you are getting advice that you don't understand, it would help a lot if you were to ask questions about any answer that you don't understand.

Read these posts again and ask questions if you need to.
Especially these parts, they tell you how to do it correctly:
#1 If your pattern includes “http://example.com/” then mod_rewrite will never recognize the URL, hence conditions (if any) won't be evaluated, and the rule will never execute.
#2 Parameters have to go in a RewriteCond, because the body of the rule doesn't see them.

the Pattern of a RewriteRule directive "is matched against only a partial path".
source: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

therefore it won't match the hostname or the query string specified in the Pattern.
you can use RewriteCond directives prior to the RewriteRule to match these if necessary.

To see all the why and how, visit source: https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
(copy that URL and paste it into your browser's address bar to visit)

6:13 pm on June 22, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:14898
votes: 648


1. The rule has no RewriteCond
I hope you weren’t saying that RewriteRules have to have Conditions. They don’t. In particular, if you have old redirects that you shifted over from mod_alias, they will come through without conditions because none are needed.