Forum Moderators: phranque

Message Too Old, No Replies

Redirect to a subdirectory using htaccess.

How to redirect from the root to a subdirectory using htaccess.

         

tommytx

4:59 am on Apr 12, 2011 (gmt 0)

10+ Year Member



Wow i have been messing with this simple thing all night and reading all your posts on htaccess and still have no positive results.

Sample 1 URL:
http://www.mydomain.com/?s=no-results:no-results%3Asharon%20milligan&cat=no-results

Sample 2 URL:
http://www.mydomain.com/?s=no-results:no-results&cat=no-results

# Begin of my htaccess file.
options all
RewriteEngine on
RewriteBase /
ReWriteRule no-results /idx/test.php [L]

# Mandatory wordpress code that must stay in place.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule html /cgi-bin/index.pl [L]
RewriteRule ^$ /index.html
# End of mandatory Wp code.

# End of my htaccess file.



# Some other things I tried..
# RewriteCond %{QUERY_STRING} ^=no-results$
# RewriteRule ^.*$ /idx/test.php [L]



I have a ton of this junk indexed in google, and what I want to do is detect them when the come in by human or by google and forward them to a real url so they will be indexing real stuff and if a human will show them real stuff.
That is what the file test.php does, sends them to a nice page. Then logs that page so if the client comes back or google checks up it will return them the same page over and over.

1. As you can see the only thing that is guaranteed to be constant is the first "no-results" so I want to check the Query for the word "no-results" and if there forward to the idx subdirectory for processing. If not continue on their merry way. I have included the Wordpress code just in case someone has suggestions as to the best place to position the code in the actual htaccess file. I assume its best to place it before the wp code.. since if its valid the wordrpess page will never load. Hopefull the [L] following the code will prevent the wordpress code from running if it is a valid page. Meaning the url has the word no-results.

2. only 2 sample urls are shown, but there are many but they all have the first "no-results" at the beginning of the query.

g1smd

6:49 am on Apr 12, 2011 (gmt 0)

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



Your second rule is the closest (the one testing QUERY_STRING) but because you added the ^begins and ends$ syntax it will work only if the query string is exactly "no-results" with nothing else in the query string. Remove the ^ and $ from the condition.

Your code is for an internal rewrite. So, whenever a URL with "no-results" is requested, you will serve the content of the file at "/idx/test.php" and the URL bar of the browser will not change. This creates infinite Duplicate Content as the rule effectively says every requested URL is valid and they all return the same content.

What you probably need is an external redirect to a new URL. Alter the rule so that the target includes the protocol and domain name and then add the [R=301,L] flags to it. Place a question mark after the target URL to clear the query string in the redirect otherwise you will get an infinite loop.

The "no-results" redirect must be placed before the Wordpress code otherwise it will never run.

tommytx

7:48 am on Apr 12, 2011 (gmt 0)

10+ Year Member



<code>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^s=no-results
RewriteRule ^.*$ /idx/test.php? [R=301,L]
# RewriteRule ^.*$ [vahud.com?...] [R=301,L]
</code>

Thank you! That got me out of that muck, and both work fine. Don't notice any continuous loop its fast, and then the browser says I am done by the little rotating thingy stopping.

I was able to keep the littel ^ carrot on the left side since the content will always begin there. But you were right I needed to remove the ^ on the right as that content is not constant.

Thanks again you are a lifesaver... I was close.. but that only counts in horseshoes.

tommytx

8:09 am on Apr 12, 2011 (gmt 0)

10+ Year Member



Whoops I spoke too soon.
RewriteCond %{QUERY_STRING} ^s=no-results
It must match exact to pass. What do I need to add onto the end so that it won't matter what else is tied on there for example it might look like this.
RewriteCond %{QUERY_STRING} ^s=no-results:more-trash:no-results:hate-trash:and-ecetera

Would I use a wild card something like this?
RewriteCond %{QUERY_STRING} ^s=no-results(.+)

Not sure what the wild card for the rest of it is.

tommytx

8:11 am on Apr 12, 2011 (gmt 0)

10+ Year Member



