redirect blog.mydomain.com - apache

I currently have a wordpress blog located at blog.domain.com and my regular website at www.domain.com, both hosted on an apache server.
my website root has it's own .htacess file, and my blog root has an .htaccess file.
virtual hosts are setup within the httpd.conf file
everything works as it should, what I'm trying to figure out is how to set-up the following redirects.
main blog
blog.domain.com - Redirect to - www.domain.com/blog
all the blog articles
blog.domain.com/blog/article_name - redirect to - www.domain.com/blog/article_name
This needs to be seamless so we don't effect the permalinks already created within the blog
Does anyone have any ideas?

Using a Redirect rule in your .htaccess file on blog.domain.com:
Redirect 301 /blog http://www.domain.com/blog
Redirect 301 / http://www.domain.com/blog/

Seems like you have access to your apache's conf files.
I would suggest you to do this:
create separate Virtual host block to blog.domain.com.
<VirtualHost *:80>
ServerName blog.domain.com
#other settings
RewriteEngine on
RewriteRule ^(blog)(/.*)? http://www.domain.com/$1$2 [R=301,L]
RewriteRule ^(.*)$ http://www.domain.com/blog/$1 [R=301,L]
</VirtualHost>
Actually just the above will suffice.

Related

How to do this Apache mod-rewrite redirect transparently

I have this redirect (in Apache 2.4 VirtualHost *:80 configurations), which redirects example.com over to example.com/api/ (subfolder) and it works flawlessly.
Once I enter http://example.com into the browser, it takes me directly to http://example.com/api/.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /api$1 [R,L]
Now, what I need is for this to work transparently. I want http://example.com/api to load directly on the root domain: http://example.com
I've tried all kinds of suggestions I found on the internet, but nothing that would hide/mask the subfolder from the final URL that shows in the browser.
I figured there is an easier solution to this. I am running my app on a Tomcat server behind Apache and thus adding the following to Apache rules into VirtualHost config fixed the issue with subfolder transparency.
ProxyAddHeaders off
ProxyPass / http://server-host-for-example-com:8080/connect/
ProxyPassReverse / http://server-host-for-example-com:8080/connect/

URL Rewrite in httpd.conf

I have a redirect setup on my Apache server within the httpd.conf file that redirects all traffic to "server.mydomain.com" to https://server.mydomain.com/uri. Unfortunately, some users will enter https before the URL and it does not redirect, instead they get Apache error page. I want to know how can I get users who input https before the URL to be redirect to https://server.mydomain.com/uri. I believe I may have to do a rewrite but I'm not sure or know how to go about doing it. I've research about rewrite and found it should be done in the .htaccess file but when I read Apache best practice they state it should not be done within .htaccess file for security and performance. Instead, it should be done within the config file. Since the redirection is working within the httpd.conf file, I would like to incorporate the rewrite there as well. I presume that is the correct according to Apache website. My issue is how do I go about doing this within that file. I've included the file information below. Any assistance is greatly appreciated. The server is using Tomcat 7 with Apache 2.2.15.
httpd.conf file:
</IfModule>
#
ProxyPass /uri/fbs-ws ws://server.mydomain.com:8081/uri/fbs-ws
ProxyPassReverse /uri/fbs-ws ws://server.mydomain.com:8081/uri/fbs-ws
</IfModule>
<VirtualHost *:80>
ProxyPass /uri http://server.mydomain.com:8080/uri
ProxyPassReverse /uri http://server.mydomain.com:8080/uri
Redirect permanent / https://server.mydomain.com/uri
</VirtualHost>
Did you try creating a vhost for 443 and switching to http there via Rewrite?
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

Cannot 301.htaccess redirect in XAMPP

A simple working 301 .htaccess redirect on my live server looks like this:
Options +FollowSymLinks
RewriteEngine on
redirect 301 /test.php /index.php
But I can't make it work my XAMPP installation.
I have enabled mod_rewrite.so in the http.conf file and I can't see why it does not redirect. Have restarted apache but no luck.
Just to let you know that Apache's redirect directive is from mod_alias module not from more_rewrite module. Check docs here: https://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirect
If you want to handle it via mod_rewrite it then use it like this:
RewriteEngine on
RewriteRule ^test\.php$ /index.php [L,NC,R=301]
ALternatively enable mod_alias module to make redirect work.
Got it working by creating a local domain Using Apache Virtual Hosts. Once the local domain is setup, the redirect code works as it does on the live server:
redirect 301 /test.php /test2.php.php

