[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Redirect (or rewrite) URL with Apache

Posted on 2009-04-30
14
Medium Priority
?
1,581 Views
Last Modified: 2012-05-06
I am trying to configure a rewrite with Apache so that anyone who goes to the root of the site, is redirected to a specific subdirectory.

For example:

Anyone going to

http://my.domain.com 

is automatically redirected to

http://my.domain.com/This/Sub/Directory

A couple of important points to note.

1.  All requests, must be rewritten as https
2.  The subdirectory is case sensitive (ie: /this/sub/directory is wrong, but /This/Sub/Directory is correct)
3.  Any requests for a specific resource (ie: something other than the root) should not be redirected, only rewritten as https.  For example, http://my.domain.com/assets/logo.gif should be rewritten as https://my.domain.com/assets/logo.gif, but not redirected at all

I currently have the following directives in httpd.conf to rewrite as https:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

and this works perfectly.

I had thought that maybe I could simply add another rule like this:

RewriteRule http://my.domain.com http://my.domain.com/This/Sub/Directory

I thought that if I made it the first rule it would handle the redirection, and the next rule would rewrite the request as https.

Alas, it doesn't work.  

I also tried the following:

Alias / /This/Sub/Directory

But that just gave me an error.

I am not sure of the best way to implement this - I don't know if I should be using a redirect or a rewrite.  

This is kind of urgent, so any help is greatly appreciated.

Cheers,

Paul
0
Comment
Question by:mrgordonz
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 51

Expert Comment

by:Steve Bink
ID: 24277257
Your https rewrite needs to be an external redirection since it is changing protocols.  That means your first RewriteRule acts separately from all the others, and should end with the [R] modifier.

To detect just a request to the home directory and send it somewhere else:

RewriteRule ^\/$ https://my.domain.com/This/Sub/Directory [R]
0
 

Author Comment

by:mrgordonz
ID: 24277416
Hi routinet,

I tried it this way:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^\/$ https://my.domain.com/This/Sub/Directory [R]

And this way:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^\/$ https://my.domain.com/This/Sub/Directory [R]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

And neither way made any difference.  When I browse to the home directory, I just get the standard Apache test page that says "Seeing this instead of the website you expected?".  It does the https rewrite perfectly, but not the redirection to the subdirectory.

I'm obviously missing something.
0
 
LVL 51

Expert Comment

by:Steve Bink
ID: 24278279
The second one will only work for requests in the http protocol.  If someone goes to https://www.mydomain.com/, the rule would not match.

The first one should match.  Can you turn on your rewrite log and post what happens when that rule is test against a request?
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 27

Expert Comment

by:caterham_www
ID: 24278459

RewriteEngine On
RewriteRule ^/$ https://%{HTTP_HOST}%/This/Sub/Directory/ [R=301,L]
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Open in new window

0
 

Author Comment

by:mrgordonz
ID: 24278563
I've been doing some more experimenting, and I think I am getting closer.  If I use the following directives:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/(.*)$ https://my.domain.com/This/Sub/Directory/%1/$1 [R]

When I browse to http://my.domain.com, I am successfully redirected to https://my.domain.com/This/Sub/Directory

But when I browse to https://my.domain.com, the URL remains the same and all I see is the Apache welcome page.

Basically I need http://my.domain.com AND https://my.domain.com to both redirect to https://mydomain.com/This/Sub/Directory
0
 

Author Comment

by:mrgordonz
ID: 24278596
Hi caterham_www,

I tried your suggestion, and it half works.

When I browse to http://my.domain.com, I am successfully redirected to https://my.domain.com/This/Sub/Directory

But when I browse to https://my.domain.com, the URL remains the same and all I see is the Apache test page.

Basically I need http://my.domain.com AND https://my.domain.com to both redirect to https://mydomain.com/This/Sub/Directory
0
 

Author Comment

by:mrgordonz
ID: 24278662
I have been trying lots of different combinations, and it seems like the one bit that won't work is to redirect from

https://my.domain.com 

to

https://my.domain.com/This/Sub/Directory

I must confess that my experimentation is a bit random - I don't fully understand the syntax of the rewrite rules or conditions, so I'm just trying different things and hoping for the best. :)
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 24279005
Where are you placing the RewriteRules (may be we don't need mod_rewrite at all, mod_alias is simpler and faster).

You have different <virtualhost...> sections for port 80 and 443, correct?

If yes,

1) would go into your <virtualhost> for port 80 and 2) into the <virtualhost> section for port 443:
1) (port 80)
 
