Anyone good with .htaccess?

designsonline

Power Member
Joined
Jan 22, 2009
Messages
651
Reaction score
146
Ive hunted through all the .htaccess sites and tutorials I can find and cant find one that will do this: (and ive tried modifying all the existing ones too and they wont do it!)


I want to combine the standard SEO rewrite to add missing www. etc..

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

With a rule that will also change any access to my domain (with or without the www.)

from http://www.domain.com

to http://www.domain.com#my_longtail_keyword




SO the end result im after is that if anyone visits my website at the domain level. they end up at http://www.domain.com#my_longtail_keyword

But I still want them to be able to visit pages within my site without being redirected, for example they could visit www.domain.com/mypage1.html and it will load, but if the visit www.domain.com they will be redirected via a 301 to http://www.domain.com#my_longtail_keyword


So if you are a .htaccess guru could you please post up the solution...?
Thanks
Is that clear enough? I just cant get it to work, ive been trying for 2 weeks, I need a hand.
 
I'm pretty sure htaccess can't read check to see if there is an anchor in the url (only seen by browser) and setting a redirect would therefor result in a infinite loop. (Although I suck at htaccess so I could be wrong)

My suggestion, if your homepage is a php file, through the following code at the very top:
PHP:
<?php
if(!isset($_COOKIE['temp'])){
setcookie('temp', 'temp', time()+30);
header("Location:http://domain.com/#keyword");
}
?>

This would check to see if the person has the cookie called "temp". If he doesn't (all first time viewers wouldn't have it) the page would set a temporary cookie that would be deleted in 30 seconds, and redirect to domain.com/#keyword. Once on the page, the person won't be redirected again because he already has the cookie (infinite loop problem fixed!).

This code is untested, so let me know if it works.
 
Thanks for the code, but it is an all HTML site! So I do need to do it in .htaccess

Does anyone else have any ideas?
 
Try this:

Code:
RewriteEngine on

# If there wasn't anything after the domain, append the LTK and redirect 
RewriteRule ^$ http://www.mydomain.com#log-tail-keyword [L,R=301]

# If there's no www in the domain name
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
#  Prepend www and append whatever was after the domain
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301,QSA]
 
Ooops.

The code above won't work. There's a basic problem with what you're trying to do. The anchor part of the URL (the part after the #) won't get sent to the server, so there's no way the server can tell the difference between mydomain.com and mydomain.com#stuff which makes it impossible for the rewrite to know when to redirect and when not to.

To get a URL with your keywords try:

Code:
RewriteEngine on

# Handle the no page/script case. Add the LTKs and redirect
# takes care of "http://www.domain.com/" and "http://domain.com/"
RewriteRule ^$ http://www.mydomain.com/long-tail-keywords [L,R=301]

# Handle "(www.)domain.com/long-tail-keywords" and do an internal redirect to whatever script you want 
RewriteRule ^[^\.]*$ myscript.php [L,QSA]

# If there's no www in the domain, name prepend www and append whatever was after the domain
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301,QSA]

This will redirect domain.com and http://www.domain.com to http://www.domain.com/long-tail-keywords. The subsequent request to http://www.domain.com/long-tail-keywords gets redirected internally to myscript.php

Anything with a dot in the URL will get passsed through with domain.com/somepage.php redirected to http://www.domain.com/somepage.php

It's a bit cheap and nasty because it relies on anything you want to pass through having a dot in the URL, so http://www.mydomain.com/mydirectory/ will be treated as though "mydirectory" was a LTK, but it could be tweaked to handle that case if needed
 
Last edited:
I'm pretty sure htaccess can't read check to see if there is an anchor in the url (only seen by browser) and setting a redirect would therefor result in a infinite loop. (Although I suck at htaccess so I could be wrong)

My suggestion, if your homepage is a php file, through the following code at the very top:
PHP:
<?php
if(!isset($_COOKIE['temp'])){
setcookie('temp', 'temp', time()+30);
header("Location:http://domain.com/#keyword");
}
?>
This would check to see if the person has the cookie called "temp". If he doesn't (all first time viewers wouldn't have it) the page would set a temporary cookie that would be deleted in 30 seconds, and redirect to domain.com/#keyword. Once on the page, the person won't be redirected again because he already has the cookie (infinite loop problem fixed!).

This code is untested, so let me know if it works.

There's a problem with doing this. Anyone who doesn't accept cookies gets into a redirect loop. And last time I checked, that includes Googlebot. Googlebot isn't happy when you redirect it to the page it just came from - as I have found to my cost in the past :)
 
There's a problem with doing this. Anyone who doesn't accept cookies gets into a redirect loop. And last time I checked, that includes Googlebot. Googlebot isn't happy when you redirect it to the page it just came from - as I have found to my cost in the past :)

didn't know that. how about sessions?

An alternative fix... temporary text file or db with timed i.p. list.
 
Sessions are implemented with cookies by default in PHP, and athough you can use sessions without cookies, you have to append an ugly session ID to all your urls.