Forward a subdomain to a subfolder and preserve subfolder's .htaccess mod_rewrite rules

I have a standard LAMP setup and I'm trying to make subdomains automatically work as long as the appropriately-named folder exists.
If you're familiar with MediaTemple's GridServer shared hosting service, I'm trying to emulate the way it handles wildcard subdomains:
If subdomain.domain.com is requested, look for a folder named subdomain.domain.com
If the folder is found, call it home and serve up the site inside of it, obeying any .htaccess files that may be in there.
If no folder is found, just display domain.com
I've been told a .htaccess mod_rewrite in my root domain is the way to go, and I've been able to detect subdomains and at least point to the appropriate subfolder, but I don't think this leaves any opportunity for the subdomain.domain.com .htaccess file's own mod_rewrite to take over once the server knows where the subdomain's folder is located.
Here's my .htaccess for that:
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]+)\.domain.com$
RewriteRule ^(.+) /vhosts/%1.domain.com/html/pages/$1.php [L,QSA]
...And my folder structure:
domain.com
html
.htaccess
vhosts
subdomain.domain.com
html
.htaccess
pages
index.php
pagename.php
So as you can see, the site inside subdomain.domain.com is dependent on it's own mod_rewrite in order to function, since the page files aren't where the server expects them.
Now, I know I could probably include the subdomain's .htaccess rules in my root domain's .htaccess with the appropriate conditions, but the kicker is that I need to be able to point completely different domain names to these subdomains too (so the subdomain is accessible via subdomain.domain.com and mydomain.com), so these subdomain.domain.com folders need to be totally self-sufficient.
So how can I get my server to look in the correct location for a subdomain's folder, while allowing its own .htaccess mod_rewrites to work?
Any help is greatly appreciated!
Just had someone suggest that mod_rewrites are the wrong way to go about it, and mod_vhs is what I want to use. Does anyone know about mod_vhs?
I got it to work using VirtualHosts:
NameVirtualHost *
# Root domain homepage
<VirtualHost *>
DocumentRoot /var/www/domain.com/html
ServerName domain.com
ServerAlias www.domain.com
</VirtualHost>
# Hosted sites
<VirtualHost *>
VirtualDocumentRoot /var/www/vhosts/%0/html
ServerName *
ServerAlias *
</VirtualHost>
The first entry catches requests to my home page and serves up the standard site, while the second catches EVERYTHING else and routes to a different location based on the host name requested.
Easy. Just make sure the subdomain is registered with your server's DNS, then point it with an htaccess like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com
RewriteRule .* /subfolder [L]
No need to mess with the subfolder's htaccess.

Redirect non-www to www not working

I set up a virtual server using virtualmin, it didn't create the .htaccess file so I created one in the public_html folder and put the following code
RewriteEngine On
RewriteCond % ^megahotserved.com [NC]
RewriteRule ^(.*)$ http://www.megahotserved.com/$1 [L,R=301]
restarted apache and no effect and then tried
<VirtualHost *:80>
ServerName megahotserved.com
Redirect permanent / http://www.megahotserved.com/
</VirtualHost>
in the httpd.conf file, when I restarted apache firefox came up with an error
The page isn't redirecting properly.
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
What should I do ?
your code is correct, you just need to follow the next paragraph which I quoted from http://httpd.apache.org/docs/current/mod/mod_rewrite.html
By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each section:
RewriteEngine On
RewriteOptions Inherit
Seems like you don't have a VirtualHost that properly matches the www. address, so requests for http://www.megahotserved.com/ are hitting the very same vhost and getting into a circular redirect. So the redirect is working fine; you just have a different part of the server config to fix.
Agree with the above, and a small addition: it is better to redirect non-www to www rather than rewrite, otherwise you have two complete views ("copies") of your entire website; each page has two URLs, instead of one canonical one. This can be bad for search engines and other things.