开发者

How to config Apache2 to redirect URL

开发者 https://www.devze.com 2023-02-03 00:34 出处:网络
I am trying to config url redirect in Apache. I have tried some approa开发者_JAVA技巧ches and nothing works. Can someone tell me the solution of this as it doesn\'t seem too difficult.

I am trying to config url redirect in Apache. I have tried some approa开发者_JAVA技巧ches and nothing works. Can someone tell me the solution of this as it doesn't seem too difficult.

I am going to redirect request from:

https://myhost/configuration/jmx-console

to:

http://myanohterhost/jmx-console

This is a https to http re-direct.

Can someone point me to the right direction?

Many thanks!


You could use the RedirectMatch directive to force Apache to send the user someplace else:

RedirectMatch 301 ^(.*)$ http://www.anotherserver.com$1

See the following:

http://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectmatch

http://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx


The normal way to do this would be the following, in the configuration of the current server (or virtual host) `myhost':

Redirect /configuration/jmx-console http://myanohterhost/jmx-console

Edit: According to your comment, it looks like you can do it using one of the following techniques:

1. mod_proxy, using a reverse proxy setup

Simply map the remote server's urls to a local url:

ProxyPass /configuration/jmx-console http://myanohterhost/jmx-console
ProxyPassReverse /configuration/jmx-console http://myanohterhost/jmx-console

2. mod_rewrite using the proxy throughput feature

RewriteRule ^configuration/jmx-console(.*)$ http://myanohterhost/jmx-console$1 [P]

There can be some caveats in using reverse proxying like this, I recommend you to read thoroughly http://httpd.apache.org/docs/2.2/en/mod/mod_proxy.html to see the various options available when using reverse proxying.


Refering to the question title How to config Apache2 to redirect URL I would propose simple solution which works fine for me (using RedirectPermanent from mod_alias).

Firstly we check if our domain has proper DNS entries, as example A type entry:

name somefancydomain.com
TTL 600
type A
value 10.100.10.100

and CNAME entry:

name www.somefancydomain.com
TTL 600
type CNAME
value somefancydomain.com

Then we go to ubuntu webserver with IP 10.100.10.100 and configure new virtual host:

cd /etc/apache2/sites-available/
sudo vim redirect.conf

Paste configuration as below and save:

<VirtualHost *:80>

    ServerName somefancydomain.com
    ServerAlias www.somefancydomain.com

    RedirectPermanent / https://redirectedurl.com/

    ServerAdmin admin@redirectedurl.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Enable new virtual host and reload apache:

sudo a2ensite redirect.conf
sudo service apache2 reload

Finally check if redirection is working.


According to the Apache site you should not use mod_rewrite for simply redirecting from http to https (or the other way, which seems more common):

http://httpd.apache.org/docs/2.2/rewrite/avoid.html

The website suggests using mod_alias with the Redirect and RedirectMatch directives.


Assuming you are doing this on your hosts "myhost"

1/ touch /etc/apache2/conf-available/my-redirect.conf

2/ edit my-redirect.conf and add

Redirect permanent /configuration/jmx-console http://myanotherhost/jmx-console

3/ a2enconf my-redirect.conf

4/ apache2ctl configtest

5/ this should give "Syntax OK"

6/ systemctl restart apache2

0

精彩评论

暂无评论...
验证码 换一张
取 消