htaccess QUERY_STRING urldecode - .htaccess

I have a problem concerning .htaccess and QUERY_STRING.
I try redirecting an URL with my htaccess that looks like this:
http://mydomain.tld/out/http%3A%2F%2Fotherdomain.tld%3Fparam%3D0
to
http://otherdomain.tld?param=0
I use RewriteCond and RewriteRule with the REQUEST_URI to redirect the url and everything works fine since REQUEST_URI is urldecoded by default in the htaccess.
However, when I email the link to Hotmail, Hotmail urldecodes the slashes and the question mark. The result looks like this:
http://mydomain.tld/out/http%3A//Fotherdomain.tld?param%3D0
So htaccess takes the link and tries to redirect it but due to the question mark the htaccess "thinks" everything behind the question mark is a QUERY_STRING.
The problem: apache2 doesn't urldecode the QUERY_STRING. So what happens is that htaccess redirects to
http://otherdomain.tld?param%3D0
which will fail.
So my question is:
How can I tell htaccess to either urldecode the QUERY_STRING or use the full requested url (either urlendcoded or urldecoded) including the part after the question mark
Thanks in advance!
Cheers

You need not add [QSA] to your rewrite rule to force htaccess to encode query string too.
http://httpd.apache.org/docs/current/en/mod/mod_rewrite.html

Related

301 redirect subdirectory and anything in it

I bought an old site that apparently used some kind of a CMS, maybe wordpress.
Now it's a simple html site. I want to redirect www.example.com/main and everything in that directory to root. In other words, if a url is example.com/main/a_bunch_of_garbage that should 301 redirect to the homepage. Here is what I have but it doesn't quite work.
Options +FollowSymLinks
RewriteEngine on
RedirectMatch 301 ^/main/(.*)$ http://www.example.com
If the url doesn't contain any weird characters like ? or = then it works. But for example, if I go to www.example.com/main/index.php?option=com_content&view=post&id=84&Itemid=982/
Then although it does redirect to the homepage, in the address bar, I still see the entire URL. Can I fix this?
I also just remembered another problem I had. If I put example.com/main (without forward slash after main) the redirect doesn't work. It only works if I put example.com/main/ (with forward slash)
You need to use mod_rewrite rules only and avoid mixing RedirectMatch here. Have your rules like this in your root .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^main(/.*)$ $1? [L,NC,R=301]
? in the end is used to strip off any existing query string.

What to do with Question Marks in HTAccess Redirects

I'm new to redirects and have researched how to do this but am just getting more and more confused.
What I'm wanting to do is create a rule to redirect:
From: http://www.example.com/wordpress/?p=1250
To: http://www.otherexample.com/blog
Where I'm running into issues is with the question mark. I've read somewhere that I'm not sure I'm doing this correctly, but here's what I have so far:
**Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=1297$
RewriteRule ^wordpress$ linktoothersite [R=301, QSA, L, NC]**
I'm failing somewhere, any of you see what I'm doing wrong? Any help would be appreciated. I could only post two links, so link to other site would be the link.
If it's a single link you want to change you don't need a htaccess rule for that you can just do a redirect.
redirect 301 /wordpress/?p=250 http://www.othersite.com/blog
If the URL parameter changes you can use this rule.
RewriteEngine on
RewriteRule ^wordpress/(.*) http://www.othersite.com/blog [R=301,QSA,NC,L]
Note that when you use the QSA flag it will append the URL with the query parameter so if that's what you want the resulting URL will be.
http://www.othersite.com/blog?p=250
Otherwise if you remove the QSA flag, then it will just redirect to this
http://www.othersite.com/blog

htaccess static url rewrite

I am working on wordpress blog and the its has the url pattern something like this. localhost/blog/2012/10/12/sample-post/ .
Now i have updated the url pattern to localhost/blog/sample-post/.
Now i need a redirection to second URL pattern if someone enters the first URL Pattern as i have already submitted URLs with first pattern to couple of articles.
i.e, localhost/blog/2012/10/12/sample-post/ should be redirected to localhost/blog/sample-post/
Can anyone assist me by providing htacess code ?
Try adding these to the htaccess file in your document root. Make sure they are before any wordpress related rules:
RewriteEngine On
RewriteRule ^/?blog/[0-9]{4}/[0-9]{2}/[0-9]{2}/(.*)$ /blog/$1 [L,R=301]

.htaccess: rewrite URL with query strings

I need to rewrite a URL using an .htaccess and the URL has two query strings. Here is my raw URL:
http://domain.com/channel-partners/en/index.php?location=phoenix-az&name=company
How do I get that to be written as:
http://cp.domain.com/phoenix-az/company
RewriteEngine On
RewriteBase /
RewriteRule ^partners/([\w-]+)/([\w-]+)/?$ channel-partners/en/index.php?location=$1&name=$2 [L]
This will redirect URLs in the format partners/location/name with an optional trailing slash to the raw URL you provided. location and name can be alphanumeric, underscore, or dash characters.
Note: I have prefixed the URL with the literal partners. Without it your RewriteRule would be too loose and match nearly all requests. Feel free to change it.
UPDATE
To redirect from your sub-domain, the above needs to be in the .htaccess file for cp.domain.com. You'll need to modify your RewriteRule to the following:
RewriteRule ^partners/([\w-]+)/([\w-]+)/?$ http://domain.com/channel-partners/en/index.php?location=$1&name=$2 [L]
However, I really don't recommend this. Link management is going to be a nightmare. You should really try to have all these links under the same domain. 301 Redirect from the sub-domain if you have to.

htaccess redirect with dynamic variables conflict

I'm working in an old CMS that uses htaccess to rewrite URIs with GET variables into something more user friendly.
RewriteEngine on
RewriteRule ^animals/(.*)/ secondary.php?page=$1
RewriteRule ^animals/(.*) secondary.php?page=$1
which results (correctly) in
http://www.example.com/animals/duck
The problem is I now need to redirect some of those pages to new pages. I've tried:
Redirect 301 /animals/goose http://www.example.com/animals/fowl
The redirect almost works, but it adds "?page=goose" to the end of the rewritten URI:
http://www.example.com/animals/fowl?page=goose
I've tried using RewriteRule as well as RewriteCond, but unfortunatley I'm having no luck. Any help would be immensely appreciated.
Try placing this before the other rules instead of the Redirect statement. R=301 is for the redirect and L signals that the rule in question is the last rule to be processed.
RewriteRule ^animals/goose /animals/fowl [R=301,L]
Also you can easily make the slash (just like any other character) optional with a question mark, instead of having two rules.
RewriteRule ^animals/(.*)/?$ secondary.php?page=$1

Resources