RedirectMatch 301 ^/$ https://my.domain.com/This/Sub/Directory
RedirectMatch 301 ^/(.*) https://my.domain.com/$1
 
1) (port 443)
RedirectMatch 301 ^/$ https://my.domain.com/This/Sub/Directory

Open in new window

0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24279021
line 6 should read "2)   (port 443)"

No "rewriteEngine on" needed.
0
 
LVL 11

Expert Comment

by:Goodangel Matope
ID: 24279091
Have you tried using HTAccess? (RedirectMatch)

Check this link out

http://perishablepress.com/press/2008/10/06/redirect-subdirectory-to-root-via-htaccess/
0
 

Author Comment

by:mrgordonz
ID: 24279208
Hi caterham_www,

No, I don't have different <virtualhost...> sections for port 80 and 443.  I'm not using virtual hosts at all.  Can I still use RedirectMatch if I'm not using virtual hosts?
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24279613
No, that won't work because "redirect everything to https" would create a loop without checking for https =off.

But since you have the rule / --> /subdir in place and it seems not to hit your https request (and there's no condition for that rule), it should match.

Enable a rewriteLog as suggested above via

Rewritelog logs/rewrite.log
RewriteLoglevel 5

but I'd expect it's empty if you request https://example.com/
0
 

Author Comment

by:mrgordonz
ID: 24284035
Hi caterham_www,

I have set up two virtual hosts, one for 80 and one for 443.  Here is what I have configured:

<VirtualHost *:80>
    RedirectMatch 301 ^/$ https://my.domain.com/This/Sub/Directory
    RedirectMatch 301 ^/(.*) https://my.domain.com/$1
</VirtualHost>

<VirtualHost *:443>
    RedirectMatch 301 ^/$ https://my.domain.com/This/Sub/Directory
</VirtualHost>

As before, it works for http://my.domain.com, but not for https://my.domain.com.

I have turned commented out all the rewrite directives so only the virtual host redirects are active.  But I did capture a log of the rewrites, and you are right that nothing was logged when I requested https://my.domain.com.

I have not masked the domain or URL from the log file.


124.187.99.108 - - [01/May/2009:23:18:18 +1000] [saba.amp.com.au/sid#26c0d8][rid#47d7730/initial] (2) init rewrite engine with requested uri /
124.187.99.108 - - [01/May/2009:23:18:18 +1000] [saba.amp.com.au/sid#26c0d8][rid#47d7730/initial] (3) applying pattern '^/$' to uri '/'
124.187.99.108 - - [01/May/2009:23:18:18 +1000] [saba.amp.com.au/sid#26c0d8][rid#47d7730/initial] (2) rewrite / -> https://saba.amp.com.au/Saba/Web/Main/
124.187.99.108 - - [01/May/2009:23:18:18 +1000] [saba.amp.com.au/sid#26c0d8][rid#47d7730/initial] (2) explicitly forcing redirect with https://saba.amp.com.au/Saba/Web/Main/
124.187.99.108 - - [01/May/2009:23:18:18 +1000] [saba.amp.com.au/sid#26c0d8][rid#47d7730/initial] (1) escaping https://saba.amp.com.au/Saba/Web/Main/ for redirect
124.187.99.108 - - [01/May/2009:23:18:18 +1000] [saba.amp.com.au/sid#26c0d8][rid#47d7730/initial] (1) redirect to https://saba.amp.com.au/Saba/Web/Main/ [REDIRECT/301]

Open in new window

0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24285874
> I have set up two virtual hosts, one for 80 and one for 443

You'd have to configure the whole virtualhost, the main server config is not inherited. And, for SSL, you should use IP based virtualhosts (protocol limitation):
# IP needs to be modified
<VirtualHost 127.0.0.1:80>
    Servername my.domain.com
    DocumentRoot /var/www
    RedirectMatch 301 ^/$ https://my.domain.com/This/Sub/Directory
    RedirectMatch 301 ^/(.*) https://my.domain.com/$1
</VirtualHost>
 
# IP needs to be modified
<VirtualHost 127.0.0.1:443>
    Servername my.domain.com
    DocumentRoot /var/www
    RedirectMatch 301 ^/$ https://my.domain.com/This/Sub/Directory
    # SSL certificate stuff and other directives
    #....
</VirtualHost>

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question