Apache Rewrite/Redirect different for TLD vs directory - apache

I've migrated the content of a site from one domain to another. I have .htaccess Rewrite set up successfully to redirect any request to the old domain to the same location on the new domain, which is working fine, using the following code:
RewriteEngine On
rewritecond %{http_host} ^roundeltable.com
rewriteRule ^(.*) http://wheelspin.tv/$1 [R=301,L]
However, I want to go a little further. When somebody simply requests the old TLD (http://roundeltable.com/) I want them to be redirected to a specific page within the new domain (http://wheelspin.tv/rt). If they make a request to any other location from the old domain (for example, http://roundeltable.com/about) I want them to be sent to that exact same place at the new domain (http://wheelspin.tv/about) the way they currently are now.
Is this possible? If so, how?

So I've found the solution to this issue, which ended up being provided by somebody from Media Temple's service department. Here's the entire code required in the .htaccess file to make this work:
RewriteEngine On
RewriteRule ^$ http://wheelspin.tv/rt/ [L]
RewriteRule ^(.*)$ http://wheelspin.tv/$1
Obviously substitute whatever domain you want to redirect to in place of mine, but that's the solution for my example. Thanks for the help!
If you want to see all the details on my solution, I have it here: christiaanconover.com/media-temple-service-rocks/

Related

htaccess Remove directory from end of URL in apache

Ok, so I know this is a question that has been asked many times, however, I have not been able to find an answer to my particular case, so please do not shoot me down.
I have a website: http://gmcomputers.co.za.
I am redirecting this URL, using .htaccess file, to a subfolder to load the content:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /gmcomputers/ [L,DPI,R=301]
Which works perefectly, except when I go to http://gmcomputers.co.za I get http://gmcomputers.co.za/gmcomputers/.
So my question is, how do I modify the above code to remove the /gmcomputers/ from being appended?
Please note I copied the code above from a website as I am not at all experienced in redirect, etc and am still learning. Also, the reason I am using .htaccess to redirect is due to there being other websites in the root directory and I therefore cannot edit any config files for Apache.
Thanking you.
You contradict yourself in your question. On the one hand you write that you want to redirect and that this "works perfectly", but then you write that you do not want that result.
My guess is that you actually do not want to redirect at all, but that instead you want to internally rewrite your requests to point to that server side folder. While the URL visible in the browser's URL bar does not show that folder. Is that what you are trying to ask?
If so take a look at this example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
You might want to add an actual redirection to direct clients actually using the folder name in their requests:
RewriteEngine on
RewriteRule ^/?gmcomputers/(.*)$ /$1 [R=301,END]
RewriteCond %{REQUEST_URI} !^/gmcomputers
RewriteRule ^ /gmcomputers%{REQUEST_URI} [END]
Best is to implement such rules in the central http server's host configuration. If you do not have access to that you can instead use a distributed configuration file (typically called ".htaccess") located in the DOCUMENT_ROOT folder configured for the http host, if you enabled the consideration of such files in your host configuration . Though that comes with a number of disadvantages. Above implementation works likewise for both approaches.

Canonicalizing domain name with mod_rewrite

I had a working .htaccess for my website, but I decided to add domain name canonicalization - e.g. prepend 'www' if it is omitted.
Here's what I'm using now:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^.*$ http://www.domain.com/$0 [L,R=301]
RewriteRule ^$ http://www.domain.com/blog/ [L,R=301]
// etc
The first condition/rule are the ones I added. The second rule redirects empty filepath to "blog/". Now, the problem is, if I type in a path like "domain.com/page", for some reason it redirects me to the blog. If I change the first rule to http://www.domain.com/test?url=$0, it correctly redirects me to www.domain.com/test?url=page. What am I doing wrong, and is there a way to possibly see some sort of logs/debug output? (I'm using shared hosting so I don't have access to low level logs)
Update: its working fine now; it seems the redirects were cached somewhere, even though clearing all browser data didn't help. Could it be possible that the redirects got cached on proxy level, and how to deal with it, since it makes editing/debugging .htaccess nearly impossible?

.htaccess - Redirect from subdomain root to subdomain's folder (mod_rewrite/mod_proxy)

Problem
I am trying to redirect from the root folder of a sub-domain, e.g. sub.example.com, to a sub-directory of the sub-domain and keep the sub-domain in the address bar, e.g. sub.example.com/subdir1.
This is not the first time that I have used mod_rewrite, but I am not an expert. I have not used mod_proxy before, but I believe that it will be required for this task.
Background
This is part of a project that I am working on in which the web-host is private, but the server cannot be modified. I am required to specify all sub-domains using their tool, as opposed to creating the rewrites myself. This question pertains to a sub-domain being used as a development site, which will eventually go live without being a sub-domain, e.g. sub.example.com will become sub.com when it goes live. The reason why I cannot simply set the root folder of the subdomain to the directory that I am redirecting to is that the code base for the project relies heavily on the DOCUMENT_ROOT for internal link management, and the target folder, sub.example.com/subdir1, is not the root of the site.
What I have done
I will start by saying that I have exceeded my allotted time to work on this problem, mainly out of interest.
I have done a lot of research and have tried to pull bits and pieces from the sources that I have looked at. However, all but one of the sources that I have looked at only had information for redirecting from a subdomain to a subdirectory of the site's root, e.g. sub.example.com -> example.com/root-subdir. In fact, the only reason that the aforementioned page did have information related to my question is because the user who asked the question accidentally did what I am currently trying to do. Unfortunately, what he did still is not working for me. Here is what I am trying right now (but is giving me a "310 - too many redirects" error):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com
RewriteRule ^ /subdir1/ [R=301,L]
As previously stated, I think that I may need to use mod_proxy for this, but I have no idea what that would look like.
Any help that you can give would be much appreciated. Thank you for your help! :)
Now if you want to redirect just sub.example.com into sub.example.com/subdir1, then give this a try:
RewriteCond %{HTTP_HOST} ^sub.example.com
RewriteRule ^/?$ /subdir1 [R=301]
The solution given in the comment by #faa did not completely solve my issue, as my real mistake was forgetting to match the root, and only the root, of the subdomain. #faa did, however set me back onto the right track and did answer my question regarding preventing loops. The information that I gained from using his snippet led me to realize what I needed, which I found in the solution to this ServerFault question.
My final solution is the following:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subdir1/ [R=301,L]
Thank you all for your help!

