How to config Apache2 to redirect URL

可紊 提交于 2019-12-21 17:11:12

问题


I am trying to config url redirect in Apache. I have tried some approaches 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!


回答1:


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.2/mod/mod_alias.html#redirectmatch

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




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/4596066/how-to-config-apache2-to-redirect-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!