Forum Moderators: phranque

Message Too Old, No Replies

Weird Redirect

         

Mystique

4:32 am on Nov 5, 2008 (gmt 0)

10+ Year Member



Hello guys,

I have a problem with my .htaccess redirect

How to redirect my directory including any header request to new domain excluding header request..
Example:
http://www.example.com/test/?header=test
to
http://www.newexample.com

This my code for now:


redirect 301 /test/ http://www.newexample.com

but whenever i include the header request, the header request also be passed to new domain..
Example:
http://www.example.com/test/?id=test
to
http://www.newexample.com/?id=test

How to exclude all header request being passed to new domain?

BTW, I'm a New Member here.. Hope I Can Make A Good Input To These Forums And Learn Alot. :)

Thanks..

[edited by: jdMorgan at 5:59 am (utc) on Nov. 5, 2008]
[edit reason] examplified, de-linked. [/edit]

jdMorgan

6:02 am on Nov 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Mystique, and welcome to WebmasterWorld!

The "id=test" part is called a query string.

It is not considered to be part of the URL, but rather, is data attached to the URL to be passed to the resource (such as a script) at that URL.

Therefore, it is handled separately, or in the case of older Apache modules such as mod_alias, it may not be handled at all.

In order to accomplish your redirect as specified, you will need to use Apache mod_rewrite:


Options +FollowSymLinks
RewriteEngine on
#
RewriteRule ^test/$ http://www.newexample.com/? [R=301,L]

The "?" on the substitution URL in the code above is a special operator, which tells mod_rewrite to clear the query string. It will not appear in the URL requested from newexample.com

mod_rewrite uses regular-expressions pattern-matching. The "^" and "$" characters are called "anchors" and mark the start and end of the pattern. If both are used, then an exact match is required. For more information, see the regular-expressions tutorial cited in our Apache Forum Charter [webmasterworld.com].

Note that RewriteRule cannot "see" the URL-path to the directory where it is running. In the case of mod_rewrite code located at www.example.com/.htaccess, this means that RewriteRule will not see the leading slash on the URL-path. You will notice that the pattern in the RewriteRule above is therefore "^test/$" and not "^/test/$".

Jim

Mystique

7:46 am on Nov 5, 2008 (gmt 0)

10+ Year Member



Thanks Jim For thats in depth explaination.. :)

finally i finished this script,. can u please check is my .htaccess correct?


RewriteEngine on
Rewritebase /
#
RewriteCond %{HTTP_REFERER} homepage\.net [NC,OR]
RewriteCond %{HTTP_USER_AGENT} im-allowed [NC,OR]
RewriteCond %{REMOTE_ADDR} ^111\.111\.111\.111 [NC]
RewriteRule .* http//www.SecretSite.com/? [R=301,L]
#
redirect 301 /secret http//www.searchtest.com

i will put this htaccess in "secret" folder
& i want it to allow only this:
- ip (111.111.111.111) or
- visitor refered by (hompepage.net) or
- (Im-Allowed) user agent
to be redirected to the correct site..else go to wrong site.
The query string will only be sent to searchtest.com & not to secretsite.com

$correctsite: http//www.secretsite.com
$Wrongsite: http//www.searchtest.com

[edited by: Mystique at 7:48 am (utc) on Nov. 5, 2008]

jdMorgan

3:46 pm on Nov 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the purpose of the "redirect" directive in the last line?

I suspect that it should *not* be a redirect, but rather, an internal rewrite.

Also, do not mix mod_alias and mod_rewrite directives. If you do so, you cannot control the order of execution. Code in config files is executed in per-module order, not in sequential line order.

That is, each Apache module "scans" the config files for directives that it understands, and executes only those directives. Therefore, directives handled by one module will executed in order by that module, but the order in which each module executes is determined by the server configuration.

This can cause very bad problems if you test on one machine and deploy to a different on-line server after testing. If the two machines are not configured identically, then the code may run correctly during test, but fail when deployed to the Web server.

Another problem is that if your site is hosted by a hosting company, they could make a minor change to your server configuration, changing the module execution order, and thereby breaking your code.

So if you use mod_rewrite at all, do not use the Redirect and RedirectMatch directives in mod_alias; Instead, use mod_rewrite for those functions as well.

Also, be aware that the HTTP_REFERER may be blank, even for valid requests; Your code should handle that possibility as you think best.

Jim

Mystique

4:02 pm on Nov 5, 2008 (gmt 0)

10+ Year Member



Hi Jim,

Thnaks for your reply.

