How to avoid apache transfering urlencoded chars to lowercase when doing a 301 redirect - apache

I ran into this problem when I tried to redirect all contents from one site to another. My http server is apache, and Im using mod_rewrite to do the job.
So I added these codes to httpd.conf:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/(.*) http://www.example2.com/$1 [R=301,L]
Everthing seems to be good after Ive done that, however when I visit a urlencoded url such as http://www.example.com/?word=%E5%A4%A9 , apache redirects me to http://www.example2.com/?word=%e5%a4%a9 , as you see, all the urlencoded chars are trasnfered to lowercase which makes me impossible to infrom search engine about my domain change because search engine asks for a exact match of new and old URI(case sensitive).
BTW, when I use urlencode() function in PHP, the results it returned is automatically uppercased, so the standard URL should be something like this %E5%A4%A9.
I googled a lot and got nothing so I had to come here to ask for help, any help will be appreciated, thanks in advance.

Not sure if this works, but try
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/(.*) http://www.example2.com/$1?%{QUERY_STRING} [R=301,L]
or
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/(.*) http://www.example2.com/$1 [R=301,L,NE]

Related

htaccess redirect to a specific url on same domain (without looping)

I really hope you can help me out (it is driving me crazy).
I've tried dozens of setups and nothing seems to work, Googled myself dizzy and tried numerous different setups, but it all seems to result in a loop or a server error.
This is what needs to happen:
I have a site with multiple domains attached to it. What I need is that when someone visits the website via the "domain.co.uk"-domain, a redirect to the correct language parameters (among others) takes place.
To be very specific: when visiting via "www.domain.co.uk" the visitor must be redirected to "www.domain.co.uk?lang=en&noredir=1&currency=3"
I've made sure that the www is present with this:
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
The trouble is (I think) the redirect within the same domain without causing a loop.
I've tried stuff like this, but with no result:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]
Hope you can help,
Cheers!
This will cause a loop:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]
Because you're only checking the host header. Every time the redirect fires it will arrive back at the server with a host header of www.domain.co.uk and redirect again. You need to also check the query string and only redirect if it doesn't already match what you sent:
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$
RewrteCond %{QUERY_STRING} !lang=en&noredir=1&currency=3
RewriteRule ^$ http://www.domain.co.uk/?lang=en&noredir=1&currency=3 [L,R=301]

Mod_rewrite (htaccess) QSA and removing charactes from appended query string

I've been struggling with some htaccess redirects. I just spent some time reading and searching and couldn't get a solution that works with my scenario.
I'm in the process of making the 301 redirect for an old website (ASP) to a new one (Wordpress). The old pages has parameters query which I need to process but also remove 'http://' string from it to get redirect to work.
Example URL (old) to redirect looks like:
http://www.domain.org/index.asp?documentID=2410&utm_source=IT+Travel+Reminder&utm_medium=Informz%2FnetFORUM&utm_campaign=IT%2FTravel+Reminder%2FMonthly+Monthly+Travel+Reminder&zbrandid=4050&zidType=CH&zid=28841368&zsubscriberId=1036792259&zbdom=http://my.informz.net
redirected to:
http://www.domain.org/permalink-2410/?qs=true&utm_source=IT+Travel+Reminder&utm_medium=Informz%2FnetFORUM&utm_campaign=IT%2FTravel+Reminder%2FMonthly+Monthly+Travel+Reminder&zbrandid=4050&zidType=CH&zid=28841368&zsubscriberId=1036792259&zbdom=my.informz.net
and .htaccess code to redirect it:
RewriteCond %{QUERY_STRING} ^documentid=2410(&.*)$ [NC]
RewriteRule ^index\.asp(.*):(.*)$ http://www.domain.org/permalink/?qs=true%1%2 [L,R=301,QSA]
but somehow is not working as I have expected when
RewriteCond %{QUERY_STRING} ^documentid=2410(&.*)$ [NC]
RewriteRule ^index\.asp$ http://www.domain.org/permalink/?qs=true%1 [L,R=301,QSA]
works fine when I will remove http:// or : from a query string.
Where do I have made mistake?
Thanks!
Try this rule:
RewriteCond %{QUERY_STRING} ^documentid=(\d+)(&.+?)http://(.+)$ [NC]
RewriteRule ^index\.asp$ http://www.domain.org/permalink-%1/?qs=true%2%3 [L,R=302,NC,NE]
Make sure to clear browser cache before testing this.

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.

Apache mod_rewrite: can these simple RewriteRule be improved? And suggestions!

I started finally to understand Apache mod_rewrite. It's pretty GREAT!
Plz have a look at the followings:
1) Permanent redirects http://www.domain.com/folder_name/ (with or without final slash and with or without the www) to http://www.domain.com/some/path/some_page.html
RewriteRule ^folder_name[/]*$ "http\:\/\/domain\.com\/some\/path\/some_page.html" [R=301,L]
2) Permanent redirects all requests to www.domain.com... to same path and file request but without www in domain
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\/$1" [R=301,L]
They all work as expected and do their jobs, I'm simply curios if some guy, who is more expert than me in mod_rewrite, could give me some advises like: "it could be better in this way...", "there might be a problem if...", etc.
Thanks!
Use the ? quantifier instead of * and you don’t need to escape the substitution URL:
RewriteRule ^folder_name/?$ http://example.com/some/path/some_page.html [R=301,L]
You might want to consider HTTP 1.0 requests where the Host header field is missing. Another useful extension would be to take HTTPS into account:
RewriteCond %{HTTP_HOST} !^(|example\.com)$
RewriteCond %{HTTPS} ^on(s)|
RewriteRule ^ http%1://example.com%{REQUEST_URI} [R=301,L]

Using Apache Rewrite to go from HTTP to HTTPS and vice-versa, but I get a 302 response and my app errors out

I really hope someone knows what I'm doing wrong here, cuz I sure don't!
We have a certain page on our site which has account balance information on it, and we want to make it secure with SSL. But we only want this one particular page to be secure. I have the following in the localhost:80 virtualhost, and it works perfect:
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/account\.php(.*) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [QSA,L]
However, as you might guess, we want all other pages to just use HTTP. So I stuck this little snippet into my localhost:443 virtualhost:
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/account\.php(.*) [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [QSA,L]
...And that's when the problem happens. I have no problems going back to HTTP, but when I click the link to go to the account page, it changes to HTTPS but I immediately get an error 302 response. I do not get this response when I remove either one of those rewrite rules, it only happens when they are both there.
I have tried replacing [QSA] with [R] and [R=301], to no avail.
(I'm aware that the %{HTTPS} on/off is a bit redundant ;))
So I have two questions:
Is there something I am forgetting or doing wrong that might be causing this?
Is using [QSA] redundant with $1? We use the GET method a lot to specify pages and what not.
We are using PHP 5.2.9 and Apache 2.
Many thanks in advance!!
Brian
Whilst it's on here (and not moved to serverfault).. try
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/account\.php https://%{HTTP_HOST}/account.php [R=301,QSA,L]
HTTP/1.1 302 = Found (and is not an error code), but temporarily at another location.
EDIT
Actually, whilst you are putting the code in separate VirtualHosts, you may as well do
(In :80)
RewriteRule ^/account\.php https://%{HTTP_HOST}/account.php [R=301,QSA,L]
(in :443)
RewriteCond %{REQUEST_URI} !/account\.php$
RewriteRule ^(.*) http://%{HTTP_HOST}$1 [R=301,QSA,L]