Apache HTTP_URI redirect everything but sertain urls that query_string - apache

We would like to redirect all trafic to our new page. We would still like that ceratin urls are still accessible on our old page, because these are used by our partners and are not yet ready to migrate.
The problem i'm having is with QUERY_STRING parameters.
My rules so far:
RewriteCond %{REQUEST_URI} !^/endfile.php
RewriteCond %{REQUEST_URI} !^/sites/default/files/pdffolder/
RewriteCond %{REQUEST_URI} !^/insure/agency
# RewriteCond %{REQUEST_URI} !^/content/insurance1?szs=000029&wssl=1
# RewriteCond %{REQUEST_URI} !^/text/insurance2?wssl=1&zst=enter
RewriteRule (.*) https://www.new-domain.eu/ [R=301,L]
The code works for the fist 3. I cant get it to work for the 4th and 5th rule. I currently have the commented out.
I tried using QUERY_STRING rules, without success. Is htere any way to use AND in rewrite condition?
Any ideas?

You cannot match query string using REQUEST_URI. Use THE_REQUEST to match both URI and query:
RewriteCond %{REQUEST_URI} !^/endfile\.php [NC]
RewriteCond %{REQUEST_URI} !^/sites/default/files/pdffolder/ [NC]
RewriteCond %{REQUEST_URI} !^/insure/agency [NC]
RewriteCond %{THE_REQUEST} !\s/+content/insurance1\?szs=000029&wssl=1[&\s] [NC]
RewriteCond %{THE_REQUEST} !\s/+text/insurance2\?wssl=1&zst=enter[&\s] [NC]
RewriteRule ^ https://www.new-domain.eu/? [R=301,L]
Also note use of trailing ? in target to strip off any existing query string.

Related

htaccess strip part of string in url

I have a rewrite rule that redirects a specific path like
http://mev-hongkong.com/product/boardwalk-long-shorts/ to https://mothersenvogue.com.hk/product/boardwalk-long-shorts/
So my rule looks like this
RewriteCond %{HTTP_HOST} ^(www\.)?mev-hongkong\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^product/boardwalk-long-shorts(/.*)?$ https://mothersenvogue.com.hk/product/boardwalk-long-shorts$1 [L,R=301,NC,NE,QSD]
I also have a URL of the same path but the difference is that it has a "-2" on the end like
http://mev-hongkong.com/product/boardwalk-long-shorts-2/
What rule should I include on the original URL for me to be able to catch the "-2" on my old domain?
I tried making my RewriteRule like this (adding .* before the slash) but it didn't work.
RewriteRule ^product/boardwalk-long-shorts(.*/.*)?$ https://mothersenvogue.com.hk/product/boardwalk-long-shorts$1 [L,R=301,NC,NE,QSD]
Thanks!
You may use this rule:
RewriteCond %{HTTP_HOST} ^(www\.)?mev-hongkong\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^product/boardwalk-\w+-shorts(?:-[^/]+)?(/.*)?$ https://mothersenvogue.com.hk/product/boardwalk-long-shorts$1 [L,R=301,NC,NE,QSD]
(?:-[^/]+)? makes -2/ an optional match

How to redirect a URL with a querystring

I've moved a website to a new domain and I've used RewriteRule to redirect a number of my URLs.
I'm stuck trying to redirect URLs with query strings in them?
RewriteRule /news/archive.php?cat=economic-impact http://www.new-website.com/faqs/economic-impact [R=301,L]
RewriteRule /news/archive.php?cat=housing-need http://www.new-website.com/faqs/housing-need R=301,L]
RewriteRule /news/archive.php?cat=infrastructure http://www.new-website.com/faqs/infrastructure R=301,L]
Further up the htaccess file I'm using this to redirect the domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-website.com$
RewriteRule (.*)$ http://www.new-website.com/$1 [R=301]
When I visit:
old-website.com/news/archive.php?cat=economic-impact
it redirects to
new-website.com/news/archive.php?cat=economic-impact
Which 404s, I need to to redirect to:
new-website.com/faqs/economic-impact
Can anyone help?
You need to add a blank query string on your rewrite to truncate any existing query strings.
The below should do the job:
RewriteRule (.*)$ http://www.new-website.com/$1? [R=301]
In addition, the way you're checking the existing query strings won't work. You'll need to check them separately:
RewriteCond %{QUERY_STRING} "cat=(economic-impact|housing-need|infrasructure)"
RewriteRule news/archive.php http://www.new-website.com/faqs/%1? R=301,L]
The %1 will take a backreference from a regex group match in the RewriteCond

