Reputation: 1659
So here’s what I’m trying to accomplish. I have this link:
https://www.mydomain.com/foo/bar/
Now, the directory "foo" actually has a site in it that is up and operational. For the sake of organization I have had to create another site like this:
https://www.mydomain.com/fubar/
So in reality the link https://www.mydomain.com/foo/bar/
isn’t really a directory with anything it. What I would rather like to happen is when people go to https://www.mydomain.com/foo/bar/
that this address doesn’t change in the address bar, but rather on the backend the software actually brings up and uses https://www.mydomain.com/fubar/
.
EXAMPLE:
When someone goes to https://www.mydomain.com/foo/bar/sign_up.php
they still see this in their address bar, but what they're actually getting is https://www.mydomain.com/fubar/sign_up.php
.
What I've tried so far to no avail:
htaccess at https://www.mydomain.com/foo/
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^bar/(.*) ../fubar/$1 [NC,L]
Also
RewriteCond %{PATH_INFO} ^bar/(.*)$ RewriteRule ^.*$ ../fubar/%1 [L]
htaccess at https://www.mydomain.com/
RewriteRule ^foo/bar/(.*) /fubar/$1 [L]
UPDATE: The directory https://www.mydomain.com/foo/
is actually the root directory for https://www.anotherdomain.com/
. So https://www.anotherdomain.com/bar
should be bringing up https://www.mydomain.com/fubar/
Upvotes: 3
Views: 5239
Reputation: 26066
You can’t do things like have ../
parent directory references like that for rewrite rules:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^bar/(.*) ../fubar/$1 [NC,L]
What you need to do is set something like this in the .htaccess
of your site’s root on https://www.mydomain.com/
:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^foo/bar/?(.*)$ fubar/$1 [QSA,NC,L]
The last line basically grabs any URL that has foo/bar
in it’s path, the /?
makes the trailing slash optional, and the (.*)$
captures the values passed as parameters.
Now, not 100% sure on my addition of QSA
(Query String Append) to the rewrite rule flags, but the idea is the query string values get fully passed to the destination when you use. Which I assume you would need, but if you don’t just use this instead:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^foo/bar/?(.*)$ fubar/$1 [NC,L]
Also, there is a nice way to debug rules like this without reloading the browser all of the time which can be a headache & cause issues when content is cached. And that is to temporarilly add the R
(Rewrite) flag & use curl -I
to view response headers directly while debugging.
For example, on my local MAMP (Mac OS X LAMP) setup I see this when I run curl -I
to http://localhost:8888/foo/bar/
with the R
flag set:
curl -I http://localhost:8888/foo/bar/
HTTP/1.1 302 Found
Date: Mon, 23 Jun 2014 14:11:11 GMT
Server: Apache/2.2.23 (Unix) mod_ssl/2.2.23 OpenSSL/0.9.8y DAV/2 PHP/5.4.10
Location: http://localhost:8888/fubar/
Content-Type: text/html; charset=iso-8859-1
As you can see the Location
changes to Location: http://localhost:8888/fubar/
when using the R
flag. Which is what you want. Then when you are done tweaking your rules, just remove the R
flag & you should be good to go.
EDIT: Since the original poster states this desired behavior in their update to the question, a rewrite rule will never work:
The directory
https://www.mydomain.com/foo/
is actually the root directory forhttps://www.anotherdomain.com/
. Sohttps://www.anotherdomain.com/bar
should be bringing uphttps://www.mydomain.com/fubar/
.
For a case like this, mod_rewrite
is the wrong tool for the job. Use mod_proxy
instead. First enable it in Apache like this; example assumes you are on Ubuntu 12.04 but should work on most any Linux Apache install
sudo a2enmod proxy proxy_http
Then set this to enable a reverse proxy on https://www.anotherdomain.com
from your path of /bar/
to https://www.mydomain.com/fubar/
:
<IfModule mod_proxy.c>
# Proxy specific settings
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ProxyPass /bar/ https://www.mydomain.com/fubar/
ProxyPassReverse /bar/ https://www.mydomain.com/fubar/
</IfModule>
Upvotes: 3