Actually what I meant is the the url might look like this and it varies...
[mydomain.com...] and even longer urls.. but the first part s=no-results is in every URL no matter what.

tommytx

8:15 am on Apr 12, 2011 (gmt 0)

10+ Year Member



OHMYGOD! You folks are teaching me something even if it is thru osmosis. The (.+) worked like a charm. Thankyou very much for all the help g1smd. You gave me a push and I ran with it.

jdMorgan

4:38 pm on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The new line

RewriteCond %{QUERY_STRING} ^s=no-results(.+)

will require that *something* follows "s=no-results". The rule won't be invoked if the query is *exactly* "s=no-results" and nothing following.

However, changing it to

RewriteCond %{QUERY_STRING} ^s=no-results(.*)

is entirely equivalent to the originally-posted

RewriteCond %{QUERY_STRING} ^s=no-results

unless you are actually using the back-referenced value stored by the parentheses.

Therefore, it is quite likely that you've missed out on a very important factor in testing server-side code of any kind -- be it config code, .htaccess code, PERL or PHP code, etc. -- Be sure to delete your browser cache before testing any new server-side code. Otherwise, your browser will very likely show you previously-cached content and server responses. Either delete your browser cache after each change to server-side code, or disable your browser cache for the duration of testing by setting its size or "keep time" to zero (the appropriate method depends on the specific browser in use).

Doing this will avoid many "strange and unexplainable" problems.

That said, the maximally-robust implementation of your RewriteCond would be:

RewriteCond %{QUERY_STRING} ^([^&]*&)*s=no-results(&.*)?$

Allowing for any number (zero or more) ampersand-delimited query parameters to precede or follow "s=no-results", while requiring that the "s" value be exactly "no-results". Any other "loose-match" approach could allow "pests=no-results" or "s=no-results-whatsoever-mate" to match, both of which are undesirable.

Jim

g1smd

8:31 pm on Apr 14, 2011 (gmt 0)

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



Jim, I have recently seen several sites using an alternative to your code:
RewriteCond %{QUERY_STRING} (^|&)s=no-results(&|$)


Any comments?

jdMorgan

9:18 pm on Apr 14, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just another way to skin the same cat... :)

Jim

g1smd

9:25 pm on Apr 14, 2011 (gmt 0)

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



As long as it doesn't trigger some "weird Apache bug"... :)

tommytx

9:46 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Wow... I must have my email notification turned off.. all these nice posts and I did not even know. Anyway this works fantastic RewriteCond %{QUERY_STRING} ^s=no-results(.+)
And as Morgan mentioned there must always be something more following and there is always a long line of text following.. there will never be just that term. So with that given is the code safe as is, as it is working well or must I make it more complicated?
I want to keep it the same if I can. Especially if it will only fail if there is not more text.. there will always be a lot more text.
Thanks for all the suggestions.

g1smd

10:09 pm on Apr 14, 2011 (gmt 0)

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



You can make it less complicated.

Delete the (.+) as it takes time to parse the rest of the input and then do nothing with it.

The code will then run whether there is anything following or not.

tommytx

10:23 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Well it would only run with exact match until I added that. I will go right now and remove and test again. Simple is better... hee...hee and I am as simple as it gets.
Actually I am working on another horrendous problem, and they seem to have given up.. I would bring it here but then it would be dupe content. You folks are so smart you could probably solve it...
here is the thread... and I have broken it up so as not to give a backlink to the competition. Its a real doozy. Part of the same package.
hxtxtxp:x/x/forums.devshed.com/dns-36/a-record-forwarding-the-non-www-subdomain-805202.html

tommytx

10:31 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



1. RewriteCond %{QUERY_STRING} ^s=no-results(.+)
2. RewriteCond %{QUERY_STRING} ^s=no-results(.*)
3. RewriteCond %{QUERY_STRING} ^s=no-results

Well I was wrong, the version being used was item 2, not item 3.
But I guess item 1 or 2 would work.. just extra work.
I removed the end as in item 3 and it worked fine... So thanks that did simplify it some more.. however it will alwasy be a long url every time.

g1smd

10:35 pm on Apr 14, 2011 (gmt 0)

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



3. "Begins with", don't care what, if anything, follows it.

