homepage Welcome to WebmasterWorld Guest from 54.237.213.31
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How to redirect from non-html to .html extension
How to redirect from non-html to .html extension
batu544



 
Msg#: 4393296 posted 6:43 am on Dec 2, 2011 (gmt 0)

Hi,
Due to some reason google has indexed two versions of few links from my website. For example

1. http://www.domain.com/some-content.html
2. http://www.domain.com/some-content


but, both the links point to same content and as per my script both the links are also valid. So, now I am trying to redirect the non-html version of my urls to their respective .html version of urls to avoid duplicate content ( urls) as per google webmaster tool.

I think it can be done by .htaccess but I don't have much idea on .htaccess rules, so not able to get it done.


somehow I wrote one code which is doing exactly the opposite of my requirement.. :(


RewriteRule ^(.*).html$ http://www.my-domain.com/$1 [L,R=301]


Could anyone please let me know what to write in .htaccess file to get it fixed ?

Thank you
batu

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4393296 posted 6:52 am on Dec 2, 2011 (gmt 0)

No, but stop and consider this:

If your existing rule is doing the exact opposite of what you want, is it possible that the rule itself is backward? Try turning it around.

But first go over to Apache [httpd.apache.org] and read the sections on RewriteCond and RewriteRule. You can save the other stuff for later.

batu544



 
Msg#: 4393296 posted 7:07 am on Dec 2, 2011 (gmt 0)

Thanks Lucy24 for your replay..

I actually tried to write the code in reverse way also.. but it didn;t work. for example if I am trying to enter my domain name as www.domain.com/ then the reverse code is adding .html at the end like domain.com/.html which is not correct.

I will go through the page you suggested..let me see, if I can figure out something from that link .. :)

mememax

5+ Year Member



 
Msg#: 4393296 posted 9:09 am on Dec 2, 2011 (gmt 0)

Have you tried with Conds?

Maybe eliminating files ending with html or / you can redirect anything else to .html

RewriteCond %{REQUEST_URI} !^(.+)\.html
RewriteCond %{REQUEST_URI} !^(.+)/
RewriteRule ^(.+) [mydomain.com...]

However this will affect only the first level, I don't know if you have got subdirectories.

batu544



 
Msg#: 4393296 posted 9:42 am on Dec 2, 2011 (gmt 0)

Hi Mememax -
I just tried your code but its not working ..

I do have also some other code in my .htaccess file



RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?%{QUERY_STRING}&resource=$1 [L]




Is it creating any issues with redirect ?

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4393296 posted 9:46 am on Dec 2, 2011 (gmt 0)

If you are looking only at the end of the filename, you don't need an opening anchor, but you do need an ending one:

!\.html$

In the Rule itself, if you are capturing the entire request you don't need anchors at all.

:: waiting for g1 to drift by and put in a plug for sticking with the extensionless version, especially since you've already got both floating around ::

mememax

5+ Year Member



 
Msg#: 4393296 posted 9:53 am on Dec 2, 2011 (gmt 0)

Hi Batu, well any code can affect your redirects, for being sure that anything may be affecting the redirect we'll have to see the complete htaccess.

What do you prefer, going to .html or to the extensionless url?

Lucy, you're right, I'm not a real coder, thanks for cleaning my code :-)

mememax

5+ Year Member



 
Msg#: 4393296 posted 10:10 am on Dec 2, 2011 (gmt 0)

However I've tried my redirects in a clean htaccess and they work just fine. I don't know if it's the best way to code this but this works for me...


RewriteCond %{REQUEST_URI} !\.html
RewriteCond %{REQUEST_URI} !(.+)/
RewriteRule ^(.+) http://example.example.com/$1.html [R=301,L]

[edited by: engine at 5:15 pm (utc) on Dec 12, 2011]
[edit reason] url obfuscated [/edit]

batu544



 
Msg#: 4393296 posted 10:22 am on Dec 2, 2011 (gmt 0)

Not sure what's stopping this code on my website.. here is my complete .htaccess file


#php_flag display_errors On
#Options +FollowSymlinks
RewriteEngine Off
RewriteEngine On


RewriteCond %{HTTP_HOST} ^doamin.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?%{QUERY_STRING}&resource=$1 [L]

<Files ~ "\.tpl$">
Order allow,deny
Deny from all
</Files>


Regarding the extensionless url .. I don't see there is any benefit with extensionless urls and few pages of my website are having PR2 and PR3 when there is an extension. Extensionless version of same page has no pagerank.. So, I would prefer not to change my existing style..


If .htaccess trick will not work, then I need to look into the script to add "canonical" link in each page..

If there are any other suggestion, please let me know.

mememax

5+ Year Member



 
Msg#: 4393296 posted 10:31 am on Dec 2, 2011 (gmt 0)

This Rule is obviously giving problems...


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?%{QUERY_STRING}&resource=$1 [L]


Maybe lucy can help more than I... Just one thing, why:


RewriteEngine Off
RewriteEngine On


?

batu544



 
Msg#: 4393296 posted 11:29 am on Dec 2, 2011 (gmt 0)

Hi Lucy - Could you please do something on this ?

@Mememax - I don't know why this off|on is happening .. It's just there .. never tried to remove or change it.. :)

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4393296 posted 12:54 pm on Dec 2, 2011 (gmt 0)

Extensionless is better in the long run.

Which version of the URL does the site itself link to?

The links on the pages of your site must point to the one you want people to use.

The .html redirect should be first. The www redirect comes next. Any internal rewrite comes last.


Test for .html URL requests is
^(([^/]+/)*[^/.]+)\.html?$

Test for extensionless URL requests is
^(([^/]+/)*[^/.]+)$


Don't worry too much about what does and does not have pagerank now. Look at the long term.

mememax

5+ Year Member



 
Msg#: 4393296 posted 1:03 pm on Dec 2, 2011 (gmt 0)

Hi g1smd, can I ask you why do you think that extensionless is better in the long run?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4393296 posted 1:07 pm on Dec 2, 2011 (gmt 0)

No need to ever expose the server technology in the URL.

Also, say your site moves from static .html files to a dynamic .asp site then moves again to an open source .php package. With extensions you've a mess of redirects to install on the first move and then to update and add to with the second move.

With extensionless your URLs are the same for all three versions of the site.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4393296 posted 5:32 pm on Dec 2, 2011 (gmt 0)

Extensionless is better in the long run.

Nice work there, crystal ball ;)

The devil's advocate says:

On the other hand you then have the risks that come with any mass rewrite. You're not just dumping the hotlinkers that you don't care about anyway, you're rewriting your "real" visitors.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4393296 posted 5:55 pm on Dec 2, 2011 (gmt 0)

You'll be redirecting real visitors for a while.

Within a year, link-building will have more links to the new URLs than the old. Pagerank passes through a single-step redirect with only mnor loss.

It's a long term gain.

batu544



 
Msg#: 4393296 posted 6:07 pm on Dec 2, 2011 (gmt 0)

Thanks a lot to g1smd.. !

Your trick worked like a charm.. :)

I understand the point of using extensionless version, but my website is a small one and its already online for more than one year. I don't have any plan to switch the under laying technologies.. :), so, I am okay with extension version of my urls.

No need to ever expose the server technology in the URL.


I don't feel its always true. in my case I am running all php codes to generate my content but on url its just displaying as html file..


Thanks again to all for your valuable time and inputs....

mememax

5+ Year Member



 
Msg#: 4393296 posted 12:34 am on Dec 4, 2011 (gmt 0)

Yes, that's a good point g1smd. Thanks for sharing!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved