Rewriting URL for subdomain redirection - apache

I need to pass through a list of pages to show them coming from subdomain
When a user enters this url in the browser
xyz.mydomain.com/
it should be passed though as
www.mydomain.com/level1/pageA?subdomain=xyz
and
xyz.mydomain.com/innerpage_abc?param1=123
as
www.mydomain.com/level1/innerpage?dynamicparam=abc&param1=123&subdomain=xyz

Enable mod_rewrite and mod_proxy. Then put this code in .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(xyz)\.mydomain\.com$ [NC]
RewriteRule ^$ http://www.mydomain.com/level1/pageA?subdomain=%1 [L,P,QSA]
RewriteCond %{HTTP_HOST} ^(xyz)\.mydomain\.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)param1=([^&]+) [NC]
RewriteRule ^([^_]+)_([^/]+)/?$ http://www.mydomain.com/level1/$1?dynamicparam=$2&subdomain=%1 [NC,L,P,QSA]

Related

Change domain and bring users to the same page as the new domain

I changed the tld of my domain from .net to .it.
But I don't want to lose content indexing, and I want to set up a 301 redirect for everyone.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteRule ^(.*)$ https://domain.it [R=301,L]
</IfModule>
This works great but I have a problem: if I'm on the domain.net/rev/1-test page he takes me to domain.it and not in domain .it/rev/1-test
how can i make it take me to the new domain but remain on the user's page?
with $1 I don't say to the htaccess to insert everything that is inside to (.*)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.net$
RewriteRule (.*)$ http://www.domain.it/$1 [R=301,L]
</IfModule>

htaccess redirect for load a specific image url at subdomain from main domain

I have a subdomain (us.example.com) and main domain (example.com). I want to redirect my subdomain url to main domain only for a specific image. For example if I hit http://us.example.com/images/test.jpg then it should redirect to http://example.com/images/test.jpg
I am trying 301 redirect as below but it's not working.
Redirect 301 https://us.example.com/images/test.jpg https://example.com/images/test.jpg
I have tried other rules also but they all work on complete image directory not for a specific image under directory.
Update
Changed rule like this -
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]
But no luck.
The Redirect directive from mod_alias has the following syntax:
Redirect [status] [URL-path] URL
and, as you see, it receives/accepts the URL path, and not the whole URI/URL for matching.
For the subdomain match, use an <if> directive:
<If "%{HTTP_HOST} == 'us.example.com'">
RedirectPermanent /imges/test.jpg https://example.com/images/test.jpg
</If>
The same can be achieved using mod_rewrite as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]
Try this mate ,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us.domain.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
</IfModule>
What will happen for the above .htaccess was ,
If you visit
us.domain.com/somelink
It will redirect to
domain.com/somelink
EDIT :
Suppose if the above doesn't help try the one below with symlinks
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^us.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
Update 1 :
For only test.jpg
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us.domain.com/images/test.jpg [NC]
RewriteRule (.*) http://www.domain.com/images/test.jpg [R=301,L]
</IfModule>

Do redirect and add variable to URL through htaccess

I would like to have htaccess redirect oranges.info to
example.com/about.html?=fruit
This is my htaccess file which unfortunately is only doing the redirect and not adding the variable. Any ideas?
Options +FollowSymLinks
rewriteengine on
rewritecond %{HTTP_HOST} ^www.oranges.info$ [OR]
rewritecond %{HTTP_HOST} ^oranges.info$
rewriterule ^domainfolder\/(.*)$ "http\:\/\/example\.com\/about\.html$1?fruit" [R=301,QSA,L]
Options -Indexes
Your rule syntax isn't correct:
rewritecond %{HTTP_HOST} ^(www\.)?oranges\.info$ [NC]
rewriterule ^ http://example.com/about.html?fruit [R=301,QSA,L]
Make sure to test in a different browser.

Redirecting from subdomain without changing URL

I want to redirect a subdomain with the .htaccess file without changing the URL. But can't figure it out.
ex. i want to turn
http://foo.domain.com
into
http://www.domain.com/users/foo/dashboard.php
I generated a script to do this, but the URL doesn't stay on http://foo.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ http://www.domain.com/users/%1/dashboard.php [L]
Thanks for the help!
What you're htaccess it is doing is simply saying "if you find this, do a full redirect to that url"
Do this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ dashboard.php?domain=%1 [L, QSA]
And it should work (moving to wherever dashboard is in your directory structure on-server, relatively)
Try this (assuming that all the subdomains point to the root directory) :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^.*$ users/%1/dashboard.php [L]
Proxy flag [P] will do the trick:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.org$ [NC]
RewriteRule http://example.org/users/%1/dashboard.php [P,NC,QSA]
</IfModule>

generic non-www to www, and non-http to https

I have the following code for my .htaccess file that I've picked up from here and tried adapting it as I understand from .htaccess, yet I can't seem to get it to work (or maybe the browser has cached it but I can't seem to clear it).
Options -Indexes
Options +FollowSymlinks
<IfModule mod_rewrite.c>
########## FORCE SSL ##########
RewriteEngine On
RewriteBase /
# Non-secure requests to www.domain.com should redirect to https://www.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ https://www\.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# All secure (HTTPS) traffic should redirect to https://www.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ https://www\.%{HTTP_HOST}/$1 [L,R=301]
</IfModule>
I want to make it as generic a possible so I can simply copy and paste it to any site I make so no need to edit it each time - I guess I could also do this in PHP but I think it would be good if .htaccess is also there.
Another point is, can .htaccess be read if i were to go to www.domain.com/.htaccess or do I need to cover that in a 'deny all' kind of thing?
The second argument to the RewriteCond must be a regex, so it cannot contain a variable.
Try adding the following to your .htaccess file in place of the rules you had
#capture top level domain (.com or .co.uk)
RewriteCond %{HTTP_HOST} ([-_a-zA-Z0-9]+\.([a-zA-Z]{2,5}|co\.uk))$ [NC]
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ - [E=MY_TLD:%2]
# Non-secure requests to www.domain.com should redirect to https://www.domain.com
RewriteCond %{HTTPS} off
RewriteRule ^ https://www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301]
# All secure (HTTPS) traffic should redirect to https://www.domain.com
RewriteCond %{HTTPS} on
#if host does not start with www
RewriteCond %{HTTP_HOST} !^www\.[-_a-zA-Z0-9]+\.([a-zA-Z]{2,5}|co\.uk)$ [NC]
RewriteRule ^ https://www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301]
Shortest version would be:
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]
Only (somewhat) downside would be that subdomain.example.com gets redirected to www.subdomain.example.com