2. "Begins with", and may or may not have something following, capture that "something" in the %1 backreference.

1. "Begins with", and MUST have something following, capture that "something" in the %1 backreference.

[edited by: g1smd at 10:50 pm (utc) on Apr 14, 2011]

tommytx

10:38 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Wow.. what a simple explanation and easy to follow...
Are you willing to cut and paste the url to throw in your thoughts.

tommytx

10:41 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Oops! cut and paste the url to throw in your thoughts
Correct cut and paste the url 4 boxes above throw in your thoughts.

I think I will cut and paste your expanation into my snipped box that I lean on sometimes when I am lost.

tommytx

10:50 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



RewriteCond %{QUERY_STRING} s=no-results

OK.. lets see if I learned anything. This says does not matter if anything is before or after the term, but to match the term must be there somewhere. Yes means it qualifies to meet the answer.

RewriteCond %{QUERY_STRING} s=no-results

1. "s=no-results" = yes
2. "no-results" = No
3. "s=no-results:no-results" = yes
4. "s=no-results-" = Yes
5. "s=no-result" = No

g1smd

10:55 pm on Apr 14, 2011 (gmt 0)

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



Yes, you are correct, but this also matches
this=no-results
as there is no beginning anchor to say "begins with" "s=" or is preceded immediately by "&". That's what the
(^|&)
in my code above does.

Re: [forums.devshed.com...]

# Redirect example.com URLs to other site
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.exampleothersite.com/$1 [R=301,L]
#
# Canonicalise all other requests to be www.example.com
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

tommytx

11:29 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Will google be more aware of the redirect if I do not use the A or CNAME.. and instead use htaccess as you suggest?

I am well aware of the htaccess stuff to make the google juice be directed to one site..

In other words you have a www or non www. site but not both and google will not split pr between the two. So in general we see the pr climb a little when you have only one name www. or non www.

Does google get to know anything when redirecting by cname or a record... or do the know the same about either method. so using 301 tells google that it is a permanent move.

So is this correct then.. if I direct eample.com to idx.com can I then direct www.example.com to example.com to make it appear again that I have only one site which will be example.com or is google thinking that I have only one site called idx.com Will google even know about idx.com?

What I want is for it to look like I have example.com and no www.example.com and no one to know its really idx.com doing all the work in the background.

Right now I have been setting up a search.example.com and pointing example.com to the subdomain search.example.com and sure enough google is indexing all the search.example.com pages and you can easily see this when you do site:example.com

My hope is to forward example.com to idx.com will make google index pages under example.com and not search.example.com.

To see a live situation visit halfa.com and do a site:halfa.com in google. This shows 70,000 pages indexed under home.halfa.com with a few thousand under halfa.com and we know that halfa.com is pointed to home.halfa.com. So google is indexing under search.halfa.com vice the actual work site idx.com as far as I know none of the pages index under idx.com.

The problem I see is even though they got 70,000 plus pages indexed, they are running a whopping PR1. Now they may not have a lot of backlinks so low pr might be deserved, but with 70,000 pages you would think they could get at least a pr2 just for working so damn hard... makes no sense.

I am wondering if making sample.com the sub and using www. as the main site.. google would not see any sub like home.sample.com or search.sample.com and only see sample.com which will be pointing to idx.com.

So I will be using www.sample.com as my home page and anytime I want to see properties I will have links on my homepage like.. sample.com/myhouses_for_sale etc... tons of these links.

Then maybe add in a few good back links and try to get the PR above a pr1.

IF anyone else is trying to follow all this crap about 5 boxes up is a link to a whole lot more... but it looks like g1smd has given me the most hope.. I am being sure not to dupe the content on the other site... I hate duplicate content.


This is making me crazy... any thoghts.

g1smd

11:37 pm on Apr 14, 2011 (gmt 0)

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



Google indexes URLs which directly return the "200 OK" HTTP status code.

Google does not index URLs which return a "301 Moved Permanently" HTTP status, as there is no content AT that URL.

Within your site, do not link to any URLs which return any sort of redirect status code when clicked.

tommytx

11:45 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



Wow! I think this may be perfect... but can I understand it..

