Forum Moderators: phranque

Message Too Old, No Replies

.htaccess add .html to url

Change URL extension to .html

         

SaleF

5:09 pm on May 4, 2012 (gmt 0)

10+ Year Member



Hi
I was Looking long for customize my current web site URL link
my web site using wordpress and I'm trying to make it optimized for search engines

this is my current URL look like
http://mysite/post-one

I want it like this
http://mysite/post-one.html


current .htaccess contents

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


<Files 403.shtml>
order allow,deny
allow from all
</Files>

[edited by: SaleF at 6:06 pm (utc) on May 4, 2012]

incrediBILL

5:47 pm on May 4, 2012 (gmt 0)

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



That's backwards.

Everyone is removing the .html for SEO friendliness, not adding it.

SaleF

6:04 pm on May 4, 2012 (gmt 0)

10+ Year Member



^^ but I read many articles about making URL links as .html so search engine recognize it as HTML Page not a dynamic link could changed any time !

g1smd

6:18 pm on May 4, 2012 (gmt 0)

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



The extension matters not one bit, but if you have a way to remove it then take that opportunity and do so.

incrediBILL

6:35 pm on May 4, 2012 (gmt 0)

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



As a matter of fact, I just stripped off all the .html from one of my sites today!

Sgt_Kickaxe

2:58 pm on Jun 23, 2012 (gmt 0)



Given that this page is found in Google for people who are trying to add it, and I happen to be doing just that right now, how would you add .html to any article within wordpress?

Simply changing the permalinks structure to /%postname%.html doesn't fully work. While it does add .html to posts it does not redirect the old versions to the new. Using /%postname%-%post_id%.html however DOES change all urls and does redirect old to new. Why is /%postname%.html not working on redirecting old to new?

Also, Bill, one reason you may want the .html extension with wordpress is that you can save a page as a static .html file and drop it into the root folder to completely bypass wordpress and, more importantly, the database. The database connection is often a cause for high TTFB(time to first byte) times, especially on shared hosts. Without the .html extension in permalinks the static file isn't found from the root folder because wordpress takes over.

Sgt_Kickaxe

4:37 pm on Jun 23, 2012 (gmt 0)



I'm finding that this is a wordpress weakness.

if your old permalink was /%postname% and you want your new permalink to be /%postname%.html there is technically no change that wordpress needs to process(301). That means it doesn't attempt to redirect anything. If your old permalink was /%postname%-%post_id% and you want /%postname%.html then wordpress WILL redirect /%postname% to /%postname%.html and /%postname%-%post_id% to /%postname%.html

An htaccess solution is needed to add .html since wordpress wasn't designed to apply redirects if nothing else changes besides adding .html

g1smd

4:45 pm on Jun 23, 2012 (gmt 0)

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



That's one line of code and it goes before the non-www to www redirect which is before the wordpress internal rewrite code.

Sgt_Kickaxe

5:47 pm on Jun 23, 2012 (gmt 0)



That's one line of code and it goes before the non-www to www redirect which is before the wordpress internal rewrite code.


It's going to need to make sure that .html isn't added to the root, after a trailing slash and to urls that already have an extension.

You mentioned the following in a thread six months ago
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 ^(([^/]+/)*[^/.]+)$


But I'm not sure exactly how to fit that into my htaccess, and definitely not in one line (htaccess is not my strong suit)

After looking over the apache documents where turning on multiviews seems to be a strong suggestion I dug 6 pages deep for various searches on WW and in Google/Bing to find a repeating problem with this question. E.G. the argument that you should not add .html for SEO and other reasons dominates most threads and many end without solution, others with complicated solutions that cause other problems.

Over on the wordpress forum there are suggestions to use plugins and a lot of confusion in differentiating between posts and pages.

Anyway, this thread is now #1 on WW when looking for help in adding .html to urls via .htaccess. Can you share the one line of code please? It would be much appreciated.

P.S. Wordpress team, if you read this, please consider having wordpress detect a missing .html and redirect accordingly if it is indicated by permalinks since you redirect for other missing words in a url?

g1smd

6:51 pm on Jun 23, 2012 (gmt 0)

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



Redirect adds .html extension (not recommended) to extensionless URL requests:

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


This will not add the extension to folder URLs or to file requests already with extensions.

g1smd

8:04 am on Jun 25, 2012 (gmt 0)

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



See also: [webmasterworld.com...]