I am trying to redirect visitors of alldomain.com to the domain newdomain.com it does redirect however I want that when the user open's alldomain.com the data should be of newdomain.com however the top url should be alldomain.com
My Current HTACCESS:
RedirectMatch .* http://www.newdomain.com
I believe the solution to this consist of two parts: correct .htaccess, and using mod_proxy on your Apache server:
Uncomment these lines in httpd.conf (and restart Apache!):
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Add the following lines to your .htaccess:
RewriteEngine on
RewriteRule .* http://www.newdomain.com/ [P]
ProxyPassReverse / http://www.newdomain.com/
The key here is that the [P] flag in the RewriteRule tells Apache to use mod_proxy (which you enabled earlier), and the ProxyPassReverse makes sure that any links from the new domain are properly "attributed" to the old domain as well. I think that does it, but I can't test... Let me know whether this works for you!
Related
I know there are quite some questions out there about this issue but I have not yet been able to find the exact way how to do a rewrite in my case:
Having an application available at https://example.com/dev/myapp/#/?query=1234, I would like to allow an alternative access using this URL: https://example.com/dev/myapp/1234 without creating a sub-directory /1234.
Using the htaccess, I would like that the latter URL is rewritten/ forwarded to the former URL.
What I have tried:
RewriteEngine On
RewriteRule ^myapp/([^/])?$ myapp/index.html?query=$1 [L]
will forward
https://example.com/dev/myapp/1234
to
https://example.com/dev/myapp/1234#/
And
RewriteEngine On
RewriteRule ^(?!myapp/)([^/])?$ /dev/myapp?query=$1 [L,R=302]
will forward
https://example.com/dev/myapp/1234
to
https://example.com/dev/myapp/?query=1234#/
I am somehow stuck with getting the syntax right. Any help?
If I good understood, I create regex with group contain the numbers and re-used it for your query parameter, with NE flag for "noescape" output and keep "#" syntax, the doc.
A. You can use in your virtualhost, the code below, the url will changed.
RewriteEngine On
RewriteRule ^/dev/myapp/([0-9]+)$ /dev/myapp/\#/?query=$1 [L,R=301,NE]
B. If you need to keep url of source, enable in httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
And you can use in your virtualhost, the code below. (P for proxy)
SSLProxyEngine on
RewriteEngine On
RewriteRule ^/dev/myapp/([0-9]+)$ /dev/myapp/\#/?query=$1 [P]
I hope that will help you
I have an app deployed to Elastic Beanstalk whose Tomcat container uses Google OpenID Connect for authentication. I want to redirect all http requests to https, for which I have the following mod_rewrite configuration in a file in .ebextensions -
files:
"/etc/httpd/conf.d/ssl_rewrite.conf":
mode: "000644"
owner: root
group: root
content: |
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
Google OAuth2 credentials console has https://example.com/j_security_check as an authorized redirect URL. The configuration works fine when either example.com or https://example.com is requested, whereupon the app is redirected to the mentioned authorized URL.
However, when http is explicitly requested - http://example.com - the app is being redirected to https but port 80 is still being used. The authorized redirect URL then becomes https://example.com:80/j_security_check and I get Error: redirect_uri_mismatch.
How can I redirect explicit http requests to https with the port changed to 443? The main goal is to match the mentioned authorized redirect URL. If possible, I'd like to implement this with the .ebextensions configuration file or a similar solution.
Can you something like this. If it got worked I will give you the explanation.
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
The problem was not with the rewrite rule. The file had to be placed in a specific path within .ebextensions for it to work in Tomcat 8. The configuration files had to be setup differently too. Most examples provided were not for Tomcat so I ended up putting them in the wrong location.
What worked -
In /.ebextensions/httpd/conf.d/myconf.conf, place -
LoadModule rewrite_module modules/mod_rewrite.so
and in /.ebextensions/httpd/conf.d/elasticbeanstalk/00_application.conf, place -
<VirtualHost *:80>
<Proxy *:80>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/ retry=0
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost on
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
Take note of the use of .conf files instead of .config. This is important!
Also, the redirection that I was getting was not genuine. I was not paying close attention, because when I requested example.com, the browser cache was serving me https://example.com. It was not actually redirecting an http request to https.
I'm trying to use .htaccess to send all traffic to https. I'm using the example in this answer: Need to redirect all traffic to https
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This isn't redirecting anything for me. I placed it in var/www but it doesn't seem to have any effect. What did I do wrong?
Try [R,L] at the end of the rewrite rule:
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
R redirects to the provided link, and L breaks the flow if condition is met.
For reference you can see:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
If that's the only rules you have in your htaccess file and it's in your document root then you need to check a few things because the rules are fine.
Make sure mod_rewrite is loaded. There should be a line in your httpd.conf file that looks something like:
LoadModule rewrite_module modules/mod_rewrite.so
Make sure it's uncommented.
Make sure the directory that your htaccess file is in (should be your document root) is allowed to override server settings via htaccess. In your vhost or server config, there should be something along the lines of
<Directory "/var/www/">
AllowOverride All
... (some other stuff)
</Directory>
Make sure the AllowOverride is at least FileInfo
Make sure your document root is actually where your htaccess file is in. Your vhost config should have a line like:
DocumentRoot /var/www/
Make sure the document root is for the right vhost. If you have separate vhosts for SSL and non-SSL, make sure the htaccess file is in the document root for the non-SSL vhost.
Is it possible to internally redirect (so url won't change in address bar) with mod_rewrite to different port on same host?
Eg
http://host.com:8080 -> http://host.com:9999/myapplication/?param=val
1, Enable mod_proxy
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
2, You should configure apache for vhost :
<VirtualHost *:8080>
....
ProxyPass / http://host.com:9999/myapplication/?param=val
ProxyPassReverse / http://host.com:9999/myapplication/?param=val
</VirtualHost>
3, Setup also VHost on port 9999
More info here:
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://www.apachetutor.org/admin/reverseproxies
Elaboration on the mod_proxy solution with [P], the proxy flag:
Enable modules mod_proxy and mod_proxy_http:
a2enmod proxy proxy_http
Without these two enabled, you'd later get a 300 Forbidden status and the error message "AH00669: attempt to make remote request from mod_rewrite without proxy enabled" in the logs.
Place the following into the Apache2 vhost config section for the forwarding host:
<VirtualHost *:8080>
…
RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_URI} !^/
RewriteRule .* - [R=400,L]
RewriteRule (.*) http://host.com:9999/myapplication/$1?param=val [P,L]
…
</VirtualHost>
This includes a technique by Steve Webster to prevent malicious URL crafting, explained here. Not sure how to deal with appending the GET parameter in this context, though.
Restart Apache2:
sudo service apache2 restart
I want to redirect "http://localhost/b.html" --> "http://localhost/a.html"
I tried RewriteRule for that. But somehow it is not working for me.
I am using apache2 and my httpd.conf contains:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteRule ^/b.html$ http://localhost/a.html
When I tri "http://localhost/a.html" It shows me the web page. But when I triend "http://localhost/b.html" apache2/error_log says "file does not exist: b.html"
Does any setting missing to enable rewrite_module?
The problem is in your RewriteRule. this should work:
RewriteEngine On
RewriteRule ^/b.html$ /a.html [L]
the rule match (^b.html$) must not include the starting slash. (this works in .htaccess, but not in server config)
the rewrite target should be a relative URI if possible (i.e. on the same host)
the rule should end with a directive "what to do" - in this case [L]eave processing (no more rules will be processed)
Have you checked whether in your Apache configuration file (most likely httpd.conf) the directive for the Alias or VirtualHost section:
AllowOverride All
I had the same problem of modrewrite not working because I had it off:
AllowOverride None
Good luck.
Do you have it inside the virtualhost section?
<VirtualHost *:80>
RewriteEngine On
RewriteRule ^/b.html$ /a.html
</VirtualHost>
It works now. Had to do two things:
Change "AllowOverride None" in /etc/apache2/sites-available/default to "AllowOverride All".
Put the rewrite rule in /var/www/.htaccess instead of httpd.conf
I am not sure why it does not works in httpd.conf. But it works after doing the above two things.