Forum Moderators: phranque
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]
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]
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
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]
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
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..
Replace the Redirect directive with:
RewriteRule ^/secret - [F]
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]
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
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..
And again, using [OR] in your RewriteConds means that access to "secretsite" will be allowed if:
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
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..
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
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]
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
redirect 301 /test/ http//www.example.com/?view=home
redirect 301 /test/ http//www.cnn.com/?view=home
[edited by: jdMorgan at 2:59 am (utc) on Nov. 7, 2008]
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
Also, if any internal rewrite occurs before this redirect, then the results of that internal rewrite will be "exposed" by theredirect. For example if you use a RewriteRule or a DirectoryIndex directive, the results of their rewrites can be exposed by
the redirect.
----------------------------------------------------------------------------------
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
Redirect /prefix http//www.example.com
---------------------------------------------------------------------------------
3. why if i'm using:
Redirect 301 /prefix/ http//www.example.com
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]
.
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]
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
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 bythe 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]