btw, can RewritRule be included 2 time?..
my /secret folder is only for a filter, i dont want visitor can get in to this folder..
so if its in ip list it will be redirected to real site, else it will be redirected to fakesite

What do you suggest to change this code so i accomplish my purpose?..

i'm just start learning .htaccess programming..

jdMorgan

4:31 pm on Nov 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Most of my .htaccess files have hundreds of RewriteRule directives in them. Some have thousands. A RewriteRule is only executed if the URL-path pattern matches and the RewriteConds evaluate as "True". So many RewriteRules can be used, as long as the URL-path patterns and RewriteConds are sufficiently-specific to guarantee proper and expected RewriteRule execution behavior.

Replace the Redirect directive with:


RewriteRule ^/secret - [F]

This will generate a 403-Forbidden response if /secret is requested but has not been redirected by the previous rule.

Your RewriteConds need a bit of tweaking:


RewriteCond %{HTTP_REFERER} [b]^(https?://(www\.)?h[/b]omepage\.ne[b]t.*)?$[/b] [NC,OR]
RewriteCond %{HTTP_USER_AGENT} im-allowed [NC,OR]
RewriteCond %{REMOTE_ADDR} ^111\.111\.111\.11[b]1$[/b] [NC]

The first RewriteCond has been modified to allow for hhtp/https and www-/non-www variations of the referring domain, and also to allow for blank referrers as mentioned in my previous post.

I cannot suggest any improvements to the second RewriteCond, because I don't know which user-agents you intend to allow.

The third RewriteCond has been modified by adding an end anchor. This will prevent the possibility of confusion between IP addresses such as 78.56.34.12 and 78.56.34.120 if the final octet of the allowed IP address is shorter than the one attempting access.

I have to ask: Are you sure you want to redirect if *any* of these conditions are true, or do you only want to redirect if *all* of these conditions are true. If you want to require all of them, then remove the "OR" from the RewriteCond flags. As it stands, anyone with a blank referrer will be redirected. But if you disallow blank referrers, you may find yourself blocked if a network cache (for example, at your ISP) suppresses your own browser's referrer header...

Jim

Mystique

5:27 pm on Nov 5, 2008 (gmt 0)

10+ Year Member



so my code will be like this?

RewriteEngine on
Rewritebase /
#
RewriteCond %{HTTP_REFERER} ^(https?://(www\.)?homepage\.net.*)?$ [NC,OR]
RewriteCond %{HTTP_USER_AGENT} im-allowed [NC,OR]
RewriteCond %{REMOTE_ADDR} ^111\.111\.111\.111$ [NC]
RewriteRule .* http//www.SecretSite.com/? [R=301,L]
#
RewriteRule ^/secret http//www.searchtest.com [R=301,L]

i feel something weird..

this "/secret" folder is just a shield
so every visitor will be bounced to either realsite or fakesite based on ip & others..

jdMorgan

6:58 pm on Nov 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is no need to "bounce" anyone anywhere. It's a waste of server resources. You can simply deny the access using the rule I posted above.

And again, using [OR] in your RewriteConds means that access to "secretsite" will be allowed if:

  • http://www.secretsite.com is requested directly (for example, by the user typing it in.) OR
  • the referrer is homepage.net (using either http or https protocol) OR
  • the referrer is www.homepage.net (using either http or https) OR
  • the referrer is blank OR
  • the requesting IP is 111.111.111.111

    Is that what you want?

    As noted, if you check the referrer, then you *must* allow for it to be blank, because referrers are not always sent by the browser, and because caching proxies (for example, in ISP and corporate networks) will often suppress the HTTP Referer header, and there is nothing we can do about it. Denying blank referrers will result in people reporting to you that your site is broken, and you will probably occasionally deny your own accesses as well.

    ---

    I should note that it is a mistake to proceed to coding a solution until the requirements are well-defined.

    You may want to reconsider this method from the beginning. This code should be located on the "secretsite server, and it should be probably be structured like this:

    If not (referred by approved domain or blank referrer) AND
    If requesting client's IP addresss is not 111.111.111.111
    Then deny access by returning 403-Forbidden

    This is the only way you can protect your "secret" site against access if the user just types in the address.

    Jim

  • Mystique

    8:36 pm on Nov 5, 2008 (gmt 0)

    10+ Year Member



    Hi, Jim..

    Btw, i do not want to protect "secret" folder..

    it's something blackhat redirect for YSM..

    I can't tell you more in public..

    Please PM me your email because its hard to PM u or at least clean your inbox.. i always get a message "recipient's mailbox is full"

    Jim, I really need suggestion what programming should i use..

    jdMorgan

    9:13 pm on Nov 5, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You should be aware that an external redirect is a response sent to the client, telling it to ask again for what it wanted, but using a different URL. So it is a poor way to attempt to "cloak" anything.

    And of course, unless your solution is "perfect," such attempts to cloak will not survive a hand review by a search employee using a standard browser from a generic DSL or cable ISP address. A frequent result of such a review is that your account will be closed, your pending funds confiscated, and you will not be allowed to open a new account -- ever; Such bans are usually permanent. So I advise extreme caution until you have spent several months (or years) achieving expertise in these matters.

    We prefer that all question be posted and answered in public here, so as to benefit all members who are following the thread -- and the many more who will find it by searching and read it later. The moderators and contributors here are all volunteers, and have little time to offer "free private consulting" -- To do so would not be an efficient use of their time; It would not benefit the WebmasterWorld community. If you need such services, consider searching on-line for a consultant who specializes in the exact services that you need.

    Otherwise, you may post a description of your goals in as much detail as you like, obscuring your domain names, IP addresses, e-mail addresses, and products or services as you have already done above. If you stay within our Terms of Service [webmasterworld.com] and post no personally-identifiable information here or in your WebmasterWorld profile, then getting answers to your questions should not present any problems.

    You may find that our cloaking [webmasterworld.com] forum is a more-focused venue for your "What should I do" questions. Having determined exactly what you need to do, this forum (Apache) is a good place for the "How should I do this?" questions if you're hosted on an Apache server. As previously-stated, you must thoroughly-define the problem before attempting to code a solution.

    Thanks,
    Jim

    Mystique

    7:02 pm on Nov 6, 2008 (gmt 0)

    10+ Year Member



    Thnaks Jim, i have re think it again.. i won't trade blackhat redirect with my account being banned..haha..

    btw,
    im using simple redirect now for other purpose..
    can u fixed this code so that query string dismish..
    i want to use mod alias redirect


    redirect 301 /test/ http://www.newexample.com/?

    why it's always shows "?" ?

    [edited by: Mystique at 7:03 pm (utc) on Nov. 6, 2008]

    jdMorgan

    7:07 pm on Nov 6, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    It shows the "?" because you included it in the new URL.

    You may have confused the syntax of this mod_alias Redirect directive with the syntax of mod_rewrite's RewriteRule, where the "?" is an operator. In a Redirect directive, the "?" is a literal, and so will be included in the redirected URL.

    Jim

    Mystique

    7:33 pm on Nov 6, 2008 (gmt 0)

    10+ Year Member



    ohh, so how to remove all query strings being sent to new domain if using mod_alias Redirect directive?

    jdMorgan

    8:32 pm on Nov 6, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Mod_alias cannot do that job. You must use mod_rewrite.

    Jim

    Mystique

    2:44 am on Nov 7, 2008 (gmt 0)

    10+ Year Member



    hmm, mod_alias really weird.. if i using this:

    redirect 301 /test/ http//www.example.com/?view=home

    i get redirected to http//www.example.com/?view=homeindex.php
    & if i change the redirect site

    redirect 301 /test/ http//www.cnn.com/?view=home

    i got redirected to http//www.example.com/?view=home/
    why that happend?..

    [edited by: jdMorgan at 2:59 am (utc) on Nov. 7, 2008]

    jdMorgan

    3:05 am on Nov 7, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Flush your browser cache after changing any code.

    Here's a mod_alias redirect line:
    Redirect /prefix/ http://www.example.com/prefix/
    If /prefix/<anything> is requested, then the redirect is to http://www.example.com/prefix/<anything>

    Also, if any internal rewrite occurs before this redirect, then the results of that internal rewrite will be "exposed" by the redirect. For example if you use a RewriteRule or a DirectoryIndex directive, the results of their rewrites can be exposed by the redirect.

    None of that matters, anyway. If you wish to clear the query string without the "?" appearing in the redirected URL, use mod_rewrite. Do not use both mod_alias Redirect directives and mod_rewrite directives: If you use mod_rewrite for any rewrites or redirects, then use it for all of them.

    Jim

    Mystique

    1:57 pm on Nov 9, 2008 (gmt 0)

    10+ Year Member



    Also, if any internal rewrite occurs before this redirect, then the results of that internal rewrite will be "exposed" by the

    redirect. For example if you use a RewriteRule or a DirectoryIndex directive, the results of their rewrites can be exposed by

    the redirect.


    Hi,

    ----------------------------------------------------------------------------------
    1. Jim, what do you mean by my Rewriterule will be exposed if i use them before mod_alias redirect?
    the visitor will be able to read the line in "rewriterule" in .htaccess?

    ----------------------------------------------------------------------------------
    2. and what is the different between using slash and don't after prefix?


    Redirect /prefix/ http//www.example.com
    Redirect /prefix http//www.example.com

    i tried them both, but i feel that if not using a slash is much faster to be redirected.. is that true?..
    this is the test results:


    Redirect /prefix/ http//www.example.com

    much faster when:
    in url bar im using slash after the directory (http//example.com/prefix/)


    Redirect /prefix http//www.example.com

    much faster when:
    in url bar im not using slash after the directory (http//example.com/prefix)

    ---------------------------------------------------------------------------------
    3. why if i'm using:


    Redirect 301 /prefix/ http//www.example.com

    & in url bar i don't using slash after the directory((http//example.com/prefix)

    i got redirected 2 times before go to http//www.example.com ?

    first: http//example.com/prefix(301)
    second: http//example.com/prefix/(301)
    finnaly: http//www.example.com

    [edited by: jdMorgan at 4:17 pm (utc) on Nov. 9, 2008]
    [edit reason] Corrected [ quote ] tags, example.com [/edit]

    g1smd

    2:25 pm on Nov 9, 2008 (gmt 0)

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



    The double redirect must be avoided.

    .

    As for the order for redirects and rewrites, consider this..

    If you do a rewrite before a redirect, the "hidden" internal filepath that you were rewriting to, suddenly appears in the URL... and that is bad.

    Example 1:

    1. Redirect "URL x" to "URL y"
    2. Rewrite "URL request y" to internal filepath "z".

    User types in URL "x" and is redirected to URL "y". User sees URL "y" in the browser URL bar, and the server silently gets the content from "z". This is what it should do. Users sees "y" and content silently comes from "z".

    Example 2:

    1. Rewrite "URL request x" to internal filepath "z".
    2. Redirect "URL x" to "URL y"

    User types in URL "x" and server updates internal references to URL to be "z". System then discovers the following redirect and redirects to "y" but with "z" (which was supposed to be "secret") appended to the path info, and user sees "z" exposed in the URL shown in the browser URL bar. This is bad.

    [edited by: g1smd at 3:08 pm (utc) on Nov. 9, 2008]

    Mystique

    3:02 pm on Nov 9, 2008 (gmt 0)

    10+ Year Member



    yes i know double redirect is bad.. the one i want to ask is, what is the diferent using slash or not in apache..

    jdMorgan

    3:09 pm on Nov 9, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    In the HTTP protocol, a URL with no trailing slash refers to a "page" or a file. A URL with a trailing slash refers to a directory --a listing of all files within a folder-- or to a directory index page, such as a "home page" at the URL "example.com/".

    In Apache, if a URL is requested without a slash and there is no such page, then Apache mod_dir will check to see if the URL will resolve to an existing directory if a trailing slash were to be added. If so, Apache mod_dir will append a trailing slash to the requested URL and send a redirect response to the client, telling it to use that new URL to re-request the directory it was asking for. This terminates the current HTTP transaction.

    The client will then (usually) issue a second HTTP request, using the new URL with appended slash as provided by the server's redirect response.

    Jim

    g1smd

    3:10 pm on Nov 9, 2008 (gmt 0)

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



    If the resource is a folder then the correct URL will have a trailing slash, and URLs without trailing slash will redirect to a new URL with the slash appended.

    If the URL is not a folder, then it should not end with a slash at all.

    Mystique

    3:58 pm on Nov 9, 2008 (gmt 0)

    10+ Year Member



    Thanks g1smd & Jim

    Jim, im still not understand with this:

    Also, if any internal rewrite occurs before this redirect, then the results of that internal rewrite will be "exposed" by the redirect. For example if you use a RewriteRule or a DirectoryIndex directive, the results of their rewrites can be exposed by

    the redirect.

    Jim, what do you mean by my Rewriterule will be exposed if i use them before mod_alias redirect?
    the visitor will be able to read the line in "rewriterule" in .htaccess?

    [edited by: jdMorgan at 4:18 pm (utc) on Nov. 9, 2008]
    [edit reason] Corrected [ quote ] tags. [/edit]

    jdMorgan

    4:14 pm on Nov 9, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    g1smd described the problem above, and I'm afraid I can't do much better than that description...

    Jim

    g1smd

    9:58 pm on Nov 9, 2008 (gmt 0)

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



    I can't really do much better than what I wrote in #3782979.

    Note that one example has a redirect then a rewrite, and the other has them reversed.