I need to permanently redirect part of a website to a different domain and URL structure.
Everything that matches:
example.com/year/month/day/page-name/
and
www.example.com/year/month/day/page-name/
Needs to redirect to:
www.sample.com/sub/sub2/year/month/page-name/
For example:
example.com/2013/11/09/a-great-page/
should permanently point to:
www.sample.com/sub/sub2/2013/11/a-great-page/
But I don't want to redirect any other pages on the site:
example.com/a-random-page
Shouldn't redirect anywhere.
My htaccess chops and regex skills are a little rusty, so any help would be greatly appreciated!
Enable mod_rewrite and .htaccess through httpd.conf (if not already enabled) and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{SERVER_PORT}s ^(443(s)|\d+s)$
RewriteRule ^([0-9]{4}/[0-9]{2}/[0-9]{2}/[^/]+/?)$ http%2://sample.com/sub/sub2/$1 [L,QSA,R=301]
Related
Having checked other questions and trying the suggested solutions, nothing has worked so far.
I'm trying to redirect certain URLs from the old-domain to URLs on the new-domain, not necessarily with the same page names. The rest of the URLs should be redirected to the root of the new-domain. This is what I've tried. The redirecting of all pages to the root of the new-domain works, just not the individual pages:
RewriteEngine on
Redirect 301 /travel/ferry.html http://www.new-domain.com/ferry/
RewriteEngine off
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com/? [R=301,L]
Thank you.
Don't mix Redirect directive and RewriteRule directives as they come from different Apache modules and their order of execution might be unpredictable.
You may have your rules as this:
RewriteEngine on
# keep specific redirect here
RewriteRule ^travel/ferry\.html$ http://www.new-domain.com/ferry/ [L,NC,R=301]
# redirect rest of the URLs to root
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com/? [R=301,L]
Make sure to test it in a new browser or test after fully clearing browser cache.
I have a website incorrectly indexed on Google, like this:
www.mysite.fr/de-DE/mypage
I need to fix it, with a 301 redirect to www.mysite.de/de-DE/mypage
To put it simple, the first level domain (.it, .de, fr...) should correspond to the second url parameter (it-IT, de-DE, fr-FR...).
How can I do it using .htaccess only?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI}::%{HTTP_HOST} ^/([a-z]{2})-.*?::(?:www\.)?mysite\.\1$ [NC]
RewriteRule . http://www.mysite.%1%{REQUEST_URI} [R=302,L,NE]
I have the following pages after enabling pretty urls in my CMS:
www.mydomain.com/cars/
www.mydomain.com/cars/carshome/
www.mydomain.com/cars/honda/
www.mydomain.com/cars/toyota/
...
I want all requests to www.mydomain.com/cars/ to go to www.mydomain.com/cars/carshome/. Thus I've been trying to use in Apache .htaccess file (after pretty urls code executes):
Redirect 301 /cars https://www.mydomain.com/cars/carshome/
and the result is it goes in infinite loop:
(https://www.mydomain.com/cars/carshome/carshome/carshome/...).
How to resolve?
A second question would be, assuming the solution above needs to be changed here: how to redirect the other direction (from www.mydomain.com/trucks/truckshome/ to www.mydomain.com/trucks/)?
Instead of Redirect you can use RedirectMatch directive for its regex capability:
RedirectMatch 301 ^/cars/?$ https://www.mydomain.com/cars/carshome/
This should work for you:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/cars/carshome/(.*)
RewriteRule ^cars/(.*)$ /cars/carshome/$1 [R=301,L]
The above will check to see if the url contains cars/carshome/ and if it does, then nothing happens, but if it doesn't then it will redirect.
As for the second question, this should work:
RewriteCond %{REQUEST_URI} ^/trucks/truckshome/(.*)
RewriteRule ^trucks/truckshome/(.*)$ /trucks/$1 [R=301,L]
I am releasing an updated version of one of my websites soon on a new domain name and I was wondering how I can make it so that the old urls point to my new website as it has quite a lot of valuable backlinks.
I have read about how to make it 301 redirect in .htaccess but I need something a bit more advanced as some of my url structures have changed. See the examples below.
olddomain.com
newdomain.com
- These will do just fine with the 301 redirect
olddomain.com/author/username
newdomain.com/user/username
- Different URL structure but I want the old urls to redirect towards the new url
olddomain.com/add
newdomain.com/submit
- Different page name for the same functionaly, would like to have these redirect correctly as well
I have no clue how to go about creating these last two redirects so if anyone could point me into the right direction that would be great! :)
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^$ http://newdomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^author/([^/]+)/?$ http://newdomain.com/user/$1 [R=301,L]
RewriteCond %{HTTP_HOST} olddomain\.com [NC]
RewriteRule ^add/?$ http://newdomain.com/submit [R=301,L]
Have you tried using RedirectMatch?
In the htaccess file in your olddomain.com document root (it must be different from your newdomain.com root):
RedirectMatch 301 ^/author/(.*)$ http://newdomain.com/user/$1
RedirectMatch 301 ^/add(.*)$ http://newdomain.com/submit$1
Redirect 301 / http://newdomain.com/
I need when an specific page is called with a subdomain it redirects to www.
I can give you an example:
nuevoleon.domain.com/empresa/
I need that the correct url for SEO purposes is:
www.domain.com/empresa
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^nuevoleon\.domain\.com$
RewriteRule ^ empresa(/|$) http://www.domain.com%{REQUEST_URI} [L,R=301]