Remove html file extension and 301 redirects
-
Hi
Recently I ask for some work done on my website from a company, but I am not sure what they've done is right.
What I wanted was html file extensions to be removed like
/ash-logs.html to /ash-logs
also the index.html to www.timports.co.uk
I have done a crawl diagnostics and have duplicate page content and 32 page title duplicates. This is so doing my head in please helpThis is what is in the .htaccess file
<ifmodule pagespeed_module="">ModPagespeed on
ModPagespeedEnableFilters extend_cache,combine_css, collapse_whitespace,move_css_to_head, remove_comments</ifmodule><ifmodule mod_headers.c="">Header set Connection keep-alive</ifmodule>
<ifmodule mod_rewrite.c="">Options +FollowSymLinks -MultiViews</ifmodule>
DirectoryIndex index.html
RewriteEngine On
#Rewrite valid requests on .html files RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html?rw=1 [L,QSA]
#Return 404 on direct requests against .html files
RewriteCond %{REQUEST_URI} .html$
RewriteCond %{QUERY_STRING} !rw=1 [NC]
RewriteRule ^ - [R=404]AddCharset UTF-8 .html # <filesmatch “.(js|css|html|htm|php|xml|swf|flv|ashx)$”="">#SetOutputFilter DEFLATE #</filesmatch>
<ifmodule mod_expires.c="">ExpiresActive On
ExpiresByType image/gif "access plus 1 years"
ExpiresByType image/jpeg "access plus 1 years"
ExpiresByType image/png "access plus 1 years"
ExpiresByType image/x-icon "access plus 1 years"
ExpiresByType image/jpg "access plus 1 years"
ExpiresByType text/css "access 1 years"
ExpiresByType text/x-javascript "access 1 years"
ExpiresByType application/javascript "access 1 years"
ExpiresByType image/x-icon "access 1 years"</ifmodule><files 403.shtml="">order allow,deny allow from all</files>
redirect 301 /PRODUCTS http://www.timports.co.uk/kiln-dried-logs
redirect 301 /kindling_firewood.html http://www.timports.co.uk/kindling-firewood.html
redirect 301 /about_us.html http://www.timports.co.uk/about-us.html
redirect 301 /log_delivery.html http://www.timports.co.uk/log-delivery.html redirect 301 /oak_boards_delivery.html http://www.timports.co.uk/oak-boards-delivery.html
redirect 301 /un_edged_oak_boards.html http://www.timports.co.uk/un-edged-oak-boards.html
redirect 301 /wholesale_logs.html http://www.timports.co.uk/wholesale-logs.html redirect 301 /privacy_policy.html http://www.timports.co.uk/privacy-policy.html redirect 301 /payment_failed.html http://www.timports.co.uk/payment-failed.html redirect 301 /payment_info.html http://www.timports.co.uk/payment-info.html -
This looks good to me, the html pages are 301ing to the non .html versions.
-
I think I've done it this is what I have found and added to my htaccess code.
<ifmodule mod_rewrite.c="">
Options +FollowSymLinks -MultiViews</ifmodule>DirectoryIndex index.html
RewriteEngine On
RewriteBase /#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]#non www to www
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.html [NC,L]#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.html\ HTTP/
RewriteRule ^index.html$ http://www.timports.co.uk/ [R=301,L]
RewriteCond %{THE_REQUEST} .html
RewriteRule ^(.*).html$ /$1 [R=301,L] -
I still have the internal error, thank you for your time in looking at this I will keep trying
-
Hi,
htaccess can be a pain and I will admit I usually manage what I am after with a bit of trial and error. Try the following, and if you have problems concentrate on the lines:
RewriteCond %{THE_REQUEST} .html
RewriteRule ^(.*).html$ /$1 [R=301,L]I have added a redirect for index.html to root, and from non www to www and removed the last .html from the last list of _ to - redirects. Give it a shot, and keep that backup handy just in case. If no go, maybe one of the htaccess experts around can step in and have a look, I am not 100% sure what some of those other rules are doing to be honest!
<ifmodule pagespeed_module="">ModPagespeed on
ModPagespeedEnableFilters extend_cache,combine_css, collapse_whitespace,move_css_to_head, remove_comments</ifmodule><ifmodule mod_headers.c="">Header set Connection keep-alive</ifmodule>
AddCharset UTF-8 .html
<filesmatch ".(js|css|html|htm|php|xml|swf|flv|ashx)$"="">
#SetOutputFilter DEFLATE
#</filesmatch><ifmodule mod_expires.c="">ExpiresActive On
ExpiresByType image/gif "access plus 1 years"
ExpiresByType image/jpeg "access plus 1 years"
ExpiresByType image/png "access plus 1 years"
ExpiresByType image/x-icon "access plus 1 years"
ExpiresByType image/jpg "access plus 1 years"
ExpiresByType text/css "access 1 years"
ExpiresByType text/x-javascript "access 1 years"
ExpiresByType application/javascript "access 1 years"
ExpiresByType image/x-icon "access 1 years"</ifmodule><files 403.shtml="">order allow,deny allow from all</files>
# mod_rewrite On only needed once
RewriteEngine On301 permanent redirect old underscore.html to new dash urls
redirect 301 /PRODUCTS http://www.timports.co.uk/kiln-dried-logs
redirect 301 /kindling_firewood.html http://www.timports.co.uk/kindling-firewood
redirect 301 /about_us.html http://www.timports.co.uk/about-us
redirect 301 /log_delivery.html http://www.timports.co.uk/log-delivery
redirect 301 /oak_boards_delivery.html http://www.timports.co.uk/oak-boards-delivery
redirect 301 /un_edged_oak_boards.html http://www.timports.co.uk/un-edged-oak-boards
redirect 301 /wholesale_logs.html http://www.timports.co.uk/wholesale-logs
redirect 301 /privacy_policy.html http://www.timports.co.uk/privacy-policy
redirect 301 /payment_failed.html http://www.timports.co.uk/payment-failed
redirect 301 /payment_info.html http://www.timports.co.uk/payment-info301 permanent redirect index.html to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)index.html?\ HTTP/
RewriteRule ^(([^/]+/))index.html?$ http://www.timports.co.uk/$1 [R=301,L]301 permanent redirect non-www to www
RewriteCond %{HTTP_HOST} !^(www.timports.co.uk)?$
RewriteRule (.*) http://www.timports.co.uk/$1 [R=301,L]301 permanent redirect all .html to non .html
RewriteCond %{THE_REQUEST} .html
RewriteRule ^(.*).html$ /$1 [R=301,L] -
thanks Lyn, but that gave an 500 internal error, back up worked though
-
Hi,
I think you will only need this bit:
#301 from example.com/page.html to example.com/page
RewriteCond%{THE_REQUEST}^[A-Z]{3,9}\ /..html\ HTTP/
RewriteRule^(.).html$ /$1 [R=301,L]And you would replace this bit below with the above:
Rewrite valid requests on .html files RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html?rw=1 [L,QSA]
#Return 404 on direct requests against .html files
RewriteCond %{REQUEST_URI} .html$
RewriteCond %{QUERY_STRING} !rw=1 [NC]
RewriteRule ^ - [R=404]But leave the at the end of that section.
htaccess files can be a bit picky, so be sure to keep a backup so you can quickly undo something if it is not working!
-
Ok have got links to work again with old code, going to try this
#example.com/page will display the contents of example.com/page.html RewriteCond%{REQUEST_FILENAME}!-f RewriteCond%{REQUEST_FILENAME}!-d RewriteCond%{REQUEST_FILENAME}.html -f RewriteRule^(.+)$ $1.html [L,QSA] #301 from example.com/page.html to example.com/page RewriteCond%{THE_REQUEST}^[A-Z]{3,9}\ /..html\ HTTP/ RewriteRule^(.).html$ /$1 [R=301,L]
where would I put this code in relation to what I already have in my htaccess file
-
Thanks you for your reply, I have looked at the links you provided and tried replacing this RewriteEngine On #
Rewrite valid requests on .html files RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html?rw=1 [L,QSA]
Return 404 on direct requests against .html files
RewriteCond %{REQUEST_URI} .html$
RewriteCond %{QUERY_STRING} !rw=1 [NC]
RewriteRule ^ - [R=404]with this, but it didn't work or I did something wrong. #example.com/page will display the contents of example.com/page.html RewriteCond%{REQUEST_FILENAME}!-f RewriteCond%{REQUEST_FILENAME}!-d RewriteCond%{REQUEST_FILENAME}.html -f RewriteRule^(.+)$ $1.html [L,QSA] #301 from example.com/page.html to example.com/page RewriteCond%{THE_REQUEST}^[A-Z]{3,9}\ /..html\ HTTP/ RewriteRule^(.).html$ /$1 [R=301,L]
Now www.timports.co.uk says this page cant be displayed so I tried to put it back to the previous .htaccess and still no links working
I am so stuck
-
Hi,
Indeed there seems to be an issue with your redirects since the .html versions are still available on your site. Two things to check in the first instance:
1. The redirect line for the .html to non .html versions:
Rewrite valid requests on .html files RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html?rw=1 [L,QSA]
I am not sure if this will work the way you want it. First of all a # at the beginning of this line means it is a comment and not processed so you seem to have the RewriteCond part of the statement as a comment (maybe this is just the forum formatting it wrong, but good to check).
You can check some other solutions for redirecting .html to non .html here: http://stackoverflow.com/questions/5730092/how-to-remove-html-from-url2. At the bottom of the file you have a bunch of 301 redirects like this:
redirect 301 /kindling_firewood.html http://www.timports.co.uk/kindling-firewood.html
Which are working as expected redirecting underscored urls to urls with dashes. But they are also redirecting to the .html version which means you will be getting into double redirects which is pointless in your case. Once you have the non .html redirects working as expected you should adjust these 301s to go to the non .html version like so:
redirect 301 /kindling_firewood.html http://www.timports.co.uk/kindling-firewood
Hope that helps!
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Delete old blog posts after 301 redirects to new pages?
Hi Moz Community, I've recently created several new pages on my site using much of the same copy from blog posts on the same topics (we did this for design flexibility and a few other reasons). The blogs and pages aren't exactly identical, as the new pages have much more content, but I don't think there's a point to having both and I don't want to have duplicate content, so we've used 301 redirects from the old blog posts to the new pages of the same topic. My question is: can I go ahead and delete the old blog posts? (Or would there be any reasons I shouldn't delete them?) I'm guessing with the 301 redirects, all will be well in the world and I can just delete the old posts, but I wanted to triple check to make sure. Thanks so much for your feedback, I really appreciate it!
Technical SEO | | TaraLP1 -
To 301 redirect or not...
Hi guys i'd like to get your opinion on this. We currently have two sites, site A is the old one with PA44 and DA33. Site B is the new one which is going to replace site A it currently has PA37 and DA24 Our plan for the future is to shut down site A and redirect all pages using 301 to the relevant pages on side B. Currently we have some links in place for a couple of keywords on site A to site B which seems to be working great for our ranking. Now i'm wondering if this is maybe a good option, to give back links from A to B or will i pass through more link juice when redirecting everything? (ps. both are e commerce sites hosted and registred with different companies)
Technical SEO | | Immanuel0 -
301 Redirect / cross-domain canonical to a URL w/ Ampersand
I have a question regarding ampersands, we are needing to redirect to a URL w/ an ampersand in the URL: http://local.sfgate.com/b18915250/Sam-&-Associates-Insurance-Agency Will Google pass page authority/juice despite the fact that there is an ampersand in the URL, if we were to 301 redirect or cross-domain canonical to the url? Should we 301 redirect to http://local.sfgate.com/b18915250/Sam-%26-Associates-Insurance-Agency instead of http://local.sfgate.com/b18915250/Sam-&-Associates-Insurance-Agency? I don't have the option of removing the ampersand Thank you for your time!
Technical SEO | | Gatelist0 -
Correct Redirect method for switching pages from .html to /pretty urls/
I have a customer that has all his site files as .html extensions and i'm going to rebuild this site into a wordpress site for easier management, regarding the new permalink structure, should i just do a 301 redirect on this?
Technical SEO | | tgr0ss0 -
301 redirect or 302
A client of mine has an international company named www.taxglobalizers.com if you come from Holland they 302 redirect you to the www.taxglobalizers.com/en version of the site and if you come from Holland they redirect with a 302 to www.taxglobalizers.com/nl version. I think it is best to change this in a 301 redirect. Am i correct in this? Kind Regards, Ruud
Technical SEO | | RuudHeijnen0 -
Converting files from .html to .php or editing .htaccess file
Good day all, I have a bunch of files that are .html and I want to add some .php to them. It seems my 2 options are Convert .html to .php and 301 redirect or add this line of code to my .htaccess file and keep all files that are .html as .html AddType application/x-httpd-php .html My gut is that the 2nd way is better so as not alter any SEO rankings, but wanted to see if anybody had any experience with this line of code in their .htaccess file as definitely don't wan to mess up my entire site 🙂 Thanks for any help! John
Technical SEO | | JohnHerrigel0 -
Not sure which URL to use for 301 redirect
A client has new website design completed by another developer, was launched in April of this year. No 301 redirect was set up so duplicate content is an issue. Client has had a website with same domain name for about 10 years, but has not had any SEO work completed before or since his new site design. For non-www there are 6 referring links - 1 considered to have authority, for www there are also 6 but 3 considered to have authority. More links seem to coming from www than non-www. But for one of the clients keywords they are ranked #1 for their area and that links to their non-www address. And even though no redirects set up by developer, non-www has had far more visits according to Google Analytics. So many basics that still need to be done for site: no meta-descriptions on any page, H1 and page titles could use keywords, call to action moved above fold, etc. Considering this is a new site, and new SEO work and many more inbound links needed, does it matter which address I redirect to? _Cindy Barnard
Technical SEO | | CeCeBar0 -
301 Redirect Help
Hello! I am getting ready to launch my freshly coded site in the next week or so. My product URLs are changing SLIGHTLY and want to confirm I am going about things the right way: A. My LIVE site store URLs look like http://hiphound.com/shop/dog-collars . My DEV site store URLs look like http://hiphound.com/dog-collars . No /shop directory. B. The dev firm installed the rewrite rule below: ############################################ enable rewrites Options +FollowSymLinks RewriteEngine on #RedirectMatch 301 ^/shop?/$ http://hiphound.com/ RedirectMatch 301 ^/shop?/$ http://hiphound.com ########################################### C. When I manually enter a URL with /shop in the address the website redirects to the correct page which is good. QUESTIONS I HAVE 1. Is the above redirect correct? I need them to permanent. Don't think the above is right... 2. Will links in the Google index be redirected as well? I am assuming yes but just want to confirm. 3. For each page indexed in Google will its pagerank, etc. be passed to the new page using just the 301 above? 4. Do I need to create addtional 301s for each page? So mapping the old page to the new page? Please advise. The goal here is to of course preserve the rankings of the pages already in the Google index. THANK YOU!!! Lynn
Technical SEO | | hiphound0