Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have two two files /etc/apache2/sites-available/foo and /etc/apache2/sites-available/foo_maintenance

The rewrite rule I use in /etc/apache2/sites-available/foo is

<Directory /var/www/public_html>
        Options +FollowSymlinks
        RewriteOptions inherit
        RewriteEngine on
        #
        RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
        RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
</Directory>

so that all mysite.com/* redirect to www.mysite.com

After I take my site down for maintenance, if the user is navigates to a subpage of the site like mysite.com/subdir/something.php I would like to redirect them to www.mysite.com so the index.html of the maintenance page would be displayed.

What is the rewrite rule to redirect all traffic from any subpage to www.mysite.com?

share|improve this question

2 Answers 2

up vote 1 down vote accepted
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]

RewriteRules have three parts. A pattern to match, the replacement, and the flags. The pattern in this case is ^(.*)$. The ^ represents the beginning of the line, and the $ represents the end of the line. The . represents any character, and the * following it means that it can occur any number of times. Now, the round brackets define a capture group, which essentially stores everything that occurs between the brackets. These capture groups are then available for you to use in the replacement. So if the request is for /subdir/something.php, then the first capture group will contain "/subdir/something.php".

The replacement in this case is http://www.mysite.com/$1, and the $1 represents the first capture group in the matching pattern. The target URL is formed by filling in these capture groups, which in this case will yield http://www.mysite.com/subdir/something.php.

So if you want to always send users to the root of www.mysite.com regardless of the path which they requested, you just need to not fill in the path in the replacement, i.e. by removing the $1 part. Your final rule will look like this:

RewriteRule ^(.*)$ http://www.mysite.com/ [R=301,L]
share|improve this answer
    
I'm getting a redirection loop error. btw what are u doing up this late if youre in mountain view? –  user784637 Jun 19 '12 at 8:51
    
@user784637 Did you replace your existing RewriteRule with this one (i.e. retaining that RewriteCond)? I wish I knew the answer to that question... ;-) –  mgorven Jun 19 '12 at 9:09
    
Yah =( Unfortunately it's not redirecting, it loads the subpages. I started with the directive I included in the original post, then the only I change I made was removing the $1 part –  user784637 Jun 19 '12 at 9:19
    
Hey Michael - Sorry, I wasn't fully clearing the browser cache, your solution works –  user784637 Jun 19 '12 at 9:33

You can go with something along the lines of this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteCond %{REQUEST_URI} !^/$ [NC]
RewriteRule ^.*$ http://www.mysite.com/ [L]
  • Line 1 enables the rewrite engine in this context.
  • Line 2 checks if the request is sent to mysite.com.
  • Line 3 checks if the requested URI is not already /.
  • Line 4 redirects all addresses to http://www.mysite.com/.
share|improve this answer
    
This isn't redirecting subpages to the homepage, it just loads the subpage –  user784637 Jun 19 '12 at 8:54
    
I fixed a typo in the 3rd rule (slash doesn't have to be escaped), maybe that was the problem. Other than that, I don't see why it wouldn't work. Do you have any other rewrite rules in the virtual host that we should be aware of? –  Vladimir Blaskov Jun 19 '12 at 9:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.