How can I use URL rewriting in .htaccess to redirect to different domains depending on the URL?
Examples:
http://ONE/ to http://TWO/
http://ONE/some_content to http://THREE/some_content
This ought to work if you want to redirect the client:
# http://ONE/ to http://TWO/
RewriteCond %{HTTP_HOST} =one
RewriteRule ^$ http://two/ [R,L]
# http://ONE/some_content to http://THREE/some_content
RewriteCond %{HTTP_HOST} =one
RewriteRule ^(.+)$ http://three/$1 [R,L]
If you prefer to proxy the requests, change the R flag to a P instead.
Related
I want to ensure that a small number of URLs in my application are always served over SSL; the rest should always be served over HTTP.
My current mod_rewrite ruleset goes like this:
RewriteCond %{HTTPS} off
RewriteRule ^/?account.html$ https://example.com/account.html [L]
RewriteRule ^/?checkout.html$ https://example.com/checkout.html [L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/account.html$
RewriteCond %{REQUEST_URI} !/checkout.html$
RewriteRule (.*) http://example.com/$1 [L]
The first file in each RewriteCond works OK (account.html in this example). The 2nd file doesn't work - the server is "redirecting in a way that will never complete".
Any ideas on how to make this work? In production there'll be more than 2 SSL-only URLs, likely 7 - 10.
Thanks
You must use R flag to redirect url
RewriteEngine On
# redirect account.html or checkout.html (if http) to https
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(account|checkout)\.html$ [NC]
RewriteRule ^ https://%{HTTP_HOST}/%1.html [R,L]
# redirect other urls (if https) to http
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(account|checkout)\.html$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]
Note: i used R flag, which performs a 302 redirect by default. Feel free to replace [R,L] by [R=301,L] when it works (301 redirect is a permanent redirect and is cached by browsers)
I have already searched this site but my workaround does not work.
Basically, I need all pages to have https turned on except for 1 page that has a url like this:
domain.com/begin/index.php?pageid=130&usertype=1&building=2
If the page has the Querystring parameter pageid=130 I do not want https turned on.
My .htaccess file looks like this:
RewriteEngine On
#HTTPS OFF on pageid 130
RewriteCond %{HTTPS} on
RewriteCond %{QUERY_STRING} (^|&)pageid=(130)($|&)
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
#FORCE HTTPS ON ALL PAGES except for pageid 130
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !(^|&)pageid=(130)($|&)
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
LOL! Small mistake but hard to spot: you still redirect to https ;). Just remove the 's'.
Otherwise may redirect for sure and stop the rewrite process = Last = L.
RewriteEngine On
# HTTPS Off on pageid 130
RewriteCond %{HTTPS} On
RewriteCond %{QUERY_STRING} (^|&)pageid=130($|&)
# (!) Remove the "s":
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [QSA,NC,L]
# Force HTTPS on all pages but the one QS 'pageid=130'
RewriteCond %{HTTPS} Off
RewriteCond %{QUERY_STRING} !(^|&)pageid=130($|&)
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,NC,L]
What should look isapi rewrite rule which can change https to http for a specific page, ex:
https://www.portal.com/mypage to http://www.portal.com/mypage
Regards
Try using:
RewriteCond %{HTTP:Host} (.*)
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} (/mypage)
RewriteRule .? https://%1%2 [R,L]
I would like to redirect to https using mod_rewrite only if certain conditions are met:
If the URL does NOT contain the word 'administrator'
AND the URL DOES contain the string 'xyz' (in any part of the URL, including the querystring)
This does not seem to work:
RewriteCond %{REQUEST_URI} xyz [NC,OR]
RewriteCond %{QUERY_STRING} xyz [NC]
RewriteCond %{REQUEST_URI} !administrator [NC]
ReWriteCond %{HTTPS} != on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R,L]
Try this rule:
RewriteCond %{THE_REQUEST} !administrator
RewriteCond %{THE_REQUEST} xyz
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Testing the request line in THE_REQUEST is easier as it contains both the path and query. But make sure your xyz is not part of the method or HTTP version.
I ended up using a coding solution as I could not get it to work with mod_rewrite. :(
RewriteEngine On
RewriteRule ^/ai?$ /Market/publish.jsp [QSA,L,PT]
RewriteRule ^/ar?$ /Market/MailDispatch [QSA,L,PT]
RewriteCond %{HTTP_HOST} !^web\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://web.example.com/$1 [L,R]
#How skip www\. to web\. for this 1 ?
#RewriteRule ^/vi/?([0-9]+)\.htm$ /Market/vi.do?id=$1 [PT,L]
RewriteRule ^/li /Market/list.do [QSA,PT,L]
RewriteRule ^/vi/locations.jsp /Market/locations.jsp [PT,L]
ErrorDocument 404 /notfound.html
Nearly undoable(?) I try http://example.com/vi/{N}.htm should redirect to http://web.example.com/vi/{N}.htm where N is dynamic ID.
Seen mod_rewrite with subdomain and url pattern
There is no clear way to make eg http://example.com/vi/1096.htm pass up to next version http://web.example.com/vi/1096.htm where number is dynamic. I tried
A rule with the following scheme should do it:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/vi/\d+\.htm$ http://web.example.com%{REQUEST_URI} [L,R=301]
It’s important to put this rule in front of those rules that do an internal redirect. Otherwise an already internally rewritten URL could be rewritten externally.
If you want to use this rule in a .htaccess file, remove the leading slash from the pattern in RewriteRule.