Forum Moderators: phranque

Message Too Old, No Replies

Apache URL Rewriting

         

siddharth mazumdar

6:50 am on Feb 8, 2010 (gmt 0)

10+ Year Member



Hi all,

I want to redirect a URL in Apache in the following manner.

http://122.***.203.118

to

http://122.***.203.118?url=www.google.co.in

Your help will be greatly appreciated.

Thanks

Siddharth Mazumdar

[edited by: jdMorgan at 1:20 pm (utc) on Feb 10, 2010]
[edit reason] de-linked [/edit]

g1smd

9:21 am on Feb 8, 2010 (gmt 0)

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



What code have you already tried?

What did it do, and how did that differ from what you wanted?

siddharth mazumdar

10:27 am on Feb 8, 2010 (gmt 0)

10+ Year Member



Hi g1smd,

I haven't written any code as i don't know what to write for the following problem.


What i want is to open a html page at 122.***.203.118, this page will have a link "Click Here", once you click on "Click Here" it will take you to http://***.160.203.118?uri=google.co.in

Thanks
Siddharth

[edited by: jdMorgan at 1:20 pm (utc) on Feb 10, 2010]
[edit reason] de-linked, obscured IP address [/edit]

jdMorgan

2:07 pm on Feb 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Siddharth,

Welcome to WebmasterWorld!

Please see our Apache Forum Charter for info on how to get the most from this forum, and for links to relevant and useful documentation. You may also find the threads in our Apache Library to be helpful. In addition, we have a site search feature that you can use to find previous threads on this subject.

Links to all of these resources are located in the top left area of this page.

Jim

siddharth mazumdar

8:54 am on Feb 10, 2010 (gmt 0)

10+ Year Member



Hi,

This is what i am trying

RewriteEngine On
RewriteCond %{REQUEST_URI} !^google.co.in$
RewriteRule $ http://$ [R=301,L]

it is showing me

http://$/?url=www.google.co.in

i want this to go

http://$/?url=

Thanks
Siddharth

[edited by: jdMorgan at 1:19 pm (utc) on Feb 10, 2010]
[edit reason] de-linked [/edit]

jdMorgan

1:28 pm on Feb 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We need to know the URL in your link and the URL to be redirected-to in order to make much progress here...

Mod_rewrite's function is primarily based on the link requested from your server.

In general, you'll need:
RewriteCond %{QUERY_STRING} !url=google\.co\.in$
RewriteRule ^<linked-page-URL-path>$ http://122.***.203.118/<replacement-page-URL-path>?url=google.co.in$ [R=301,L]

If either the linked-page URL or the replacement-page URL-path is "/" (your home page), that's fine, but you need to state that.

Jim

siddharth mazumdar

5:11 am on Feb 11, 2010 (gmt 0)

10+ Year Member



Hi Jim,

The Link in the page is

http://192.168.100.50/?url=www.google.co.in

i am trying this to capture it & redirect it.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^uri=(.*)$ [NC]
RewriteRule ^/$ $1 [R=301,L]

[edited by: jdMorgan at 2:08 pm (utc) on Feb 11, 2010]
[edit reason] De-linked [/edit]

jdMorgan

2:40 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the query "url=" or "uri="? Letter "elle" or letter "eye"?
The RewriteCond pattern must match exactly.

Also, your code redirects "a" to "b" while your stated goal is to redirect "b" to "a" -- Which do you want?

Assuming that you want to redirect a client requesting "/?url=www.google.co.in" from your server to "http://www.google.co.in/" then the code for use in .htaccess would be:

RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^url=(.+)$ [NC]
RewriteRule ^$ http://%1/? [R=301,L]

On the other hand, if you want to redirect a client request for "http://www.google.co.in" to "/?url=www.google.co.in" on your own site, then that is impossible, since that link will resolve to Google's server, not yours -- Your server will never receive that request, and so cannot redirect it.

