301 redirect all .htm to the root domain - http-status-code-301

I rebuilt a site and would like to redirect all urls ending in .htm (that might be in folders) to the root of the website.
I don't want to change or remove the extension, just make all the old adresses ending in .htm go to the root website www.website.com/
I tried
RedirectPermanent (.*)\.htm$ https://www.mywebsite.com => does nothing
RedirectMatch (.*)\.htm$ http://www.mywebsite.com => "too many redirects"

You can do this with the RedirectMatch rule.
RedirectMatch 302 \.htm$ /single-page

You can also try a mod_rewrite rule
RewriteRule ^(.*)\.htm$ /single-page [R=301,L]

Related

301 Redirect a folder to a single page

I have a folder on my website that has been superceded and I want to redirect an attempt to access any file in this folder to the current home page.
I have read many questions here but none seem to do exactly this.
I have tried various things in .htaccess but it seems to always append the filename to the redirect address.
redirect 301 old/$ www.example.com/index.html
redirect 301 old/ www.example.com/index.html
redirect 301 ^old/ www.example.com/index.html
I have seen various mentions of RewriteRule. Do I need to use that?
You can use this code in your /old/.htaccess file:
RewriteEngine On
RewriteRule ^ http://www.example.com/index.html [L]
OR from root .htaccess use:
RewriteEngine On
RewriteRule ^old(/.*)?$ http://www.example.com/index.html [L,NC]

Redirecting subfolders and keeping the same /forum and url $1 after

I need to redirect 3 subfolders from
https://www.website.com/forums/forum-group/forum/directing-subfolder-information-example
to
https://www.website.com/forum/directing-subfolder-information-example <-- this is what I want
I tried
RedirectMatch 301 forums/forum-group/forum(.*) forum/$1
but it puts /forum/forum
Any ideas on how to do it?
This should be working
RedirectMatch 301 ^forums/forum-group/forum/(.*)$ /forum/$1
If you want to do it using mod_rewrite
RewriteEngine On
RewriteRule ^forums/forum-group/forum/(.*)$ /forum/$1 [R=301,L]

Prevent htaccess 301 redirects on sub-domain

I have 301 redirects which direct /index.html to /site folder and when i created a sub-domain it also redirects the link to /site which causes 404 Not Found
For example: members.mysite.com redirects to members.mysite/site which causes 404 error can I add execption for specific forlder or something without changing the redirect.
.htaccess content
AddType text/x-server-parsed-html .htm .html
RedirectMatch 301 ^/index.html(.*)$ /site$1
You can use mod_rewrite to check for the host:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-main.domain.com$ [NC]
RewriteRule ^index.html(.*)$ /site$1 [R=301,L]
Take a look at the RewriteCond docs for a better description of its functionality. You can tailor the regular expression (^your-main.domain.com$) to fit more along the lines of your needs, like adding a (www\.)? in front to handle an optional "www".

How to do 301 redirects with folders & subfolders?

I need to redirect all the old URLs on my site to new URLs, but am having issues. A given URL and its subfolders need redirecting to the same page.
For example, this is the first one:
redirect 301 /panache/sports-bra/ http://www.newdomain.co.uk/sports-bra.html
This works ok. However there are these size sub-pages which need to redirect to the same location:
redirect 301 /panache/sports-bra/30DD http://www.newdomain.co.uk/sports-bra.html
redirect 301 /panache/sports-bra/30E http://www.newdomain.co.uk/sports-bra.html
redirect 301 /panache/sports-bra/30F http://www.newdomain.co.uk/sports-bra.html
And these don't work, I end up at a location like the following:
http://www.newdomain.co.uk/sports-bra.html30DD
See the way the last part of the path is appended to the url? I'm guessing it's because the second redirect is conflicting with the initial 301 redirect?
I have also tried using this rewrite rule but have had no luck. The site is Magento so I don't know if this has some effect? mod_rewrite is enabled on the server.
RewriteRule ^panache/sports-bra/ http://www.newdomain.co.uk/sports-bra.html [R=301]
Any help mucha ppreciated.
Try this.
RewriteEngine on
RewriteRule ^(panache/sports-bra/.*)$ /sports-bra.html [L,R=301,NC]

301 redirect any page to one specific page

IS there a way with just htaccess 301 redirect to redirect any page on a domain to a specific page on another domain.
eg. I was domain.com/index.html and domain.com/contact.html to both redirect to newsite.com/index.html
But I am wanting to do this without having to list each of the pages specifically.
can my 301 redirect be just something like
301 * http://newsite.com/index.html
or how should it be set up. Unfortunately I don't have access to mod rewrite so I cant use mod rewrite to make it work.
Had an issue similar to this using wordpress and trying to remove all .asp extensions from pages, this worked pasted at the top of my .htaccess file
## 301 Redirects
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)\.asp$ $1? [R=301,NE,NC,L]
Yes, that is possible -- instead of mod_rewrite you need to use mod_alias (which has more chances to be enabled).
This one will redirect everything to index.html on newsite.com/
RedirectMatch 301 ^/(.*)$ http://newsite.com/index.html
This one will redirect everything to the same path but on another domain: (e.g. oldsite.com/meow.php => newsite.com/meow.php)
RedirectMatch 301 ^/(.*)$ http://newsite.com/$1

Resources