Unfortuately you can't guarantee that an IP will remain constant for a single visitor, nor can you assume that two accesses from a single IP within a short time is the same person. They almost always are, but not necessarily.
 
You can't do this with .htaccess. You are trying to process data that is only show to the client via server side software. I think the answer is in javascript, an onload function or something...
Code:
<script type="text/javascript">
function on(){
//check if this if front page something with top.location or window.location or window.url donno
//if yes then
window.location.hash="#your keyword_here";
}
window.onload = on;
</script>
fiddle with the code to see if anything is right.
 
Last edited:
There are multiple types of redirects that will accomplish this but that depends on several different server factors... and how dead-set you are on having it done with a 301 redirect at the same time.

Do you need that 301 for a reason or will another type of redirect that still transfers the traffic the way you said be acceptable to you?
 
Thanks for the code, but it is an all HTML site! So I do need to do it in .htaccess

Does anyone else have any ideas?

if your server supports it i think you can still process .html/.htm as whichever script you use.

Code:
AddType application/x-httpd-php .html .php .htm
or
Code:
AddHandler application/x-httpd-php .html .php

in the .htaccess (for PHP). i cant remember which, try looking up "AddType" and "AddHandler" in the Apache docs (assuming Apache webserver!)

i'm too late for regexp and .htaccess - i hope that is of some use. you can definitely take something sent via querystring etc and parse it with PHP.
 
Last edited:
For .htaccess, this works in my sites :
Code:
RewriteCond %{HTTP_HOST} !^www.domain.tld$
RewriteRule (.*) http://www.domain.tld/$1 [R=301,QSA,L]
 
There are multiple types of redirects that will accomplish this but that depends on several different server factors... and how dead-set you are on having it done with a 301 redirect at the same time.

Do you need that 301 for a reason or will another type of redirect that still transfers the traffic the way you said be acceptable to you?

Basicly im trying to find a way to get the keywords im targeting in the URL for the main domain of a website that doesnt have the keywords in the domain.

So for those of you who use market samurai imagine that all you need to do it get the keywords in the URL and then you would have a red score right across the board, this is the edge I need to get to the top of the first page, that I am already half way up.

The reason that I am trying to use a 301 redirect is that I dont want to lose the 8k backlinks that I have to the root domain, but rather I want them to be passed over to the URL http://www.mydomain.com#my-keyword

I tested this URL in Market Samurai and it is exactly what I need to dominate, but the only problem is that I only have a few links to that URL.

Also I dont want any duplicate content issues, so I want Google to change it's index of http://www.mydomain.com to http://www.mydomain.com#my-keyword for my website, without doing myself any damage along the way.

Does that make sense to anyone?
 
Basicly im trying to find a way to get the keywords im targeting in the URL for the main domain of a website that doesnt have the keywords in the domain
My example (the corrected one!) will do exactly that. It won't use "mydomain.com#my-keywords" but it will work just fine for SEO as "mydomain.com/my-keywords" which is what my example will redirect to.

Existing links to "mydomain.com" will get a 301 redirect to "mydomain.com/my-keywords" (thus transferring existing link juice), and requests to "mydomain.com/my-keywords" get redirected internally to whatever page you want. Just change "myscript.php" to the HTML file you want to display.

You don't have to play with PHP, MySQL Apache config files or anything else. It just works.
 
Basicly im trying to find a way to get the keywords im targeting in the URL for the main domain of a website that doesnt have the keywords in the domain
My example (the corrected one!) will do exactly that. It won't use "mydomain.com#my-keywords" but it will work just fine for SEO as "mydomain.com/my-keywords" which is what my example will redirect to.

Existing links to "mydomain.com" will get a 301 redirect to "mydomain.com/my-keywords" (thus transferring existing link juice), and requests to "mydomain.com/my-keywords" get redirected internally to whatever page you want. Just change "myscript.php" to the HTML file you want to display.

You don't have to play with PHP, MySQL Apache config files or anything else. It just works.
 
Existing links to "mydomain.com" will get a 301 redirect to "mydomain.com/my-keywords" (thus transferring existing link juice)

Doesn't really xfer 'link juice' that way. Maybe 2 or 3 year ago yes. You should go read evil matt's blog hxxp://mattcutts.com/blog/ for more about that.
 
I want to combine the standard SEO rewrite to add missing www. etc..

With a rule that will also change any access to my domain (with or without the www)

from http://www.domain.com

to http://www.domain.com#my_longtail_keyword

This is a bad idea. This should be done as two separate rules. Leave the RewriteCond and RewriteRule you have there alone and add the following one after them:

Code:
RewriteRule ^/$ /whatever-url-you-want [R=301,NS,QSA,L]

If you visit yoursite.com, you will be 301'd to http://www.yoursite.com. That will 301 you to your destination keyword page.

Ignore the PHP and Javascript "solutions" presented in this thread. This is not an application-level concern.
 
Back
Top