Apache rewrite sub2.domain.com/sub1/sub2 to sub2.domain.com - apache

How to rewrite so that the subfolders go away in the browser URL?
We have a new subdomain that is already in DNS going to a subfolder:
Entering sub2.domain.com changes the url in the browser to sub2.domain.com/sub1/sub2.
RewriteCond %{HTTP_HOST} ^sub2\.domain.com$
RewriteRule ^(/)?$ sub1/sub2 [L]
I thought it's supposed to redirect first then rewrite? But this doesn't work:
RedirectMatch 301 ^/sub1/sub2/.*$ http://sub2.domain.com/
RewriteCond %{HTTP_HOST} ^sub2\.domain.com/sub1/sub2$
RewriteRule ^(.*)$ http://sub2\.domain\.com [L]

Related

how to 301 redirect from root domain to a page with htaccess

i am trying to auto 301 redirect from domain.com to domain.com/v2
ive tried this code
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^$ /v2/ [NC,L]
also tried
Redirect 301 / /v2/
i get the error 'site has tried to redirect you too many times'
is there any way to do this?
thanks for any help
Untested but this should redirect both domain.com and www.domain.com to the v2 subfolder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
You need to replace example.com with your actual domain because of posting restrictions.
I suppose you have an endless redirect because you are not matching the end of the domain string with $. Redirection should only take place at the root domain.
you can use a write rule with ^$ representing the root of the site and rewrite it to the selected folder
RedirectMatch ^/$ /v2/
it redirects the root and only the root URL.
or also you can use
RewriteEngine On
RewriteRule ^$ /v2[L]
If you want external redirection(301) you can use R flag
RewriteRule ^$ /v2[L,R=301]
Another way to use it with RewriteEngine
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
Original Answers
I hope I've helped

Redirecting subdomain to subdirectory through htaccess

I've been trying to redirect a subdomain to a subdirectory through htacess..
I have a domain lets say sub.domain.com and the directory domain.com/site/
I want to redirect sub.domain.com to domain.com/site not changing any url, simply redirecting in a SEO friendly way.
I've tried a redirect 301 rule but it doesn't seem to have worked.
Try this in your .htaccess:
RedirectMatch 301 wiki.comp.tf(.*) comp.tf/wiki$1
If that does not work, an alternative option is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^wiki.comp.tf
RewriteRule ^(.*)$ http://comp.tf/wiki$1 [L,NC,QSA]
Some potential options for you using actual names:
Redirect 301 wiki.comp.tf comp.tf/wiki
You can use the following rule :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ /site/$1 [L]
This will internally redirect
sub.domain.com
to
/site/

.htaccess redirect root url exclude index.php

I want to redirect the root URL only excluding index.php which is in root folder. This is what i have so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} website\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://website.com/redirect/ [L,R=301]
RewriteRule ^/index\.php$ - [L]
It still redirects the index.php file when manually entered!
Please try the following:
RewriteEngine on
# Condition: Only match host 'website.com'
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
# Rule: If request is for root, redirect
RewriteRule ^$ http://website.com/redirect/ [R=302,L]
As you used 301 already, you may need to clear the cache for the browser to redirect properly.
Once you are happy, and would like to make the redirect permanent, change 302 to 301.

.htaccess redirect to a single subdomain

I have a website like www.example.com, I want to redirect each hit to this URL and Sub URL to a single URL on subdomain. Following are some examples
www.example.com should be redirected to http://test.example.com
www.example.com/show/mypage1 should be redirected to http://test.example.com
www.example.com/show/mypage2 should be redirected to http://test.example.com
www.example.com/show/mypage3 should be redirected to http://test.example.com
I want to do it using .htaccess. and I want 302 temporary redirect. I am using Apache WebServer.
Are you using Apache or IIS?
In Apache:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/ [L,R=302]
edited
edited: replaced http://test.example.com/$1 to http://test.example.com/, it works now as required.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/ [L,R=302]
</IfModule>

How to redirect non-www to www URL's using htaccess?

I have a website say http://www.example.com/ in the root of my website, I have added .htaccess file to redirect any request of http://example.com/ to http://www.example.com/
Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/ . In this video folder I have another htaccess file which is performing rewriting for video entries. When I am trying to access http://example.com/videos/ then its not redirecting me to http://www.example.com/videos/
I think .htacces is not inheriting the previous rules from the parent directory. Can anyone please tell me what can be the rule I can add in the .htaccess file of /videos/ folder so that any request for http://example.com/videos/ will be redirected to http://www.example.com/videos/ URL.
This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The contrary is also possible (www to non-www):
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I think it may just be that your existing rule is too strict, and is not getting triggered in your subdirectory because of this. Something like this should work site-wide:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
This is Best Solutions to redirect non-www to www URL's using htaccess. using this code in htaccess file and check url.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
I recommend everyone to stick with the below method it will only redirect the main domain only not any other sub-directory or sub-domain in your host.
# Redirect non-www to www only for main domain
# recommended if you have any/some sub-domain(s)
RewriteEngine on
RewriteBase /
# Replace yoursite and .tld respectively
RewriteCond %{HTTP_HOST} ^yoursite\.tld$
# Replace yoursite.com
RewriteRule ^(.*) http://www.yoursite.com/$1 [R=301]
Use this method if you are really sure and I hope you will that you won't ever use sub-domain with your website i.e. subdomain.yoursite.com or whatever. Than you're welcome to use the below method.
"Saying Again make sure if you really want to use this method"
# Redirect all non-www to www including subdomain(s)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The below method will redirect all www url to non-www including your sub-domains although you cannot use www.subdirecty.yoursite.com it will prompt you an error with 500.
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]