Apache redirect based on host and uri - regex

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$

Related

Apache Rewritemap not being read?

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.

Redirect httpd file

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

apache proxypass from subdomain

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.

htaccess RedirectMatch regular expression for URL?

On my Wordpress blog, I used to have a plugin that I no longer need. The plugin used to create a bunch of URLs that looked like this:
http://tambnguyen.com/manage-subscriptions?srp=532&sra=s
with the postID being 532. How do I redirect the query strings so that the above URL will redirected to:
http://tambnguyen.com/?p=532
I've tried a few method without any luck (there's an optional "/" after "manage-subscriptions"
<IfModule mod_rewrite.c>
RedirectMatch 301 ^/manage-subscriptions/?\?srp=(\d{1,5})(.*)$ http://tambnguyen.com/\?p=$1
</IfModule>
Please help. Thanks!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^srp=([^&]+) [NC]
RewriteRule ^manage-subscriptions/?$ /?p=%1 [L,R=301,NC]

Redirect non-www URL to www URL in conjunction with other rules

Redirecting a visitor who hits http://example.com to http://www.example.com isn't terribly difficult. But how is it done in conjunction with a RewriteRule that directs all page requests through "index.php"?
RewriteRule !\.(gif|jpg|png|css|js|php|ico|xml)$ /index.php
You just need to make sure that those rule, that cause an external redirect, appear before those, that cause internal rewrites. So simply:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule !\.(gif|jpg|png|css|js|php|ico|xml)$ /index.php
See the answer for this post, just do the opposite.
<VirtualHost *:80>
ServerName example.com/
RedirectPermanent / http://www.example.com/
</VirtualHost>