.htaccess redirect olddomain/folder to newdomain/otherfolder - apache

I use this in my httpd.config and it works:
<VirtualHost *:80>
ServerName olddomain.com
ServerAlias www.olddomain.com
Redirect permanent /FolderName/Filename_with_underscores.html http://newdomain.com/some-folder-with-dashes/?lang=fr
Redirect permanent /FolderName/Other_filename.html http://newdomain.com/some-other-folder/?lang=fr
Redirect permanent / http://newdomain.com/
</Virtualhost>
Now I'd like to put this in the .htaccess file in the root of the website.
I have tried several things but it keeps failing.
Does anyone know how to translate this httpd redirect to .htaccess?
There are capitals, dashes and underscores in the url's...
The code below works (but it's only half the story: it redirects all (www.)olddomain.com to the desired folder at newdomain.com):
RewriteCond %{HTTP_HOST} ^olddomain\.com\$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http://newdomain.com/some-folder-with-dashes/?lang=fr" [R=301,L]
Thnx

You can use in your root .htaccess:
RewriteEngine on
# All rules only for olddomain.com
RewriteCond %{HTTP_HOST} !^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ - [L]
RewriteRule ^FolderName/Filename_with_underscores\.html$ http://newdomain.com/some-folder-with-dashes/?lang=fr [NC,R=301,L]
RewriteRule ^FolderName/Other_filename\.html$ http://newdomain.com/some-other-folder/?lang=fr [NC,R=301,L]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

Related

URL Rewriting http > https AND domain.tld > www.domain.tld

I want to rewrite and redirect my http:\\www.domain.tld to https:\\www.domain.tld
And I want to rewrite and redirect my domain.tld to www.domain.tld
I want to have something that redirect and, with seo concern, shows that's a redirection.
For now I have something like this:
1)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,QSA]
or
2)
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,QSA]
What's the best? Is there something wrong?
Thank you
The Apache docs recommend against using a rewrite: redirect to https
To redirect http URLs to https, you should do the following:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
This snippet should go into main server configuration file, not into .htaccess as asked in the question.
But if you don't have access to the main server configuration file you could use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Line 1: if the url starts without www
line 2: if there is no https
line 3: rewrite all urls from this domain to one that starts with https and have www

Redirect old domain to new domain with htaccess

Need to migrate from example.com to example.net.
The following .htaccess working well to do this.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301]
Additionally, I need the request to the home page of example.com/ to redirect to example.net/home and NOT to example.net/ , as it is done in the above code.
Appreciate suggestions on how to do this...
There's absolutely no need for mod_rewrite if you're doing a simple 1:1 redirect:
<virtualhost ...>
ServerName example.com
Redirect permanent / http://example.net
</virtualhost>
This'll even take care of "sub urls". example.com/foo/bar -> example.net/foo/bar.
You need 2 separate rules:
RewriteEngine On
# home page
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^/?$ http://example.net/home [L,R=301]
# rest of the pages
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.+)$ http://example.net/$1 [L,R=301]

www url rewrite not working (Read All Other Threads)

i want to rewrite
example.com to www.example.com
My Code in apache2.conf (Main config file of apache in Linux/ubuntu 32bit ):
try 1:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Restart Apache Code Doesn't Work !
try 2:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
Restart Apache Code Doesn't Work !
try 3:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Restart Apache Code Doesn't Work !
Loaded Modules in apache displays:
rewrite_module (shared)
How can i solve this ?
And is there any other mod i have to enable(I don't think so).
I have to put this code in some other file ?
It is neccessary/must use .htaccess for url rewriting ?
Your rules are fine.
Make sure the rules are in the virtualhost of the "example.com" domain. If you have no "example.com" vhost, then place the rules in the first vhost container. S
<VirtualHost *:80>
ServerName example.com
....
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
....
</VirtualHost>
Try putting the rules in a directory container:
<Directory "/var/www/path/of/your/document_root/">
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</Directory>

mod_rewrite condition and rule

I've searched for this and Im probably just sitting to close but I cant seem to get this rewrite to work. I have a file on a directory that I want to redirect to a vhost domain on the same server. Below is my condition, rule, and vhost
RewriteCond %{HTTP_HOST} ^/main$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R,l]
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /data/live/main
</VirtualHost>
If you want to redirect the olddomain folder main to the newdomain then you can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^main/(.*)$ http://newdomain.com/$1 [R=301,L]
If you want to redirect all the content of the olddomain to the newdomain then you can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
And if you want to remove the www from your newdomain:
RewriteCond %{HTTP_HOST} ^www\.newdomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Or you can do it directly on the virtualhost like this:
<VirtualHost *:80>
ServerName newdomain.com
Redirect permanent / http://www.newdomain.com
DocumentRoot /data/live/main
</VirtualHost>

.htaccess HTTP to HTTPS AND non-www to www

I currently have some rules which work for directing http://domain.com to https://www.domain.com, but it doesn't work (I guess it's not matching?) for http://www.domain.com, which should redirect to https://www.domain.com. Could someone modify the below to do this? I've tried quite a few things but haven't been successful, this is my first time with .htaccess rewrite rules.
TL;DR, I need to redirect to both WWW and HTTPS
RewriteEngine On
RewriteRule .? - [E=PROTO:http]
RewriteCond %{HTTPS} =on
RewriteRule .? - [E=PROTO:https]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{ENV:PROTO}://www.%{HTTP_HOST}/$1 [R=301,L]
EDIT: I've found the following code, which somewhat works - It redirects both to https://www.domain.com, but it gives a "Too many redirects" error.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ https://www\.%2%{REQUEST_URI} [L,R=301]
Have you tried something like this?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
It would be better to modify this in the VirtualHost for the server.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
Redirect permanent / https://www.domain.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.domain.com
DocumentRoot /www/
SSLEngine On
# etc...
</VirtualHost>
This is the rule I'm using:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
You could include RewriteCond %{HTTPS} !=on as the first condition to force remove https if that's neccesary...
Hope that helps...