Can't get htaccess rule to work: force SSL on all pages, force non-SSL on two specific pages

I am no htaccess expert, but after Googling for two hours I gave up. Maybe you can help me?
I have my entire site on SSL. However, I have two pages that reference non-secure dynamic content from elsewhere. I need these to be on http instead of https.
The first part of my rules work. All the site is forced to SSL except for those two pages. However, the last part doesn't: force those two pages to non-SSL. It is probably very stupid but does anyone see where I go wrong?
#add www. if missing WORKS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
#force SSL/https WORKS
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/webshop2/localize\.php
RewriteCond %{REQUEST_URI} !^/webshop2/layoutstripper\.php
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#force http DOES NOT WORK
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/webshop2/localize\.php [NC]
RewriteCond %{REQUEST_URI} ^/webshop2/layoutstripper\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You need an [OR] flag in your second SSL rule. The 2 conditions you have essentially say:
the request must be HTTPS
the URI must start with: /webshop2/localize.php
the URI must start with: /webshop2/layoutstripper.php
As you can see, the last 2 conditions will always fail, as the request can't be BOTH at the same time. If you add an [OR] flag in there, it makes it true if the URI is one or the other:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/webshop2/localize\.php [NC,OR]
RewriteCond %{REQUEST_URI} ^/webshop2/layoutstripper\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

mod_rewrite country code into URI

I need to redirect the following URL which has the country code at the start:
http://xx.domain.com/test.html
into:
http://xx.domain.com/xx_en/test.html
I came up with the following, of course this will have an infinite loop:
RewriteCond %{REQUEST_URI} !/xx_en/ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteRule (.*) /%1_en/$1 [R=301,L]
So I added the first line which, I think, means if you don't see /xx_en/ in the request_uri then do what follows.
Any idea what I'm doing wrong?
Use something like the following set of rules:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1_en/.*$
RewriteRule ^(.*)$ /%1_en/$1 [R=301,L]
You can understand it like: for all requests to all 3rd level domains under domain.com which doesn't start with /<3rd level>_en/ redirect to it...

mod_rewrite rules, how to test for 'not a string'?

I have a blog installed in www.foo.com/wp/ and would like all requests that go to any page that doesn't start with /wp/ to be redirected to www.foo.com/wp/ (without preservation of the URL).
E.g.:
www.foo.com/bar/baz/hello.gif > www.foo.com/wp/
Also, at the same time, I need the URL to have www. added if it doesn't (which might be where my rules are getting confused)
At the moment, I have:
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.foo.com/$1 [R=permanent]
RewriteRule ^(?!wp)/(.*)$ http://%{HTTP_HOST}/wp/($2) [R=permanent]
But I think this is currently completely broken.
Help much appreciated.
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.foo.com/$1 [R=permanent,L]
RewriteRule ^(?!wp/)(.*) http://%{HTTP_HOST}/wp/$1 [R=permanent]
The path portion of the URL being matched never has a leading slash, and you don't need to anchor a .*.
L modified prevents rewriting from continuing after you arrive at your first redirect.
Negative-lookbehind (?!) is not a capturing construct, and you substitute in captured patterns by writing $1, not ($1).
Edit: Apparently for OP the last rule doesn't work, failing to exclude the cases that begin with /wp/, which makes no sense to me but whatever. Here's an attempt at a workaround:
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.foo.com/$1 [R=permanent,L]
RewriteCond %{REQUEST_URI} !^/wp/
RewriteRule (.*) http://%{HTTP_HOST}/wp/$1 [R=permanent]
Try these rules:
RewriteCond %{HTTP_HOST} !^www.exmaple.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /wp/
RewriteRule ^wp/(.*) /$1 [R=301,L]
RewriteRule !^wp/ wp%{REQUEST_URI} [L]
The first rule is for the host name. The second is to remove the /wp path prefix externally. And the third is to add the prefix again for internal redirection.