Trying to redirect blog.domain.com to www.domain.com/blog/ - apache

I'm combining a main site at www.domain.com, and an old wordpress blog at blog.domain.com, into one completely new Wordpress install. I have exported and imported all the old blog posts so that they now live under wwww.domain.com/blog//
I am trying to create one rewrite rule that will map all the old blog posts to their new URLS.
I've tried variations on these SO discussions:
DNS/.htaccess files to redirect subdomain to specific folder
Apache rewrite rule different if capture is empty
but nothing is working.
The following in my .htaccess file will redirect blog.domain.com to www.domain.com/blog, if there is nothing more in the URL:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^/?$ http://www.domain.com/blog/$1 [R=301,L]
but if there is anything more in the URL, it does not rewrite the URL at all, and goes to the new Wordpress site's 404 page.
I tried adding a capture to the final RewriteRule line, but then no rewrite occurs, and it just goes to the new homepage but the address bar still reads "blog.domain.com":
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^/(.+)$ http://www.domain.com/blog/$1 [R=301,L]
Is there a way to do what I'm trying to do?

You want to remove the leading slash:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/blog/$1 [R=301,L]
# no slash---^
And make the + a *. URI's that are sent through rules in htaccess files have the leading slash stripped off, so ^/(.+)$ would never match.

Replace your rule to this:
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com/blog%{REQUEST_URI} [R=301,L]
PS: Make sure this is the 1st rule in your .htaccess.

Related

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Redirection through .htaccess not working

I have a domain which has sub-domains (add-on). I am trying to redirect users from main (old) domain to one of sub-domains (new).
First I tried to redirect everything that user types within the OLD domain except one page and one directory... Now this EXCEPT feature is going out of control. I have been trying and implementing all possible options but some it gives an error and stops redirection.
Here are the lines from .htaccess file which I am trying:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} oldrootdomain.com [NC]
RewriteRule ^/view_card.php(.*)$ http://newsubdomain.com/view_card.php$1
RewriteRule ^/m/(.*)$ http://oldrootdomain.com/m/$1
RewriteRule ^(.*)$ http://newsubdomain.com/ [R=301,L]
It seems you run into an inifinite loop.
You DON'T want to redirect for those exception. So try some additional RewriteConds
RewriteCond %{HTTP_HOST} oldrootdomain.com [NC]
RewriteCond %{REQUEST_URI} !view_card.php$
RewriteCond %{REQUEST_URI} !m/.*$
RewriteCond %{REQUEST_URI} !m$
RewriteRule ^(.*)$ http://newsubdomain.com/ [R=301,L]
Also you seem to have not understood the RewriteCond/RewriteRule thing entirely
Any successive stream of RewriteConds does only affect the directly following RewriteRule

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

Redirecting old domian to new domain by .htaccess

I am in process of migrating my old domain to new domain using Apache Mod-Rewrite moduled and its .htaccess file.
we have almost same structure of the new domain which includes
URL's
Database
except the domain name, like it was www.oldurl.com and now its like www.newurl.com and this is what i have in my .htaccess file of Old domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Above settings seems to be working fine except in one case, we have few URL's in my old domain which has either been removed or structure has been changed so in that case above rule will not work.i came to know about adding something like this in my .htaccess file beside what i have described above
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Redirect 301 /my-page http://www.newurl.com/your-page
i have total of 20+ such URL's and i am wondering do i need to map those all 20+ URL to there new URL's and will i need to take care about any order in which they should be put in the file.
i am also wondering how Apache will work, will it look at each mapped URL for any match? or it works in some other way?
The Redirect directive won't be bound to the RewriteCond conditions and will always redirect /my-page to http://www.newurl.com/your-page, also, mod_rewrite has precedence over mod_alias so the RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L] rule gets applied before the Redirect directive gets looked at. However, if the .htaccess file sits within the document root of both the oldurl.com and newurl.com domains, the Redirect directive will be applied after the browser gets redirected to http://www.newurl.com/my-page, thus redirecting (again) to http://www.newurl.com/your-page
So, it doesn't matter what order you have these in, since mod_rewrite gets applied first. If you have 20 URLs that need to redirect to new ones on your new site, you can enumerate them each in their own Redirect. Otherwise, if you'd rather not have the browser get redirected twice, you can enumerate them using the mod_rewrite engine:
RewriteEngine On
# redirect the changed URLs individually
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page$ http://www.newurl.com/your-page [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page2$ http://www.newurl.com/your-page2 [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page3$ http://www.newurl.com/your-page3 [R=301,L]
# Finally, redirect everything else as-is
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Note that order does matter here. Having to repeat the 2 Conditions for HTTP_HOST is kind of ugly, you can maybe get around that by using the SKIP, but it's probably better to just repeat them. But if you have access to your server config or vhost config, take a look at the RewriteMap Directive, which allows you to create a mapping of, in your case, old urls to new urls and you can reduce all the individual changed url rewrites to a single one:
Inside your server/vhost config, something like this:
RewriteMap newurls txt:/path/to/file/map.txt
Where the /path/to/file/map.txt will look something like:
my-page your-page
my-page2 your-page2
my-page3 your-page3
etc...
And your combined rules would look like:
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^(.*)$ http://www.newurl.com/${newurls:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]

RewriteRule help

I have a URL
http://test.devsite-1.com/test/tbox/
which I want to redirect to
http://tbox.devsite-1.com/
Rule:
RewriteCond %{HTTP_HOST} !^tbox\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.|)(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/tbox(/.*|)$
RewriteRule /tbox/(.*) http://tbox.%{HTTP_HOST}/$1 [R=301,L]
I don't understand why it is not redirecting me to the URL? Please note I need a generalized rule so that if I change test.devsite-1.com to tempo.devsite-1.com the same should work with the other URL as well.
Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.(.+)$ [NC]
RewriteRule ^test/tbox/(.*)$ http://tbox.%1/$1 [R=301,L]
This will redirect (301 Permanent Redirect)
http://test.devsite-1.com/test/tbox/something-optional
to
http://tbox.devsite-1.com/something-optional
It has to be placed in .htaccess file in website root folder (e.g. http://test.devsite-1.com/.htaccess). If placed elsewhere some tweaking may be required.
It will only work if request is coming via test. subdomain.
And it will only work if URL requested starts with test/tbox/.
All of the above matches your URL examples.