News:

LVCS.NET offers low cost domain registration services.

Main Menu

.htaccess question

Started by Mojo, December 16, 2005, 06:35:36 PM

Previous topic - Next topic

Mojo

I've read that Google applies a penalty to duplicate content, so if it finds two variations of a link to the same page, the page loses its position in the results. For example:

http://www.freepgs.com/username/
and
http://freepgs.com/username/

would get penalized as duplicate content.

I created an .htaccess file to redirect the second variant to the www version:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^freepgs.com
RewriteRule ^.*$ http://www.freepgs.com%{REQUEST_URI} [R=301,L]

It works, but the part I haven't been able to figure out is how to strip "index.htm" and "index.php" from the URLs for the root directory and sub-directories.  For example:

Convert
http://www.freepgs.com/username/index.htm
to
http://www.freepgs.com/username/
and
http://www.freepgs.com/username/subdirectory/index.php
to
http://www.freepgs.com/username/subdirectory/

I did some Google searching but none of the examples which looked as if they would do the job worked for me. I find it confusing that there's usually more than one way to get the same result with .htaccess. It's like a black art because I'm not familiar with Regular Expressions. I'd be very grateful if someone could tell me what to add to the three line .htaccess file that I have already.

webzone (archived)

#1
Try this :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^freepgs.com$
RewriteCond %{REQUEST_URI} ^.*\/index\.(php|html?)$ [NC]
RewriteRule ^(.*)index\.(php|html?)$ http://www.freepgs.com/USERNAME/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^freepgs.com$
RewriteRule ^(.*)$ http://www.freepgs.com/USERNAME/$1 [R=301,L]


Edit (3): Fixed. All should be working now.

The working code is below.

Mojo

I didn't get a chance to return to the forum until after you'd done Edit 3, but it didn't work for me. First of all, I changed (php|html?) to (php|htm) - the extensions I use - but now either "index.htm" or "index.php" bounce me to an invalid address. It looks like you mixed the code from your .htaccess with the original example because the "last rule" flag [L] appears twice.

Here's what I want to do.....

Enter:
http://freepgs.com/username/blog/index.php
and end up at:
http://www.freepgs.com/username/blog/

I checked it out at your site....

http://freepgs.com/webzone/index.htm
gets redirected to:
http://www.freepgs.com/webzone/
but
http://www.freepgs.com/webzone/index.htm
ends up at exactly the same place:
http://www.freepgs.com/webzone/index.htm




webzone (archived)

QuoteIt looks like you mixed the code from your .htaccess with the original example because the "last rule" flag [L] appears twice.

It is perfectly normal to put the "last" flag more than once. It means "*if this RewriteRule is executed*, then do not process the ones that follow in the file". Basically, my code says : "if the first set of rules is used, then stop there and don't continue. elsewhere, process the next ones."

QuoteI didn't get a chance to return to the forum until after you'd done Edit 3, but it didn't work for me.

Fortunately, because the previous edits were incorrect (i fixed those in the minutes following my post).

QuoteHere's what I want to do.....

Enter:
http://freepgs.com/username/blog/index.php
and end up at:
http://www.freepgs.com/username/blog/

Really sorry, I read your post too fast. I'm going to change my code to make it work.

webzone (archived)

RewriteEngine on
RewriteCond %{HTTP_HOST} ^freepgs.com$
RewriteRule ^(.*)$ http://www.freepgs.com/USERNAME/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^.*\/index\.(php|html?)$ [NC]
RewriteRule ^(.*)index\.(php|html?)$ http://www.freepgs.com/USERNAME/$1 [R=301,L]


This should work as expected. I thought that you wanted to remove "index.htm" while you were redirecting from freepgs.com to www.freepgs.com. I should have read more closely (I'm sorry).

(sorry for double posting)

Mojo

#5
That's terrific! It's working perfectly. And thanks for explaining how the "last rule" flag works. I wasn't quite sure because .htaccess is voodoo to me. Also, I didn't realize that (php|html?) works just as well as (php|htm).
:-)

sinex

What is the name before extension .htaccess file I have to put in my subdirectory

I have not seen that anywhere

Thanks in advance

dest

There isn't one.  It's just .htaccess.  If you can't do that on your computer, put something in front of it, and rename it just .htaccess once uploaded.
meh :P