apache - htaccess Map One Directory To Another One Level Up -



apache - htaccess Map One Directory To Another One Level Up -

so here’s i’m trying accomplish. have link:

https://www.mydomain.com/foo/bar/

now, directory "foo" has site in , operational. sake of organization have had create site this:

https://www.mydomain.com/fubar/

so in reality link https://www.mydomain.com/foo/bar/ isn’t directory it. rather happen when people go https://www.mydomain.com/foo/bar/ address doesn’t alter in address bar, rather on backend software brings , uses https://www.mydomain.com/fubar/.

example:

when goes https://www.mydomain.com/foo/bar/sign_up.php still see in address bar, they're getting https://www.mydomain.com/fubar/sign_up.php.

what i've tried far no avail:

htaccess @ 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 @ https://www.mydomain.com/

rewriterule ^foo/bar/(.*) /fubar/$1 [l]

update: directory https://www.mydomain.com/foo/ root directory https://www.anotherdomain.com/. https://www.anotherdomain.com/bar should bringing https://www.mydomain.com/fubar/

you can’t things have ../ parent directory references rewrite rules:

options +followsymlinks rewriteengine on rewritebase / rewriterule ^bar/(.*) ../fubar/$1 [nc,l]

what need set in .htaccess of site’s root on https://www.mydomain.com/:

options +followsymlinks rewriteengine on rewritebase / rewriterule ^foo/bar/?(.*)$ fubar/$1 [qsa,nc,l]

the lastly line grabs url has foo/bar in it’s path, /? makes trailing slash optional, , (.*)$ captures values passed parameters.

now, not 100% sure on addition of qsa (query string append) rewrite rule flags, thought query string values passed destination when use. assume need, if don’t utilize instead:

options +followsymlinks rewriteengine on rewritebase / rewriterule ^foo/bar/?(.*)$ fubar/$1 [nc,l]

also, there nice way debug rules without reloading browser of time can headache & cause issues when content cached. , temporarilly add together r (rewrite) flag & utilize curl -i view response headers straight while debugging.

for example, on local mamp (mac os x lamp) setup see when run curl -i http://localhost:8888/foo/bar/ 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 can see location changes location: http://localhost:8888/fubar/ when using r flag. want. when done tweaking rules, remove r flag & should go.

edit: since original poster states desired behavior in update question, rewrite rule never work:

the directory https://www.mydomain.com/foo/ root directory https://www.anotherdomain.com/. https://www.anotherdomain.com/bar should bringing https://www.mydomain.com/fubar/.

for case this, mod_rewrite wrong tool job. utilize mod_proxy instead. first enable in apache this; illustration assumes on ubuntu 12.04 should work on linux apache install

sudo a2enmod proxy proxy_http

then set enable reverse proxy on https://www.anotherdomain.com path of /bar/ https://www.mydomain.com/fubar/:

<ifmodule mod_proxy.c> # proxy specific settings proxyrequests off proxypreservehost on <proxy *> adddefaultcharset off order deny,allow allow </proxy> proxypass /bar/ https://www.mydomain.com/fubar/ proxypassreverse /bar/ https://www.mydomain.com/fubar/ </ifmodule>

apache .htaccess mod-rewrite reverse-proxy mod-proxy

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -