Redirect sub domain to sub directory - php

I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]

Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).

Related

.htaccess redirect a folder in subdomain to a folder in main domain

Here's what I need.
I need to redirect (301) all content of blog.example.com/sample/ to example.com/sample/ and I'd like to accomplish this using htaccess.
Here's what I tried, but it didn't work:
RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]
RewriteRule ^(.*)$ https://www\.example\.com/sample/$1 [L,R=301]
Thanks for the help.
RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]
Here having /sample is problematic because HTTP_HOST only matches host name part in a request so it will never match this condition.
You can use this rule instead:
RewriteCond %{HTTP_HOST} ^blog\.(example\.com)$ [NC]
RewriteRule ^sample/ https://www.%1%{REQUEST_URI} [L,R=301,NC,NE]

Conditional htaccess

I just want some help with my .htaccess configuration. Before I have this problem where I need to redirect the page to
a specific url. Like if the user type http://mydomain/test it will be redirected to http://mydomain/app/r/test. I
achieved the redirection using .htaccess. The code is below
RewriteEngine on
RewriteRule (.*) http://mydomain/app/r/$1 [R=301,L]
But I have this new specification. As follows:
If user visit http://mydomain/ or http://domain/index.php, he will be
redirected to http://mydomain/mymanager
If user visit
http://mydomain/{any_segment}, he will be redirected to
http://mydomain/app/r/{any_segment}
Here is my current htaccess. Basically in the url, any characters will be redirected to /app/r/{} except index.php. But what about the http://domain/? How can I achieve this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
I've been playing around my htaccess and any pointers will be of great help to me.
These are your rules :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
It looks like , any request should redirect to /app/r/ except A URI start with /index.php but still not doing that , in normal cases , so you should exclude the target directory from being redirect again and again by this rule RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L] , ^(.*)$ means any URI start with any thing and the server will look, if there is a condition , and actually there is only one condition to exclude index.php, otherwise redirect .
So , you should exclude this target directory along with index.php like this :
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/)
But still you want to match a request that target HOST only so,you should add that as well :
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/|/?$)
Then you could match host with www or non-www like this :
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
Your rules should look like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/|/?$)
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
Also you could summarize that more by utilizing %{REQUEST_URI} like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
RewriteRule !^(index\.php|app/r/|/?$) http://mydomain/app/r%{REQUEST_URI} [R=301,L]
NOTE: Clear browser cache then test

Wildcard redirect links going to root to subfolder on same domain

I have moved a website from www.example.com to www.example.com/subfolder and using an .htaccess file, I would like to redirect old links going to www.example.com to www.example.com/subfolder. My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subfolder/$1 [L]
I've also tried the following:
#RewriteCond %{REQUEST_URI} !^/subfolder/
#RewriteRule ^.*$ http://www.example.com/subfolder%{REQUEST_URI} [R=301,QSA,L]
I keep getting redirect loops or the redirect not working and leading to an error 404.
You can use this rule in site root .htaccess (parent directory of subfolder) as the first rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!subfolder/)(.*)$ /subfolder/$1 [L,NC,NE,R=301]
# remaining rules go here
Make sure to clear your browser cache completely when testing this change.
are you using wordpress? Or you just moving in general?
If you are moving in general this one-liner will work
RedirectMatch ^/$ /subfolder/
Original answer from here
This is another solution i'm using currently.
Do not copy and paste. You need to Replace DOMAIN (without the .com) and SUBFOLDER to the name u want.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /subfolder/$1 [L]

.htaccess Dynamic sub domains

I want to have dynamic subdomains, such as:
(.*).mydomain.com
that I can retreive with $_GET['subdomain']. I have added an wildcard DNS record (* A xx.xx.xx.xx), and now for every subdomain I go to (e.g. test.mydomain.com) all I get is:
Apache is functioning normally
I have tried this code in .htaccess, but nothing seems to help:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
RewriteRule ^(.*)$ http://www.mydomain.com/?subdomain=%1 [L]
Thanks in advance!
Are you looking for something more like this?
RewriteCond %{QUERY_STRING} !subdomain=
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /$1?subdomain=%1 [L,QSA]

URL Rewriting domain to use subdirectory as root; still shows that directory in URL in Analytics?

I set up a certain domain to rewrite into a folder, shown below, but have been having people show up in that folder in Google Analytics.
in short: /c/ -> root when from domain, but GA still shows domain/c/ in Visitors Flow
root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)$ $1.txt
Options -Indexes
and /c/'s .htaccess is just an edited html5boilerplate one with the only difference being the same php and txt rewrite above.
But on Google Analytics, more than half of my visitors are still ending up on /c/index.php.
Visitor Flow: http://i.imgur.com/JwtcTFI.png
I haven't seen /c/ in my URL, so I really don't have a clue what's going on.
Any ideas? I do use relative URLs (is this a bad idea?) so that causes the flow to continue in the /c/ folder.
Thanks for the help!
Your rules do nothing to prevent people from directly accessing /c/whatever. If you want to prevent that, you need to add an extra rule:
RewriteCond %{THE_REQUEST} \ /c/
RewriteRule ^c/(.*)$ /$1 [L,R=301]
Oo, I found the answer! It seems like www.domain was redirecting to domain/c/index.php. I had a rewrite rule to remove the "www" in the /c/.htaccess, but not the root .htaccess.
I fixed it by adding code to remove the "www" in the root htaccess too BEFORE the domain>>directory rewrite. (after didn't seem to work)
I added
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
above
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
(in the root htaccess)
edit: It also probably has to do with the (www.), I dropped that due to redundancy. Now it's just RewriteCond %{HTTP_HOST} ^certain-domain.com$ [NC]
I also added a rewrite for /c/ to root in the /c/ .htaccess:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /c/
RewriteRule ^(.*)$ /$1 [L,R=301]

Categories