How can I encode a query string with htaccess - .htaccess

I want to redirect
http://myoldsite.com/page?query=some+arguments&some_more
To
'http://mynewsite.com/?notfound=' + urlencode('page?query=some+arguments&some_more')
Is this possible with htaccess alone?

You can use a combination of B and NE flags to escape the back-references:
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule ^page/?$ /?notfound=$0\%3f%0 [L,NC,R=302,B,NE]

Related

301 htaccess with %20 in target URL

I need to manage some URL in query string. The landing URL contains space (%20).
I wrote this:
RewriteCond %{QUERY_STRING} ^orderby=([0-9]+)&pagenumber=([0-9]+)$
RewriteRule ^[OLD-SLUG]$ https://www.domain-site.it/Content/Images/uploaded/[TEXT-01]%20[TEXT-02]%20[TEXT-03].pdf? [R=301,L]
But the result is that the URL redirects to:
https://www.domain-site.it/Content/Images/uploaded/[TEXT-01]50[TEXT-02]50[TEXT-03].pdf
What's wrong?
Thanks all
%N, with (0 <= N <= 9), is the syntax for back references to the capture groups of the previous RewriteCond.
So %2 is referring to what you captured with pagenumber=([0-9]+).
You need to escape the percent character with a backslash, for it to be meant literal.
RewriteRule ^[OLD-SLUG]$ .../[TEXT-01]\%20[TEXT-02]\%20[TEXT-03].pdf? [R=301,L]
I've solved it in this way:
RewriteCond %{THE_REQUEST} ^(\S*)\s/(\S*) [NC]
RewriteCond %{QUERY_STRING} ^orderby=([0-9]+)&pagenumber=([0-9]+)$
RewriteRule ^old-slug$ https://www.domain-site.it/Content/Images/uploaded/[TEXT-01]\%20[TEXT-02]\%20[TEXT-03].pdf? [R=301,L,NE]
Tks all!

Htaccess redirect issue with query string

I have URL domain.com/index.php
I have written this code to redirect to clientarea.php
RewriteEngine On
RewriteRule ^index.php /clientarea.php [R=301]
Now this works except for urls that contain a query string (eg. domain.com/index.php?=something). This will also redirect but I don't want it when there is a query string.
Can anyone tell me how I can do it ?
To prevent rewriting when a query string is there
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /clientarea.php [R=301,L]
RewriteEngine On
RewriteRule ^index.php$ /clientarea.php? [R=301]
$ marks the end of the string in the regex.

Replace all question mark and equal sign by slash?

Is there a way to replace all question mark, '&' , and equal sign by slash ???
currently I am only replacing php extensions by .html
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [nc]
now, if the websites url is this
http://www.mydomain.com/session/image-display.php?image=1&id=59
the user should get
http://www.mydomain.com/session/image-display.html/image/1/id/59
--edited---
I just need a example so that i could write them for other pages as well.
also how to rewrite 2 or more rewrite rules ??
Based on the example you gave, you can use this:
RewriteRule ^session/image-display.html/image/(.*)/id/(.*)
/session/image-display.php?image=$1&id=$2 [L]
The [L] at the end is optional, it tells Apache to stop looking for other rules if this one is applied
You need to change your links so that they look like:
http://www.mydomain.com/session/image-display.html/image/1/id/59
Then add this in the htaccess file in your document root, before any rules that you may already have:
RewriteCond %{THE_REQUEST} \ /session/image-display\.php\?image=([^&]+)&id=([^&\ ]+)
RewriteRule ^ /session/image-display.html/image/%1/id/%2? [L,R=301]
RewriteRule ^session/image-display\.html/image/([^/]+)/id/([^/]+) /session/image-display.php?image=$1&id=$2 [L]
Try this in .htaccess:
RewriteRule ^(.*)\.html/image/([^/]+)/id/([^/]+)$ $1.php?image=$2&id=$3 [NC]
These 2 rules will internally rewrite /session/image-display.html/n1/v1/n2/v2/n3/v3 to /session/image-display.php?n3=v3&n2=v2&n1=v1
There can be any number of /name/value pairs as this rule is pretty generic.
# this rule will be applied as many times as there are /name/value pairs in URI
# after /session/image-display.html
RewriteRule ^(session/image-display\.html)/([^/]*)/([^/]*)(.*)$ /$1/$4?$2=$3 [L,QSA,NC]
# this rule be applied in the last then all /name/value pairs have been converted
# into query string
RewriteRule ^(session/image-display)\.html/?$ /$1.php [L,NC]

Redirect htaccess php

I want to redirect this page with htaccess
products_filter.php?f16%5B0%5D=bla+bla+bla&cPath=72&M_ID=12x
to
products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x
i tried this (and many other ways)
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla$
RewriteRule ^products_filter\.php$ http://www.example.com/products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x [L,R=301]
what am i doing wrong here?
Problem is using $ (end of input) in this regex:
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla$
Since your query string is: 16%5B0%5D=bla+bla+bla&cPath=72&M_ID=12x
Change that line to:
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla(&|$)
Update:
Looking at your question I realize that you are using quite a few special characters that need to be escaped.
RewriteCond %{QUERY_STRING} ^(f16%5B0%5D)=bla\+bla\+bla(?:&(.*)|$) [NC]
RewriteRule ^(products_filter\.php)$ /$1?%1=bla+bla&%2 [L,R=301,NE]
PS: It is important to use NE flag here. Otherwise %5B and %5D will be further encoded by Apache.
Try this code :
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla
RewriteRule ^products_filter\.php$ http://www.example.com/products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x [L,R=301]

htaccess - rewrite rule to get end of url

I have a url which ends with a certain variable string, and was erroneously generated and indexed unfortunately.
Example:
http://domain.com/anything-in-between/?var=xyz-abc-abc-abc
How can I redirect to main site (kill it), by detecting 'abc-abc-abc' using htaccess?
Why wouldn't this work and what would be the best solution:
RewriteCond %{REQUEST_URI} abc-abc-abc
RewriteRule .* index.php
You want to use the query string as claesv suggests but you need to then kill the query string
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} \bvar=.*?abc-abc-abc$
RewriteRule ^ index.php? [L]
This will do it silently (i.e. in the server as an internal redirect and not involving the browser). You can't use 301s reliably to trim query strings.
Something along the lines of:
RewriteCond %{QUERY_STRING} ^var=.*abc-abc-abc$
RewriteRule ^.*$ http://domain.com/ [R=301,L]

Resources