Forum Moderators: phranque

Message Too Old, No Replies

.htaccess rewrite for URL with #

         

Darth_Pooh

4:39 am on Apr 19, 2007 (gmt 0)

10+ Year Member



We use .htaccess to redirect URLs based on IP address, and for the most part it works correctly. However if the URL contains a #, then the # and everything after it are treated like a comment, and not included in the Redirected URL, as it should be.
for example:
 
http://www.domainname.com/filename_041807.pdf#page=22

gets redirected to

http://www.redirect1.com/filename_041807.pdf

when it should get redirected to

http://www.redirect1.com/filename_041807.pdf#page=22

the htaccess file is as follows:


RewriteEngine on

#AZGB01
RewriteCond %{remote_addr} ^10\.0\.246\.[0-9]*
RewriteRule ^(.+)$ http://azgb01-nms.azgb01.gd-ais.com/$1 [R,L]
RewriteCond %{remote_addr} ^10\.0\.248\.[0-9]*
RewriteRule ^(.+)$ http://www.redirect1.com/$1 [R,L]
RewriteCond %{remote_addr} ^10\.2\.[0-9]*\.[0-9]*
RewriteRule ^(.+)$ http://www.redirect2.com/$1 [R,L]
RewriteCond %{remote_addr} [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*
RewriteRule ^(.+)$ http://redirect3.com/$1 [R,L]

What needs to be changed, for it to work with the # in the URL?

[edited by: Darth_Pooh at 4:50 am (utc) on April 19, 2007]

phranque

9:42 am on Apr 19, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would try to find the environment variable that contains the anchor part of the url (perhaps REQUEST_URI?) and use it with a regexp in a rewriteCond to extract the anchor part and backreference (%1) the anchor part of the url in the rewriteRule.

Darth_Pooh

2:53 pm on Apr 19, 2007 (gmt 0)

10+ Year Member



Unfortunately I don't have a degree in computer science, so what you just said is way over my head. I didn't write the .htaccess I just copied it and modified the hostnames. I don't how to look for environmental variables or how to write a regexp. I need something than I can copy verbatim.

jd01

4:28 pm on Apr 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is no way to "see" the # in .htaccess. It is an anchor for browsers, not part of the URL, so you will not be able to find/use it in a comparison.

Justin

Darth_Pooh

5:29 pm on Apr 19, 2007 (gmt 0)

10+ Year Member



I don't need to use it in the comparison, the comparison is on the IP address. I just need you include it in the rewritten URL

jd01

5:33 pm on Apr 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My point was the # and anything following the # are not available to .htaccess.
It is not, and should not be included in a redirect.

The response you are getting from your server is correct.
The # is not part of the URL.

Justin

Achernar

5:47 pm on Apr 19, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



What jd01 is saying is that if your browser sees www.example.com/page.html#anchor , it will request www.example.com/page.html from the server (without the "#..."). "#anchor" is used internally by the browser to focus the display on a specific part of the page.

www.example.com/page.html#anchor
www.example.com/page.html#otheranchor
www.example.com/page.html

is exactly the same page from the point of view of the browser. And _it is_ the same page/document/resource/whatever.

[edited by: Achernar at 5:47 pm (utc) on April 19, 2007]

Darth_Pooh

6:28 pm on Apr 19, 2007 (gmt 0)

10+ Year Member



Yes i realise"#anchor" is used internally by the browser to focus the display on a specific part of the page, But that isn't the result we are getting.

without the rewrite
[domainname.com...]
will load the PDF and display it at page 22. However,
with the rewrite the PDF is displayed at the beginning, and the Anchor is totally ignored.

So is there a way that the URL can be rewritten, so that the Browser will have the #Anchor, to go to the right place in the file?

jd01

6:56 pm on Apr 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No.

Achernar

7:35 pm on Apr 19, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



without the rewrite
[domainname.com...]
will load the PDF and display it at page 22.

Your server will never get the "#page=22" part of the url, because no browser in the world will send this as part of the request. The browser keeps it private.