tomsite.com = my main site
idx.com = the idx site


# Redirect tomsite.com URLs to idx.com
RewriteCond %{HTTP_HOST} ^tomsite\.com
RewriteRule (.*) http;//idx.com/$1 [R=301,L]
#
# Canonicalise all other requests to be www.example.com
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

I am not no real smart guy and i got ADHD so walk me thru.. I think I got it..

RewriteCond %{HTTP_HOST} ^tomsite\.com
RewriteRule (.*) http;//idx.com/$1 [R=301,L]

This says if anything comes looking for the domain tomsite.com
Immediately send it to idx.com and if it has any files and other junk attached to it attach that stuff to idx.com to go with it.

R=301 Tell google this is a permanent move... and

L = If this is true stop here and don't do any more stuff in the htaccess file. You are done here.. go on down to idx and get to work.

Qualified contine
*****************
Go here if you are looking for anything other than tomsite.com

RewriteCond %{HTTP_HOST} !^(www\.tomsite.com)?$
RewriteRule (.*) [tomsite.com...] [R=301,L]

This says IF the host is not www.tomsite.com then convert it to www.tomsite.com and add any junk that was attached to tomsite.com

R=301 same as above

L = same as above... but might not be requried since there is no more file anyway.

OK! how bad did I butcher it?

tommytx

11:54 pm on Apr 14, 2011 (gmt 0)

10+ Year Member



I didn't know that! great info:
*******************************
Google does not index URLs which return a "301 Moved Permanently" HTTP status, as there is no content AT that URL.

Within your site, do not link to any URLs which return any sort of redirect status code when clicked.
So for my redirects I should leave the 301 off.. will google detect they are being forwarded if I don't tell them.. I am sure they know when htaccess redirects them to a new domain... but what about a redirct by a record or cname.. do they know.
Obviously they do not know since the just indexed 70,000 pages on home.halja.com when they got redirected from halja.com to the sub.
Wow! this is getting confusing...

g1smd

11:54 pm on Apr 14, 2011 (gmt 0)

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



The explanation in post4297991 is exactly right (there's one syntax error in your code ".com" should be "\.com" in the RewriteCond line)

If the non-www has been requested bounce the user to the other site and quit out on .htaccess processing right here.

If the request was not for a non-www URL proceed to the second rule.

If the requested hostname was not "exactly" www.example.com (for example it was for www.example.com:80 or something else) then bounce the user to www.example.com and quit mod_rewrite processing.

In all redirects, re-append the original path to the new URL (query string data is also automatically re-appended).

g1smd

12:00 am on Apr 15, 2011 (gmt 0)

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



So for my redirects I should leave the 301 off...
I don't understand the question.

A URL which returns "HTTP/1.1 301 Moved Permanently" is, by definition, a redirect.

You should use the Live HTTP Headers extension for Firefox to examine the HTTP transaction between your browser and the server when you make a request for a page.

tommytx

12:07 am on Apr 15, 2011 (gmt 0)

10+ Year Member



home.halfa.com is redirecting to idx.com and I am not sure if they are telling google 301, but they are indexing 70,000 pages on home.halfa.com when infact all the content is located at idx.com

So are you saying that if I tell google 301 they won't index home.halfa.com since it truly has no content... but they are indexing like a big dog and site: proves it.

tommytx

6:57 am on Apr 16, 2011 (gmt 0)

10+ Year Member



I am on a roll now, thanks for all the help. One final problem to solve.

This is the one you folks got working for me...
RewriteCond %{QUERY_STRING} ^s=no-results
RewriteRule ^.*$ /idx/test.php? [L]

Now i need to add the query back to it... here is what I tried no work.
RewriteCond %{QUERY_STRING} ^s=no-results(.*)
RewriteRule ^.*$ /idx/test.php?dog=$1 [L]


It goes to a php file like this....
I need to make sure I got the query string here.
************************************************
test.php
<?
$str=$_GET['dog'];
echo "$dog<br>";
?>

What am I doing wrong?

This is what a live query string might look like..
mydomain.com/hello.php?s=no-results:no-results%3Aarganza&cat=no-result
Bottom line I want to bring the query string over to test.php.