301 redirect everything to new root? - .htaccess

I am trying to do a 301 redirect of everything from an old subdomain to a new.
I have a simple .htaccess
Redirect 301 / http://www.smartphonesoft.com/
However if I goto the old URL with a subdir, it tries to redirect to the new domain with a subdir and fails.
ie
http://forum.smartphonesoft.com/reminder/
goes to
http://www.smartphonesoft.com/reminder/
When I would like it to goto
http://www.smartphonesoft.com/
How can I have everything simply redirected to the new domain root?

With Redirect you define the base path (path prefix) that is to be redirected; every path beyond that is redirected while just replacing the base path with the new base path.
If you want to stick with mod_alias, you can use RedirectMatch and omit the match:
RedirectMatch 301 ^/ http://www.smartphonesoft.com/

Assuming your server has support for mod_rewrite, you can do this:
RewriteRule . http://www.smartphonesoft.com/ [R=301,L]
Alternatively, sticking to mod_alias, this should also work (but I haven't tried it):
RedirectMatch 301 .* http://www.smartphonesoft.com/

Related

htaccess, redirect to specific URL

I created a new website for a club. It has a new Domain as well as different directory structure. The important pages are correctly redirected, but i want to add a catch all on the old domain to the root of the new domain. I already tried various things, currently (on the old domain and host):
RedirectMatch 301 ^/?$ https://newdomain.com/
but whatever I try, this
olddomain.com/sub1/page1
is always redirecting to
newdomain.com/sub1/page1
How do I make a rule, catching all pages and redirecting to a specific URL without attaching the path to the target?
Thanks!
The pattern ^/?$ only matches / the homepage of your olddomain i.e. http://olddomain.com/ . You need to repace your current pattern with ^/sub1/page1 so that your http://olddomain.com/sub1/page1 can redirect to http://newdomain.com .
Change
RedirectMatch 301 ^/?$ https://newdomain.com/
To
RedirectMatch 301 ^/sub1/page1$ https://newdomain.com/
If /page1 is a dynamic value you can replace it with .+ .

Friendly URL or url-rewriting

I am working on a site that uses such a url
www.domain.com/hotels/hotel-name
I would like visitors just to see
www.domain.com/hotel-name
This can probably be done in the .htaccess file with a rewrite condition but I don't know how.
Thank you for helping
You can use RedirectMatch directive :
Put the following Redirect above other rules in your htaccess file
RedirectMatch 301 ^/hotels/([^/]+)/?$ /$1
This example would redirect all files from the /hotels/ folder, if you want to redirect a particular file from that folder , you can use the following:
RedirectMatch 301 ^/hotels/(file_name)/?$ /$1
Now if a visiter enters www.example.com/hotels/hotel-name then will be externally redirected to www.example.com/hotel-name

Remove trailing slash from URL/folder but do not redirect subfolders

I'm using mod_rewrite. The .htaccess contains this line:
# redirect old references
Redirect 301 /folder/ http://www.donain.de/folder
This works fine execpt for one thing: All requests to subfolders are redirected as well. For example: /folder/hello is redirected to /folder. How can I prevent this?
I know that I can determine the start of an expression using ^ in a .htaccess file. But I can't figure out to say "stop this rule there".
You need to use RedirectMatch for regex support:
RedirectMatch 301 ^/folder/?$ http://www.donain.de/folder

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]

Redirect all but some to new URL?

I am moving site and I want to do a 301 redirect on all but some of the URLs,
Like this:
oldsite.com/* -> www.newsite.com
oldsite.com/specific/article/to/redirect -> www.newsite.com/fancy/blah
So there are a few things I want to redirect to specific pages but all the rest should just goto root, how can this be done in .htaccess?
Add the following to the .htaccess file in the root directory of your old site.
#place your specific redirects first
Redirect 301 /specific/article/to/redirect http://www.newsite.com/fancy/blah
RewriteEngine on
#then your general redirect all to new site last
RewriteRule ^ http://www.newsite.com%{REQUEST_URI} [L,R=301]
There's RedirectMatch if you've got only one URL that needs to be exempted.
RedirectMatch permanent !/specific/article/to/redirect http://www.newsite.com
for multiple URLs, you'd probably be better off with mod_rewrite and an external rewritemap that lists the urls to be exempted.

Resources