modrewrite - rewrite to new domain from subdirectory - .htaccess

Damn you modrewrite
I have a website hosted at a url like:
http://mydomain/mocks/thesite/
Now I want to move it to a new domain
http://thesitesdomain.com/
My htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule (.*) http://www.thesitesdomain.com/$1 [R=301,L]
Now this works fine as long as there is something after /mocks/thesite/. eg: http://mydomain/mocks/thesite/index.html redirects to http://www.thesitesdomain.com/index.php.
However the problem is that:
http://mydomain/mocks/thesite/ redirects to http://thesitesdomain.com/mocks/thesite/. Any idea why? How to stop this?
The .htaccess file is in the root of /mocks/thesite/ (if that helps)
Thank you

You should try to use the variable REQUEST_URI you might have a little more success with that. It should be the request uri and file name. To
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI} [R=301,L]
I can't remember but to also redirect with the query string (get variables) I think you need to add it like this.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI}?%{QUERY_STRING} [R=301,L]
Been a while since really doing a domain redirect....
BTW this is a good read on htacces configuration:
http://corz.org/serv/tricks/htaccess2.php

Related

RewriteRule to redirect subfolder and parameters to parked domain

This won't work, I don't know why...
# let's say I'm in HTACCESS of domain www.aaa.com
RewriteRule subfolder/script\.php\?param=value&param2=value2 http://www.bbb.ccom/otherscript.php?otherparam=othervalue [QSA,L]
# domain bbb.com is mine, BUT not on the same account (but same server)
It's not causing any bug or loop, it simply plain not work.
Other instructions in the HTACCESS does work (rewriterules among them) so I know the RewriteEngine is on and works.
It simply won't work because RewriteRule doesn't match QUERY_STRING. You will RewriteCond for that. Use code like below:
RewriteCond %{QUERY_STRING} param=value&param2=value2 [NC]
RewriteRule ^subfolder/script\.php$ http://www.bbb.ccom/otherscript.php?otherparam=othervalue [QSA,L,R]

Htaccess redirect without url rewrite

I have a little problem in hands.
I am setting up a domain that as 3 languages, example.com for principal domain, en.(...) for english and de.(...) for germany.
Usually I only redirect the httpdocs from the subdomains to the main with ln -S because all languages executes the same code, the difference is matched from php and mysql.
My new host don't provide any ssh connection so I have to use a different approach on this.
It was told to me that it can be done by .htaccess but I already tried a lot of things but only can redirect, changing the url and that's not possible, it have to keep the same, the contents yes, will be from another domain. Can someone help?
This code should looks like the one you're searching for :
www.domain.en .htaccess
RewriteBase /
RewriteRule ^(.*)$ http://www.domain.com/en/$1 [L,R=301]
www.domain.de .htaccess
RewriteBase /
RewriteRule ^(.*)$ http://www.domain.com/de/$1 [L,R=301]
You'll propably adapt the http://www.domain.com/lang/$1 part to your needs.
EDIT
Following your comment, this is a code for subdomains redirections :
domain.com .htaccess
RewriteCond %{HTTP_HOST} ^en\.domain\.com [NC]
RewriteRule (.*) http://domain.com/en/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^de\.domain\.com [NC]
RewriteRule (.*) http://domain.com/de/$1 [QSA,L]

Redirect all sites of all domain to new domain without trailing query string

I want to redirect all requests on my old domain to the homepage (root site) of my new domain. I thought it would work with a:
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]
(see: .htaccess redirect all pages to new domain) but for a strange reason, when I try for example to access the URL www.olddomain.com/faq/pdf.php?cat=7&id=93&artlang=de I get redirected to www.newdomain.com/?cat=7&id=93&artlang=de. What could be the problem?
In the past I had some problems because this domain is in a subdirectory (as addon-domain) of my new main-domain, but in the .htaccess of the main-domain I've got a:
RewriteCond %{HTTP_HOST} ^([^.]+\.)*olddomain\.com
RewriteRule .* - [L]
Which disable this kind of errors. Any other ideas? I'm trying this now since hours.
EDIT: I've googled when I got that the problem is the query string.
If you want to redirect to your new domain without path and query string, you must append an empty query string
RewriteEngine On
RewriteRule .* http://newdomain.com/? [R=301,L]
mod_rewrite appends query string to the rewritten URL if no query string was specified. Add an empty query string to avoid the problem (adding a ? is sufficient):
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/? [R=301,L]
Also read about the QSA flag.
UPDATED:
RewriteEngine On
RewriteCond %{HTTP_HOST} (?:www\.)?oldomain\.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/? [R=301,L]
Updated as an example, not as an answer.
use rewrite to redirect from an old domain to a new domain that includes the full path and query string:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]

Htaccess redirect for hundreds URL

I have old site with hundreds URL that look like that:
http:www.domain.com/Contact.asp?Pid=344
http:www.domain.com/Contact.asp?Pid=345
http:www.domain.com/Contact.asp?Pid=346
and so on ...
I need to move all of them permanent to 1 single URL :
http:www.domain.com/contact
I tried this:
RewriteCond %{QUERY_STRING} Contact.asp?Pid=([0-999]+)
RewriteRule ^http://www.domain.com/contact? [L,R=301]
But it doesn't work well.
The old site use ASP. The new site build on Joomla. The domain will be the domain from old site
You're close. You need to use the %{QUERY_STRING} variable like you did, but the var doesn't include the URI-path (the Contact.asp?) part. Also, your RewriteRule is missing a regex pattern. Try:
RewriteCond %{QUERY_STRING} Pid=([0-999]+)
RewriteRule ^/?Contact\.asp$ http://www.domain.com/contact? [L,R=301]
RewriteRule ^Contact.asp?Pid=(\d) /contact? [L]
Are the only pages you have on there the contact pages? You could just 301 the entire directory since they are all moving to one URL.
RewriteEngine on
RewriteBase /
RewriteRule ^/(.*)$ http://www.domain.com/$1 [R=301,L]

RewriteRule multilanguage site

Have site with many languages
extra languages are in subdomains on my server like de.mydomain.com, fr.mydomain.com.
in these de,fr subdirectory i've placed only htaccess file with this code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.de\.mydomain\.com$
RewriteRule ^(.*)$ "http://www.mydomain.com/$1?lan=2%1" [L]
And it works but not how I want. it simply redirects from de.mydomain.com to mydomain.com, url in the browser is changing and I would like the url to stay like: de.mydomain.com but the content to be taken from mydomain.com?lan=2.(i use lan variable to change lang)
what do I do wrong here?
maybe my general aproach to this problem is wrong?
Edit :
You need to point de subdomain to the root directory instead, and then adding these lines to the root htaccess file :
RewriteCond %{HTTP_HOST} ^(www\.)?de\.mydomain\.com$
RewriteRule ^(.*)$ /$1?lan=2 [L,QSA]
Put this rule before your rule:
RewriteRule !^(fr|en)/ /en%{REQUEST_URI} [L,R=301]
.htaccess rewrite to default language folder?

Resources