Apache RewriteRule of multiple variables in query string - apache

I've been trying to figure out how to write a new rewrite rule but I am struggling badly. We are removing the world-wide (WW) site from domain.com and want to forward any old links to the country selector. Any help would be appreciated. Thank you in advance.
# if any user goes to this URL
www.domain.com/products/folder1/folder2/index?sku=123&var2=abc&isocountrycode=ww
# forward them to this URL
www.domain.com/choose/country_selection?ref_url=/products/folder1/folder2/index.cfm?sku=123&var2=abc
# my attempt that does not work
RewriteCond %{QUERY_STRING} ^(.*)isocountrycode=ww(.*)$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=$1?%1%2 [L,R=302]
I have been using this website to test my code http://martinmelin.se/rewrite-rule-tester/

Answered my own question. Helicontech APE requires the second ? to be escaped with a backslash...
RewriteCond %{QUERY_STRING} ^(.+&)?isocountrycode=ww&(.+)?$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=/$1\?%1%2? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^((.+)&)?isocountrycode=ww$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=/$1\?%2? [NC,R=301,L]

Related

URL re-write not working without www

I am having some issues with URL re-writing that I cannot figure out. Here's the problem.
This URL IS NOT redirecting properly:
http://domain.com/index.php?en=oldpage
HOWEVER, this URL IS redirecting properly:
http://www.domain.com/index.php?en=oldpage
The only difference in the url that is not redirecting properly is the absence of the www.
Here is the re-write I am using:
RewriteCond %{QUERY_STRING} ^en=oldpage
RewriteRule ^(index.php/|)$ /newpage.html? [R=301,L]
I also have this re-write BEFORE other re-writes, to handle url's without "www.":
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Why will it not re-direct without the "www." ? Any help would be much appreciated. Thank you.
I guess there is some code/rule below those rules adding query string back to your URLs.
Try these rules instead based on THE_REQUEST variable which doesn't get overwritten:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?en=oldpage [NC]
RewriteRule ^ /newpage.html? [R=301,L]
Make sure to test it after clearing your browser cache.

htaccess is redirecting seems to be ignoring not rewritecond

I have the following scenario and I've spent hours trying to work out why it's redirecting.
Here's my htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/mypage/?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^mypage/?$ /testing.php [NC,L]
When I browse to http://www.example.com/mypage it is redirecting to https://www.example.com/mypage
I would expected because of the !^/mypage/?$ condition it wouldn't, however, I seem to be completely wrong!
Can someone please put me out of my misery and let me know why this isn't working?
Thanks in advance.
It is because 2nd rule is rewriting /mypage to /testing.php and your REQUEST_URI becomes /testing.php in the 2nd loop of mod_rewrite.
Have your rules like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/(mypage/?|testing\.php)$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^mypage/?$ /testing.php [NC,L]
Also make sure to test it in a new browser to avoid 301 caching issues.

Redirecting subdomain to file without hardcoding url

Actually topic is simple and answered million times but, somehow doesn't work on me.
What I try to do is redirect "m.domain.com" and "mob.domain.com" to a file "domain.com/folder/mobile.htm"
But looks like any code conflicts with my current code which removes "www" from beginning.
My current code is
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
# this is for webmail forward #
RewriteCond %{REQUEST_URI} /mail/?$ [NC]
RewriteRule . /webmail/ [L]
How can I redirect m and mob domains without conflicting with this code and without hardcoding "domain.com" ?
Thanks for your helps in advance.
Put this rule on top of your existing rules just below RewriteEngine On line:
RewriteCond %{HTTPS}s on(s)|
RewriteCond %{HTTP_HOST} ^(?:m|mob)\.(.+)$ [NC]
RewriteRule ^$ http%1://%2/folder/mobile.htm [L,R=301]

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.

Subdomain rewirte problem

I trying to rewrite subdomain.
For example test.mydomain.com to mydomain.com/test.php
I have create a domain *.mydomain.com.
Now I want it to send data to mydomain.com/test.php when someone visits test.mydomain. com
It will send "test" as a data.
I tried this but its not working
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain.com [NC]
RewriteCond %{HTTP_HOST) !^www\.
RewriteRule /test.php?$1 [L]
Any solution?
What's wrong?
Try write this
RewriteRule (.+) /test.php?$1 [L]
Instead of this
RewriteRule /test.php?$1 [L]
Also you can see some basic help