mod_rewrite redirect specific .asp pages back to original server

I need to redirect traffic for a section of a redeveloped site back to the original site with apache mod_rewrite rules. I need to redirect all requests starting with http://www.example.com/page.asp back to the original site http://www.original.com/page.asp with the query string or anything following page.asp intact.
This seems simple enough, however I have had no luck with mod_rewrite generators or documentation on the web. My latest stab at the problem looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http://www.example.com$ [NC]
RewriteRule ^page\.asp(.*)$ http://www.original.com/page\.asp$1 [R=301,NC]
I appreciate any insight into correcting this mod_rewrite rule. Other redirects are working fine.
I was able to get the rewrite to function properly using:
RewriteRule ^/page(.+) http://www.original.com/page$1 [R=301,NC,L]
I found this apache doc helpful: Resource Moved to Another Server

htaccess redirect for global subdomains system - all subdomains redirected to one php file to handle them all

i really could use some advices related to subdomains.
recently we decide to make every subdomain of main portal to be available for blogs (something similar to lets say any blog platform)
so what i cant achieve is to make redirection to certain folder of main portal site or php file like:
xxxxx.domain.com/index.php?get=xx&get2=xx -> to be run by actual php file located on main portal account, lets say subdomains.php?get=x&get2=xx, alternatively - to be run by mainportal/subdomains/index.php?get=x&get2=xx
i tried with
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.pl$ [NC]
RewriteRule (.*) subdomain.php [L]
effect was almost correct but get parameters are missing:(
on the other hand it can be entirely wrong... i simply dont understand or that rewrite rules:(
thanks for any help/tips
You can try with QSA (query string append):
RewriteCond %{HTTP_HOST} ^.*\.domain\.pl$ [NC]
RewriteRule . subdomain.php [QSA]