I want to proxypass in apache2 from subdomain to other ports like this
http://test1.example.com -> http://test1.example.com:6543
Is there anyway to write config file with not specific with this subdomain but all of the subdomain
in anyway like http://.*.example.com -> http://.*.example.com:6543
and http://.*.example.com/first/second -> http://.*.example.com:6543/first/second
Here is my config that
<VirtualHost *:80>
ServerName example.com
ProxyPassMatch ^([^.]+)\.example\.com(.*) http://$1.example.com:6543/$2
ProxyPassReverse / http://example.com
</VirtualHost>
You'll need mod_rewrite for this. It can proxy with [P]. Use it as follows (VirtualHost tags removed in order to satisfy syntax highlighting):
ServerName example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*) http://%1.example.com:6543/$1 [P]
%1 is the subdomain, matched in the condition, $1 is the path, matched by the rule.
You can also do this without the RewriteCond, using just
RewriteRule ^(.*) http://%{HTTP_HOST}:6543/$1 [P]
I used the extra step above to make it more informative, e.g. in the event you wanted to use the subdomain in the path.
Related
I have a simple key value map file which converts olduserid's to new userid's
The objective is to pullup a member profile page from the old site and redirect to tyhe newsite where the member has a new userid.
My virtualhost config file is like this
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAlias *.example.com
DocumentRoot /home/example/www
AllowEncodedSlashes NoDecode
<Directory /home/example/www>
Options Indexes FollowSymLinks MultiViews
Require all granted
AllowOverride All
</Directory>
CustomLog /var/log/httpd/example.com-access.log combined
ErrorLog /var/log/httpd/example.com-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel error
RewriteEngine on
RewriteMap profiles "txt:/home/example/www/userMap.txt"
RewriteCond %{SERVER_NAME} =*.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]`
The in root directory my .htaccess looks like this
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{QUERY_STRING} ^$ RewriteRule ^member/([0-9])+$ https://www.newxample.com/member/$%7Bprofiles:$1%7D" [NC,L]
The mapfile looks like this but larger
5 1
3583 7657
3584 7658
3585 703
The permissions for the map file and it's location are 777
I have tried so many ways to write the rules and condition but am getting nowhere.
The redirect works but it does not include the values of the newuseris. It's simply null empty nada!
Any help would be greatly appreciated.
tl;dr I'm guessing you haven't defined the rewrite map (and possibly other config) in the <VirtualHost *:443> (HTTPS) container AND/OR you are only capturing the first digit of the old user ID.
The virtualhost config you've posted is for port 80 (HTTP) only. Which is redirected to HTTPS (port 443). There's not much point defining the RewriteMap in <VirtualHost *:80>, since you will also need to define it again in <VirtualHost *:443>. The same applies to granting access and allowing .htaccess overrides etc.
Basically, the vHost:80 container really just serves to redirect to HTTPS, then most of the config is defined in vHost:443.
Since you are redirecting to HTTPS directly in the vHost container, the .htaccess file is only going to be processed (if at all) when the request is already over HTTPS.
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{QUERY_STRING} ^$ RewriteRule ^member/([0-9])+$ https://www.newxample.com/member/$%7Bprofiles:$1%7D" [NC,L]
The formatting/encoding of the code dump in your question is messed up (I'm assuming this is just a formatting issue with your question and not in your actual code), however, your regex that is capturing the old user ID is only capturing the first digit, so the lookup will likely fail (or return the wrong result):
^member/([0-9])+$
Should be:
^member/([0-9]+)$
Or, use a shorthand character class, eg. ^member/(\d+)$
Aside:
RewriteCond %{SERVER_NAME} =*.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
By default, SERVER_NAME is the same as HTTP_HOST, ie. the value of the Host header used in the request. This is what you would seem to be assuming here, however, it is never equal to *.example.com (which is simply a wildcard alias). There shouldn't be a need to check the requested Host here since for the request to be at this point in the config, it must have already passed the ServerName / ServerAlias check. (This is assuming this is not the "default" vHost.)
The three RewriteCond directives are therefore redundant.
By reverse IP search, I found that there are two other domain names (not mine) bound to my server's IP, which hosts my website. I just want to stop visitors from visiting my website by these two domain names. have tried many methods but all of them do not works at all. Here are some methods I have tried.
Add these lines at beginning of /etc/apache2/sites-available/haichaoyu.com.conf:
<VirtualHost *:80>
ServerName catchall
<Location />
Order Deny,Allow
Deny from all
</Location>
</VirtualHost>
<VirtualHost *:443>
ServerName catchall
<Location />
Order Deny,Allow
Deny from all
</Location>
</VirtualHost>
Add these lines in /var/www/haichaoyu.com/public_html/.htaccess
RewriteCond %{HTTP_HOST} !^haichaoyu\.com$
RewriteCond %{HTTP_HOST} !^www\.haichaoyu\.com$
RewriteCond %{HTTP_HOST} !^former\.haichaoyu\.com$
RewriteRule ^ - [F]
In addition, I have forced all http traffic to https traffic. And now I have three domains: haichaoyu.com, www.haichaoyu.com, former.haichaoyu.com.
Could anyone give me some help? Thanks in advance!
I tried editing httpd file using all the solutions i found to redirect a https://pc.web.com/MyWeb/MyWebPortal.portal;jsessionid=jZLxT04pfQ (example website) to http://pc.web.com.
But none of the solutions are working! can anyone could guide me on this?
<VirtualHost *:80>
ServerName pcnow.web.com
DocumentRoot "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"
RewriteCond %{pc.web.com} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
even redirect is also not working!!
There are multiple mistakes in your configuration
<VirtualHost *:80>
ServerName pcnow.web.com
DocumentRoot "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs"
RewriteCond %{pc.web.com} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
...
First, you want to redirect https request to http so your virtual host should probably listen to port 443 and you should set the certificate and the appropriate SSL configurations in it.
Second, you have a typo in the server name ServerName pcnow.web.com should be ServerName pc.web.com.
Third, your RewriteCond is invalid.
The RewriteCond syntax should be in the form of:
RewriteCond TestString CondPattern [flags].
So in your case the TestString will be something like: %{REQUEST_URI}.
And the CondPattern should be a regex matching what all URIs that need to be redirected, for example: ^/MyWeb/MyWebPortal\.portal;jsessionid=.*.
Or all together:
RewriteCond %{REQUEST_URI} ^/MyWeb/MyWebPortal\.portal;jsessionid=.*.
And last, the RewriteRule second parameter contracting an invalid URL to redirect. The reason is it concatenating the value of the RewriteRule expression match with the value of the RewriteCond condition match which was written incorrectly.
The RewriteRule rule should as follow: RewriteRule .* http://pc.web.com [R=301,L].
you can take a look over here for more details: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
I want to change the URL from:
http://example.com/Portfolios/iPhone/app
To:
http://example.com/iPhone/app
And same for all URLs like:
example.com/Portfolios/iPad/app
To:
example.com/iPad/app
And from:
example.com/Portfolios/xyz/app
To:
example.com/xyz/app
I have tried a lot but nothing is working for me.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^Portfolios(/.*|)$ $1 [L,NC]
</IfModule>
Enable mod_rewrite and .htaccess through Apache config and then put this code in your .htaccess under DOCUMENT_ROOT directory:
RewriteEngine On
RewriteRule ^Portfolios/(.*)$ /$1 [L,NC,R=302]
Explanation: Above rule is matching URL pattern that starts with Portfolios and have somthing like /Portfolios/xyz/app and puts xyz/app in $1. It makes an external redirection to /$1 i.e. /xyz/app.
These are the flags used:
L - Last Rule
NC - Ignore (No) Case comparison
R - External redirection (with 302)
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
You can also set your root directory as /var/www/Portfolios instead of /var/www/ in /etc/apache2/sites-enabled by writing DocumentRoot line as
DocumentRoot /var/www/Portfolios
instead of
DocumentRoot /var/www/
and also this line
< Directory /var/www/ > changed to
< Directory /var/www/Portfolios/ >
I have a fairly simple redirect rule setup in my Apache vhost.
<VirtualHost *:80>
ServerName mobile.foo.com
ServerAlias *.foo.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.foo\.com$
RewriteRule ^(.*)$ http://mobile.bar.com/foo [R=301,L]
</VirtualHost>
I want to add another condition based on the request uri. The redirect above should not be performed if the request uri equals google.html. I guess another rewrite condition is needed based on the request uri variable. Any ideas on how the regex should look like?
Should look like this:
RewriteCond %{REQUEST_URI} !^/google\.html$