I'm hosting a Laravel application on my server and have set up a subdomain to host it in my virtual host.
I have another subdomain on my server and after hours of playing around trying to set up an .htaccess file, I came up with the below which redirects all requests to www.mysite.net/example to my subdomain example.mysite.net (e.g www.mysite.net/example/12345 goes to example.mysite.net/12345)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(mysite\.net)$ [NC]
RewriteRule ^(.*)$ http://example.%1/$1 [R=301,L,NE]
I'm wanting to tweak this to work with Laravel, but it doesn't quite work the same considering Laravel is hosted out of the following path mysite.net/laravel/public rather than mysite.net/example.
How would I edit the above .htaccess to redirect all requests to mysite.net/laravel/public to laravel.mysite.net? I.e mysite.net/laravel/public/12345 would redirect to laravel.mysite.net/12345
Edit
Here is the Virtual Host I have added through Apache
<VirtualHost *:80>
ServerName laravel.mysite.net
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel/public>
Options -Indexes
</Directory>
</VirtualHost>
Place this rule as your very first rule inside /var/www/laravel/public/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?mysite\.net$ [NC]
RewriteCond %{THE_REQUEST} /laravel/public/(\S*)\s [NC]
RewriteRule ^ http://laravel.mysite.net/%1 [L,R=302]
You can use redirect rule of htaccess to redirect any directory to a different domain. Use the below code:
Redirect 302 /laravel/public/ http://laravel.mysite.net/
Please let me know if it helps you out.
I have to forward requests from internet client like this one :
https://www.app.com/AppServer?User=guest&ID=8PKX3Q2DT45&Type=laptop&Cmd=exec
to internal server with changing some parameters :
https://192.168.0.1/AppServer?User=guest&ID=NEW_ID&Type=NEW_TYPE&Cmd=exec
with Apache web server. NEW_ID and NEW_TYPE are static variables.
I've tried differents things with ProxyPass and RewriteCond %{QUERY_STRING} but without success.
RewriteEngine On
RewriteCond %{QUERY_STRING} .*User=(\w+).*&ID=(\w+).*&Type=(\w+).*&Cmd=(\w+).*$ [NC]
RewriteRule . HOST/AppServer?User=$1&ID=NEW_ID&Type=NEW_TYPE&Cmd=Exec [R=301,L]
ProxyPass HOST/AppServer
ProxyPassReverse HOST/AppServer
(URL has been replaced with HOST because of post restriction)
Also, I should be able to change header with RequestHeader (this work properly).
Could you help me to build the configuration ?
Kindest regards,
I answer to my question, this work :
#NEW UA
RequestHeader set User-Agent "NEW_UA"
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)OLD_ID(.*)OLD_DEVICE(.*)$
RewriteRule (.*) HOST/AppServer/?%1NEW_ID%2NEW_DEVICE%3 [P,L]
ProxyPass / HOST/
ProxyPassReverse / HOST
Thanks,
I am trying to remove the trailing slash from all of my URLs and redirect.
I've accomplished this here:
<Directory /var/www/html/ >
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ ${BaseUrl}/$1 [R=301,L]
</Directory>
However, I am also running a service locally and want to proxy to it visits /${ServiceRoot}.
ProxyPass /${ServiceRoot} http://localhost:${ServicePort}/
ProxyPassReverse /${ServiceRoot} http://localhost:${ServicePort}/
Each of these works fine individually. However, if I try to visit a URL like this: ${BaseUrl}/${ServiceRoot}/some/path/
The trailing slash is left on. I would like to force a redirect so the URL in the browser shows without the trailing slash.
Thanks in advance!
Just remove the L from the RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ ${BaseUrl}/$1 [R=301]
That should apply the rule to your ProxyPass directive. Just remember to check DirectorySlash if you plan to support directory listings, it will try to redirect directories to / and then back in a loop.
You probably got something like this in your apache config:
ProxyRequests On
ProxyVia On
<Proxy *>
Order deny,allow
Allow from xx.xx.xx.xx
</Proxy>
Then you have to add the following in order to proxy-pass all requests to www.olddomain.com/foo to www.newdomain.com/bar
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule /foo(.*)$ http://www.newdomain.com/bar/$1 [P,L]
What this does is:
When a request is made to host www.olddomain.com the RewriteRule will fire
This rule substitutes /foo to http://www.newdomain.com/bar/
The substitution is handed over to mod_proxy (P)
Stop rewriting (L)
Example result:
Browser is configured to use your apache as proxy server
It requests www.olddomain.com/foo/test.html
Your apache will rewrite this to www.newdomain.com/bar/test.html
It will request this page from the responsible webserver
Return the result to the browser as www.olddomain.com/foo/test.html
I want my users who type login.abcde.com to be redirected to https://client1.abcde.com and BLOCK all other HTTP URLs. So if someone types http://client1.abcde.com/thispage/isawesome.html, it will show some error message or a 404. So far I tried to add the following to my httpd.conf, but no success. I keep getting the Apache page saying: This page is used to test the proper operation of the Apache HTTP server after it has been installed. If you can read this page, it means that the web server installed at this site is working properly, but has not yet been configured.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^client1.abcde.com$ https://client1.abcde.com [L,R=301]
I am using Apache by the way, with WSGI. My virtualhost for port 80 is basically just:
<VirtualHost *:80>
ServerAdmin abisson#abcde.com
DocumentRoot /srv/www/abcde/html
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
</VirtualHost>
Since no one else has replied yet I will give it a try, it's untested but maybe it will give you some ideas on how to proceed. Maybe this could work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^login\.abcde\.com
RewriteCond %{REQUEST_URI} ^\/$
RewriteRule ^/(.+)$ https://client1.abcde.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^login\.abcde\.com
RewriteCond ${HTTPS} !=on
RewriteRule ^/(.+)$ [F]
Also it looks like in your case you have ^client1.abcde.com$ instead of ^login.abcde.com$ in your RewriteRule, not sure if changing it will make your solution work.
Maybe you could do the redirect in the VirtualHost as described in this RedirectSSL wiki page?
How do you redirect HTTPS to HTTP?. That is, the opposite of what (seemingly) everyone teaches.
I have a server on HTTPS for which I paid an SSL certification for and a mirror for which I haven't and keep around for just for emergencies so it doesn't merit getting a certification for.
On my client's desktops I have SOME shortcuts which point to http://production_server and https://production_server (both work). However, I know that if my production server goes down, then DNS forwarding kicks in and those clients which have "https" on their shortcut will be staring at https://mirror_server (which doesn't work) and a big fat Internet Explorer 7 red screen of uneasyness for my company.
Unfortunately, I can't just switch this around at the client level. These users are very computer illiterate: and are very likely to freak out from seeing HTTPS "insecurity" errors (especially the way Firefox 3 and Internet Explorer 7 handle it nowadays: FULL STOP, kind of thankfully, but not helping me here LOL).
It's very easy to find Apache solutions for http->https redirection, but for the life of me I can't do the opposite.
Ideas?
This has not been tested but I think this should work using mod_rewrite
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
Keep in mind that the Rewrite engine only kicks in once the HTTP request has been received - which means you would still need a certificate, in order for the client to set up the connection to send the request over!
However if the backup machine will appear to have the same hostname (as far as the client is concerned), then there should be no reason you can't use the same certificate as the main production machine.
For those that are using a .conf file.
<VirtualHost *:443>
ServerName domain.com
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/domain.crt
SSLCertificateKeyFile /etc/apache2/ssl/domain.key
SSLCACertificateFile /etc/apache2/ssl/domain.crt
</VirtualHost>
Based on ejunker's answer, this is the solution working for me, not on a single server but on a cloud enviroment
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If none of the above solutions work for you (they did not for me) here is what worked on my server:
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
all the above did not work when i used cloudflare, this one worked for me:
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
and this one definitely works without proxies in the way:
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
this works for me.
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
Redirect "https://www.example.com/" "http://www.example.com/"
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# ...
</VirtualHost>
be sure to listen to both ports 80 and 443.
It is better to avoid using mod_rewrite when you can.
In your case I would replace the Rewrite with this:
<If "%{HTTPS} == 'on'" >
Redirect permanent / http://production_server/
</If>
The <If> directive is only available in Apache 2.4+ as per this blog here.
None of the answer works for me on Wordpress website but following works ( it's similar to other answers but have a little change)
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If you are looking for an answer where you can redirect specific url/s to http then please update your htaccess like below
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/(home/panel/videos|user/profile) [NC] # Multiple urls
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /(home/panel/videos|user/profile) [NC] # Multiple urls
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It worked for me :)
As far as I'm aware of a simple meta refresh also works without causing errors:
<meta http-equiv="refresh" content="0;URL='http://www.yourdomain.com/path'">