Note that in either case, mod_rewrite can have no effect on the link that appears on your page. If you need to change the link on your page, then edit the HTML of that page, or edit the script that generates that page.

Also, I'm not sure where the "$" symbols in the URLs you mentioned above come from, or what you intended them to mean, but you cannot use a literal "$" in a URL -- that character must be URL-encoded as "%24" for use in a URL-path. You may use only alphabetic or numeric characters or "-", ".", "_", or "~" in the URL-path-part of URI. See "RFC3986 - Uniform Resource Identifier (URI): Generic Syntax" details.

Jim

siddharth mazumdar

5:56 am on Feb 12, 2010 (gmt 0)

10+ Year Member



Thanks Jim for the reply.

It is URL not URI,

And no i am not trying to redirect a client request for "http://www.google.co.in" to "/?url=www.google.co.in"
on my site.

As for the $ sign, i tried to send the URL ^url=(.*)
to the variable $ and redirect it.

As for the code that you wrote, it does not seem to work. It is not showing any error, but is not working either,

Your greatly appreciated appreciated.

Thanks
Siddharth

g1smd

7:51 pm on Feb 12, 2010 (gmt 0)

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



We're still at a loss as to what the code needs to DO; both in terms of URL requests and the internal server filepaths they should resolve to.

siddharth mazumdar

4:50 am on Feb 15, 2010 (gmt 0)

10+ Year Member



Hi glsmd,

well we want it to redirect it like this:

[192.168.100.50...] => [192.168.100.50...] => [google.co.in...]

The code that was give to us from you:

RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^url=(.+)$ [NC]
RewriteRule ^$ [%1...] [R=301,L]

The above code is not working,that's all.

Thanks

Siddharth

jdMorgan

2:08 pm on Feb 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You want any "home page' URL requested from this server to redirect through your script to google.co.in?

This will of course make your site disappear from the Web, as most people will try to access your site's home page...

Jim

siddharth mazumdar

5:08 am on Feb 16, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for the reply,

Yes, The link [192.168.100.5...]

is in my "home page" index.html, when you click on that

link it should take you to Google.co.in, But

what happens is that when you click on that link it

shows you [192.168.100.50...]

in the browser, we want to capture the QUERY_STRING

after url= and redirect it to Google.co.in

Your help is greatly appreciated

Thanks

Siddharth

jdMorgan

4:21 pm on Feb 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, well the code you posted above is correct, and should do exactly what you want to do.

RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^url=(.+)$ [NC]
RewriteRule ^$ http://%1/? [R=301,L]

So I have to ask... How do you define "doesn't work"?

What does or does not happen when you click on one of your on-page links?

Have you tried "monitoring" the transaction using the "Live HTTP Headers" add-on for Firefox/Mozilla browsers?

Do you have any other rewriterules that work?

If not, are you getting a 500-Server error with a message in the server error log stating that FollowSymLinks or SymLinksIfOwnerMatch is not set?

If so, then add

Options +FollowSymLinks -MultiViews

ahead of the "RewriteEngine on" directive in the code above.

Sometimes a simple test is useful. You could add a temporary 'test' rule to see if mod_rewrite is working at all:

RewriteRule ^foo\.html$ http://www.google.com/ [R=301,L]

Then request "/foo.html" from your server, and you should land at Google.

Be sure to completely flush (delete) your browser cache before testing any new server-side code.

I should also note that all code discussed so far is written to work in a .htaccess context or inside a <Directory> container in a server config file. If the code is located elsewhere, the RewriteRule pattern will need to have a leading slash -- as in "RewriteRule ^/$"

Jim

siddharth mazumdar

5:06 am on Feb 18, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for the reply,

We were using the code in the configuration file itself and didn't use the leading / in the rewrite rule.
yes it is bringing the desired result now.

Thank you again for your prompt reply, your help is greatly appreciated.

Thanks
Siddharth