URL Rewriting after redirecting - .htaccess

I've managed to have .htaccess redirect some of my domains to subdomains. Now i'd like to change the title of the urls. Or to put it differently, change how the url appears on screen in the url input of the browser, but i just can't seem to get it to work. I must've overlooked something.
I'd like it to be so that it won't show http://subdomain.domain.com/, but just http://www.subdomain.com/. And I do own the necessary domains.
My current code:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^(www\.)?sub1\.com$ [NC]
RewriteRule ^ http://sub1.domain.com [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?sub2\.com$ [NC]
RewriteRule ^ http://sub2.domain.com [R,L]
Any suggestions?

Try this
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com$
RewriteRule ^/(.*) http://sub1.com/$1 [redirect,last]
RewriteCond %{HTTP_HOST} ^sub2\.domain\.com$
RewriteRule ^/(.*) http://sub2.com/$1 [redirect,last]

Related

redirect http/https subdomain (mail) to https main domain in .httaccess

I know my question is asked many times but unfortunately I could't find my answer. Maybe because I don't put the .htaccess file in the correct place.
What I want is to redirect http://mail.example.com , https://mail.example.com to https://example.com.
I also wanted to redirect https://www.example.com , http://example.com, http://www.example.com to https://example.com. I did this job with adding below code in .htaccees file in public_html in cpanel and it works.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
I used below code from this link and put .htaccess file in mail folder. but when I type http://mail.example.com it redirects to https://mail.example.com and https://mail.example.com also doesn't go to http://example.com. Maybe the problem is with https.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mail\.mysit\.com [NC]
RewriteRule ^(.*) https://example.com/$1 [L,R=301]
Any help is appreciated.
p.s. My site is designed with joomla and there is no redirection in cpanel and joomla settings.
I think everything is correct, and that 'it's only a matter of order:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mail\.example\.com [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
In you main .htaccess file

.htaccess redirect not directing to specified file

I have an .htaccess file on an old domain of mine that includes redirects that aren't redirecting as I am wanting.
If I type in olddomain.com/about-us.html the redirect is sending the user to newdomain.com/about-us.html. What I am trying to do is direct the user to newdomain.com/about.
Below is my .htaccess file with one example of the redirect.
Does anyone see what I am doing wrong?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/administrator [NC]
RewriteRule /about-us.html https://newdomain.com/about [L,R=301]
Redirect 301 /about-us.html https://newdomain.com/about
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [L,R=301,NE]
Network Tab:
Have it like this:
RewriteEngine On
RewriteRule ^about-us\.html$ https://newdomain.com/about [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/administrator [NC]
RewriteRule ^ https://newdomain.com%{REQUEST_URI} [L,R=301,NE]
Make sure to test it in a new browser or completely clear browser cache.
Make sure there is no other code in your .htaccess when you test

Rewrite www.domainnamenumberone.com/wordpress/path/ to domaintwo.com/wordpress/path

I have two domain names for the same website, and need to do a correct rewrite, so that whenever someone accesses the first domain name and all subdirectories, with, or without www. they get redirected to the second domain and subdirectories without www.
I managed to set the redirect for the domain name without subdirectories, but for whatever reason, subdirectories are not getting rewritten.
So when I go to domainnamenumberone.com, or www.domainnamenumberone.com, i get redirected to domaintwo.com – however, when I go to domainnamenumberone.com/wordpress/path or www.domainnamenumberone.com/wordpress/path I remain there, and nothing gets rewritten.
Here's what I placed in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
Would be grateful for your help!
You need to place this rule as very first rule in DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainnumberone\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\s [NC]
RewriteRule ^ http://domaintwo.com/%1 [L,R=302,NE]
Then add this line in each child .htaccess like wordpress/ or wordpress/path/ (wherever .htaccess already exists) below RewriteEngine On line
RewriteOptions Inherit
You can use that:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
But it seems to me that it should also work with yours.....
try a different browser (cache problem)

htaccess rewriting specific folder/path to subdomain

I'm trying to setup a redirect for a site.
I want rewrite file requests from a folder to a subdomain url.
At the moment, I have this in my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} =GET
RewriteCond %{HTTP_HOST} !^example.com
RewriteCond %{HTTP_HOST} ^g.example.com
RewriteRule ^(uploads/(.*)+ uploads/%1%{REQUEST_URI} [L]
The idea is that when a visitor requests:
http://g.example.com/uploads/somefile.png
they are served:
http://example.com/uploads/somefile.png
So it's working as expected. However the problem is that:
http://b.example.com/uploads/somefile.png
also works.
I only want http://g.example.com/ to be rewritten. http://b.example.com/ should 404.
This should work:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^g.example.com$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.(JPG|GIF|PNG) [NC]
RewriteRule ^ - [R=404,L]
RewriteCond %{HTTP_HOST} ^g.example.com$ [NC]
RewriteCond %{THE_REQUEST} !^GET [NC]
RewriteRule ^ - [R=404,L]
The first rule will make sure that any URL ending with JPG, GIF, PNG that does not come from g.example.com send a 404 not found request to the user.
The second rule makes sure that g.example.com are only accessed using GET requests.

301 redirection - front page to subdirectory

I needed to redirect my od domain to a new one. All paths are same on both domains except the front page, which needed to be redirected from www.mydomain.com to www.mydomain2.com/newpath. I googled and came up with this code which works. My question is if it is valid and if all pageranks will be transfered without problems. Thank you
RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.domain1.com/folder/ [L,R=301]
RewriteRule ^(.*)$ http://www.domain1.com/$1 [L,R=301]
Your code should work but it can be fine tuned a bit. Please consider this refactored code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
Rewriterule ^$ http://www.domain1.com/folder/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://www.domain1.com%{REQUEST_URI} [L,R=301]

Resources