rewrite a subdomain to a new subdomain with path - mod-rewrite

given a url
http://subdomain.domain.com/article/
I want a rewrite to
http://sites.domain.com/preview?site=subdomain/article
I have
RewriteEngine on
RewriteCond http://subdomain\.domain\.com* [NC]
RewriteRule ^(.*)$ http://sites.domain.com/preview?site=subdomain$1 [R=301,L]
But its not working

Related

Rewrite example.txt to another based on the domain

Im using a multi-install for my webpage with two different domains and i need for each domain a unique robots.txt
like
https://www.domain1.tdl/robots.txt should use the https://www.domain1.tdl/robots_domain1.txt
and
https://www.domain2.tdl/robots.txt should use the https://www.domain2.tdl/robots_domain2.txt
Check this rewrites in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain1.tdl/robots_domain1.txt [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.tdl [NC]
RewriteCond %{REQUEST_URI} ^\/robots\.txt$
RewriteRule (.*) https://www.domain2.tdl/robots_domain2.txt [L]

Laravel htaccess, redirect url if find /en

My old url
www.example.com/cn/news
www.example.com/cn/event
I want to if user enter /cn url Laravel will redirect into this url
www.example.com/en/news
www.example.com/en/event
here is my laravel .htaccess in root folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I try this one but it doest work
RewriteCond %{THE_REQUEST} /cn [NC]
RewriteRule ^ /%1/en/%2/? [L,R=301]
UPDATE
now .htaccess look like this but still didnt work
RewriteEngine On
RewriteRule ^(.)$ public/$1 [L]
RewriteRule ^cn/(.)$ en/$1 [L,R=301]
RewriteRule ^cn/?(.*)$ en/$1 [L,R=301]
This rule alone should work.
Match a cn prefix with an optional / and capture all characters after the /.

redirect all pages including sub pages to new domain

I tried this:
RedirectMatch 301 (.*) http://olddomain.com$1
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
But all subpages are not redirected.
Try this,
RewriteEngine On
# Take care of www.old.com.au
RewriteCond %{HTTP_HOST} ^www.old.com.au$ [NC]
RewriteRule ^(.*)$ http://www.new.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^attachment_id=([0-9]*)$ [NC]
RewriteRule ^$ http://www.new.com/? [R=301,NE,NC,L]
It's simple, I was just using this to do some special rewriting for my own, here is your code:
Put this inside your /www/.htaccess file:
RewriteEngine on
// Rules to redirect to another domain
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.net/$1 [L,R=301,NC]
Check http://www.inmotionhosting.com/support/website/redirects/setting-up-a-301-permanent-redirect-via-htaccess, for 3 more ways to make a redirection.
Are you setting directive AllowOverride All in your Apache config?
Is the mod_rewrite module working?

Redirect subdomain, but only if site root requested

I can't seem to find an answer to this specific question:
How can I forward http://play.domain.com -- but NOT http://play.domain.com/xxx
To:
http://work.domain.com
Where "xxx" is any string.
In other words, if the user requests the site root, redirect, otherwise stay on the chosen URL.
I've tried
RewriteEngine On
RewriteCond %{HTTP_HOST} !work.domain.com [NC]
RewriteRule ^(.*)$ http://work.domain.com.com/$1 [R=301,L]
But it's not doing anything :-/
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^play\.domain\.com$ [NC]
RewriteRule ^/?$ http://work.domain.com/ [R=301,L]
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} !work.domain.com [NC]
RewriteRule ^$ http://work.domain.com.com/ [R=301,L]
to only redirect the blank request.

double slash apache configuration

i'm deploying a ror application and now i have to rewrite the url (in apache) to
add a prefix www to the url
add / to the end of the url
So i took the following approach:
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com/$1 [R=301,L]
The problem is that it is appending two trailing slash to my url
So for example a resource /question/ask are becoming:
http://foo.com//question/ask
I tried to add the following Rule before all my Rewrite rules to try to remove the double //:
RewriteCond %{REQUEST_URI} ^//
RewriteRule ([^/]*)/+(.*) http://www.foo.com/$1/$2 [R=301,L]
but it didnt work.. any idea to rip off all extras "//" added to the url?
The $1 will include a / at the beginning. You probably want
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com$1 [R=301,L]

Resources