Is it possible to redirect from one domain to another using htaccess?
RewriteRule http://www.google.com/old-route http://www.google.com/new-route [R=301,L]
RewriteRule https://www.google.com/old-route https://www.google.com/new-route [R=301,L]
If not, how would you do the redirect for multiple domains on one project?
This can be achieved by using rewriteConditions: https://wiki.apache.org/httpd/RewriteCond
Something along the lines of:
RewriteCond %{HTTP_HOST} ^site1
RewriteRule ^old-route$ https://www.site1alt.com/new-route [R=301,L]
For one Domain use.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.google\.com/old-route$ [NC]
RewriteRule ^(.*)$ http://www.google.com/new-route [R=301,L]
For redirecting everything you could use this.
Redirect 301 / http://www.google.com/new-route
Given https://www.example.com/foobar the single %{HTTP_HOST} as used in the other answers only looks for www.example.com, not taking the following path into account. So these answers were not working for me. Maybe something changed during Apache versions. What worked for me was to concatenate %{HTTP_HOST} and %{REQUEST_URI} to be checked.
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.example\.com/foo\.html$ [NC,OR]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.example\.com/bar\.html$ [NC]
RewriteRule ^(.*)$ https://www.example.com/foobar [R=301,L]
Check out this nice Mod_Rewrite Variables Cheatsheet for an overview of many available mod_rewrite variables.
Related
enter code here Hi I've the following code inside my htaccess file.
My wildcard subdomain routes to "mainfolder" - here i placed the htaccess file.
I've the following folders
"mainfolder"
"mainfolder/sub1"
"mainfolder/sub2"
etc.
Calling the subdomain - sub1.domain.com it should route to the subfolder "sub1" (subfolder=subdomain).
I tried to do it with this code
#Redirect to subdomainfolder if no special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^$ %1/index.html [L]
#Redirec to subdomainfolder if a special page is called
RewriteCond %{HTTP_HOST} ^(.*)\.domain.com(.*)$ [NC]
RewriteRule ^(.*) %1/$1 [L]
The first rule works well, but if I add the second rule I receive a internal server error.
Whats wrong with this - how how I can change the first rule in this way, that it works with all url-parameters after .com - that was the reasons for me to add the second rule.
Hope I get help for this. thanks a lot.
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/subfolder/(.*)$ http://subdomain.example.com/$1
LIke
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^sub1/(.*)$ http://sub1.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^sub1\.example\.com$
RewriteCond %{REQUEST_URI} !^sub1/
RewriteRule ^(.*)$ /sub1/$1 [L,QSA]
If more understanding step wise then follow https://beginnersbook.com/2013/08/redirecting-from-subdirectory-to-subdomain-using-htaccess/
I have one project online and it on two diffrent domains 1)www.example.com and 2)www.example.co.in. now, I want to redirect
www.example.co.in/category/
to
www.example.com/ceramic+industry/
And I want to redirect it through .htaccess or from server. I tried from server that only redirect domain name not directory, and also tried from .htaccess where I can redirect only domain or only directory not when both domain and directory combine. My project in php. So, you can give advise if it's possible in php also.
Add following rule in .htaccess(placed at root of website www.example.co.in):
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
or use 302 for temporary redirects.
IF you also want to redirect whole .co.in website to .com THEN add next line(remember in .com website's .htaccess these two Rules should not be there.)
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
If for any reason you are bound to use same .htaccess on both site then use condition like this:
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^category/(.*)$ http://www.example.com/ceramic+industry/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
============================================================
Solution to your another query:
This can be done by PHP. As I guessed all URL after category/ is handled by one PHP page.
in that PHP page code this at top:
if(isset($_GET['company'])){
$company=$_GET['company'];
$companyNew=str_replace('_','+',$company);
if($company!=$companyNew){
header("location:/category/?company="+$companyNew,true,301);
//header("location:/ceramic+industry/?company="+$companyNew,true,301);
exit;
}
}
You can try this:
RewriteCond %{HTTP_HOST} ^your.first.domain$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://your.second.domain/$1 [R,L]
This will redirect, if your request contains existing directory name.
Answer to your comment. You can try this:
RewriteCond %{ENV:REDIRECT_FINISH} !^$
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} ^company=(.*)\_(.*)$
RewriteRule ^(.*)$ /$1?company=%1+%2 [R,L,E=FINISH:1]
First two lines allow you to prevent infinite loop redirect.
If want to redirect all non-www requests to my site to the www version. All I need to do is add the following code to my .htaccess file.
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule .* http://www.mydomain.com%{REQUEST_URI} [R=301,L]
The problem is that when I write for example mydomain.com/products-1 (hidden URL for mydomain.com/products?category=1), all parameters become visible, even though they are specified on the .htaccess file, and I get an output url (after the redirect) of www.mydomain.com/products-1?category=1
How can I fix this? Is there any kind of problems with the .htaccess code above?
Try Changing your RewriteRule:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I prefer this because it will catch all *.domain.com. If that is not what you want, then use your original HTTP_HOST rule.
If my logic is working this morning, this rule should rewrite any requests that do not match:
www.example.com
and do not contain
/subfolder
to
www.domain.com/URI
This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.
So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.
Note: I do not want www., so simply http://m.daltonempire.nl
Thanks in advance,
Isaiah v. Hunen
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]
The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
Try this example:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com will be mapped to http://example.com/test/...
Try googling dynamic subdomain with php and htaccess to get better search results.
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/ under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.
I have a problem since i using rewrite url..
MY OLD URL:
Website.com/index.php?act=appdetail&appid=oWV
New Rewrite URL
http://website.com/angry_birds_rio-appdetail-oWVi.html
But all my old url are indexed in google and if any one come to my website its display the old URL and google also INDEXED the NEW URL. its make duplicate page on website problem.
Let me know the solution
My rewrite URL htaccess
RewriteEngine On
RewriteRule ^([^-])-([^-])-([^-])-([^-])-([^-]*).html$ index.php?appanme=$1&act=$2&appid=$3&page=$4&cat=$5 [L]
RewriteRule ^([^-])-([^-])-([^-])-([^-])-([^-])-([^-]).html$ index.php?appanme=$1&act=$2&appid=$3&page=$4&cat=$5&sString=$5 [L]
RewriteRule ^([^-])-([^-])-([^-]*).html$ index.php?appanme=$1&act=$2&appid=$3[L]
Here is your .htaccess file:
RewriteEngine on
RewriteRule ^/index.php?act=appdetail&appid=oWV$ http://website.com/angry_birds_rio-appdetail-oWVi.html [R=301,L]
You'll need to inform to web crawlers about the redirection, you cando it with a 301 code.
Appears the rule are .htaccess based; you need an additional set of rules to permanently redirect (301) BROWSER/CRAWLER requests for the index.php pages, if a set of CGI arguments are present, to the appropriate alias, this will tidy up Google in a few weeks. Then your rules above e.g.
RewriteEngine On
RewriteBase /
#Permanently redirect BROWSER requests for the index.php?xxx to the appropriate page alias:
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+)&page=([^&]+)&cat=([^&]+)&sString=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3-%4-%5-%6.html [R=301,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+)&page=([^&]+)&cat=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3-%4-%5.html [R=301,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteCond %{QUERY_STRING} ^appanme=([^&]+)&act=([^&]+)&appid=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%1-%2-%3.html [R=301,L]
# Followed by: YOUR RULES FROM ABOVE
Note:
1) There appears to be a typo in YOUR second rule: sString=$6 NOT sString=$5
2) The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.