Redirect httpd file - regex

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

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.

Subdomain redirect from https to http doesn't work

I want rewrite link at apache. Apache work with ssl. And my application return some requests as https link. Example:
https://www-prd.euro1.sdl.electric.com/uk/en/assets-re1/js/adapt-img.js
needs to be redirected to:
http://www-prd.euro1.sdl.electric.com/uk/en/assets-re1/js/adapt-img.js
Just replace https on http.
My last variant:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^https://electric\.com [NC]
RewriteRule ^(.*)$ http://electric.com/$1 [L,R=301]
But this not help me.
Config file:
ProxyPass /server-status !
Listen 443
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300
Mutex default
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin
ErrorLog logs/ssl_error_log
TransferLog logs/ssl_access_log
LogLevel warn
SSLProtocol all -SSLv2
SSLHonorCipherOrder On
SSLCipherSuite ALL:!ADH:!EXP:!LOW:!RC2:!3DES:!SEED:!RC4:+HIGH:+MEDIUM
SSLCertificateFile /etc/httpd/ssl/apache.crt
SSLCertificateKeyFile /etc/httpd/ssl/apache.key
<VirtualHost *:443>
ServerName fe1-app.projects.com
SSLEngine on
AllowEncodedSlashes On
RewriteEngine on
RewriteCond %{HTTP_HOST} ^https://www-prd\.euro1\.sdl\.electric\.com [NC]
RewriteRule ^(.*)$ http://www-prd.euro1.sdl.electric.com/$1 [L,R=301]
</VirtualHost>
ProxyPassMatch (.*) http://localhost:20180 nocanon
ProxyPreserveHost On
ProxyStatus On
SetEnv proxy-nokeepalive 0
https:// should not prefix HTTP_HOST, try this:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www-prd\.euro1\.sdl\.electric\.com [NC]
RewriteRule ^(.*)$ http://www-prd.euro1.sdl.electric.com/$1 [L,R=301]
If you need js just to be loaded (no redirect) try this:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www-prd\.euro1\.sdl\.electric\.com [NC]
RewriteRule ^(.*)$ http://www-prd.euro1.sdl.electric.com/$1 [L]

How to redirect to other host using mod_rewrite in Apache

I need to create Rewrite Rule for mod_rewrite in Apache but it's really hard for me to understand this whole syntax.
I have links such as:
http://www.example.com/0001/images/image1.gif
and I need them to redirect it to completely other server like:
http://127.127.127.127/0001/images/image1.gif
Is this achievable?
On example.com 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 %{HTTP_HOST} ^(www\.)example\.com$ [NC]
RewriteRule ^ http://127.127.127.127%{REQUEST_URI} [R=301,L]

Apache redirect based on host and uri

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$

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>