Rewriting every occurrence of pattern in query string via mod_rewrite - apache

I have queries of this form going towards my server:
http://my.host.com/moo?p=1&s=2&targetURL=http://foo.com/sub/more/query
What I need to do is replace a part of the parameter of this query, specifically I need to replace http://foo.com/sub/ with http://bar.com/, preserving all the other parameters in this query and also preserving the parameters of http://foo.com/sub/more/query. I would be content with just replacing it in targetURL, so I tried this:
RewriteCond %{REQUEST_URI} ^/moo
RewriteRule ^/moo(.*)targetURL=http://foo.com/sub(.*)$ http://other.host.com/moo$1targetURL=http://bar.com$2 [L,R]
RewriteRule ^/moo(.*)$ http://other.host.com/moo$1 [L,R]
But the first query just never matches. Any help?
EDIT: To make clear, I have this:
http://my.host.com/moo?p=1&s=2&targetURL=http://foo.com/sub/more/query
and I want it to become this:
http://other.host.com/moo?p=1&s=2&targetURL=http://bar.com/more/query

Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my\.host\.com [NC]
RewriteCond %{QUERY_STRING} ^(.*)targetURL=http://foo.com/sub/(.*)$
RewriteRule ^/?moo$ http://other.host.com/moo?%1targetURL=http://bar.com/%2 [L,R=301]
You need to match the targetURL=http://foo.com/sub/ part from within the %{QUERY_STRING} variable using a RewriteCond. The query string isn't part of the URI when a RewriteRule matches against it. You can then use the %1 and %2 back references to reference the groupings matched in the query string.

Try this version which i tested and works for me.
<i>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^my\.host\.com/moo [NC,OR]
RewriteCond %{QUERY_STRING} ^p\=1\&s=2\&targetURL\=http\:\/\/foo.com/sub/more/query$ [NC,OR]
RewriteRule ^/(.*) http://other.host.com/moo?p=1&s=2&targetURL=http://bar.com/more/query/$1 [R=301,L]
</IfModule>

Related

htaccess redirect using get parameters and remove same parameters

Please help me need redirect search.html?searchword=value&searchphrase=all to search.html?searchword=value
I tried:
RewriteCond %{QUERY_STRING} ^searchword=(.*) [NC]
RewriteCond %{QUERY_STRING} searchphrase= [NC]
RewriteRule ^(.*)$ /search.html\?searchword=%1 [R=301,L]
but it do not work.
You may use it like this:
RewriteCond %{QUERY_STRING} (?:^|&)searchphrase= [NC]
RewriteCond %{QUERY_STRING} (?:^|&)(searchword=[^&]*) [NC]
RewriteRule ^search\.html$ %{REQUEST_URI}?%1 [R=301,L,NC,NE]
Changes are:
Order of RewriteCond is important. Keep the condition with capture group as last one
No need to repeat searchword again in target, just capture from RewriteCond and use it later as back-reference %1
Instead of using .* use [^&]* to match only value till you get next & or end of string
Match search.html in rule pattern to avoid matching anything else
With your shown samples and attempts please try following .htaccess rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(search\.html)\?(searchword=[^&]*)&searchphrase=all [NC]
RewriteRule ^ /%1?%2 [R=301,L,NE]

Mod ReWrite to remove component of URL

I must be an idiot because I just can't work this bit out.
I've got a URL:
www.site.com.au/products/product-name.html
I need to redirect these to:
www.site.com.au/product-name.html
All the links are dynamic, the folder doesn't exist. What ReWrite rule do I use to accomplish this?
This is what I've got so far:
RewriteCond %{HTTP_HOST} ^(www|test)\.site\.com\.au
RewriteCond %{REQUEST_URI} ^(/products/)
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^.+\.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]
Just need to add the bit to remote /products
Thanks.
RewriteRule ^products(/.*)$ http://www.site.com.au$1 [L, R=301]
This replaces everything you listed, except for the first RewriteCond (to match the domain, though if your VirtualHost only answers on those two domains, you can exclude that RewriteCond to simplify it).
RewriteRules are matched first before Apache looks at the RewriteConds, so if you can do the match in the RewriteRule itself it greatly simplifies things. Just for your future reference, if you did need to match in the RewriteCond, it would look something like this:
RewriteCond %{REQUEST_URI} ^/products(/.*)$
RewriteRule ^.*$ http://www.site.com.au%1 [L, R=301]
Note the %1 for matching what's in the parentheses in the RewriteCond vs. the $1 for matching what's in the RewriteRule.
EDIT: Per your comment, the following modification should force lowercase. I haven't had to do that myself, but per this Apache documentation it's an internal function via RewriteMap. Based on your original code it looks like maybe you already have the RewriteMap definition elsewhere. If not, I've included it here.
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} ^/products(/.*)$ [NC]
RewriteRule ^.*$ http://www.site.com.au${lc:%1} [L, R=301]
OK, I really don't know enough about Apache Rewrite to figure out the exact formatting. But after much ado these are the results that worked:
# Lowercase all /products/
RewriteCond %{HTTP_HOST} ^(www)\.site\.com\.au
RewriteCond %{REQUEST_URI} ^/products/.+\.html
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^ ${lc:%{REQUEST_URI}} [R=301,L]
# Lowercase all /products/ and strip products/ subfolder
RewriteCond %{HTTP_HOST} ^(www)\.site2\.com\.au
RewriteCond %{REQUEST_URI} ^/products/.+\.html
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^products/(.+\.html)$ /${lc:$1} [R=301,L]
Thanks,
Dom

Use rewrite rule by query string

How can I make that if the QUERY_STRING matches something it would use that rule?
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/?$ index.php?uri=$1 [L,QSA]
RewriteCond %{QUERY_STRING} uri=admin
ReqriteRule ^admin\?(.*)/?$ index.php?uri=admin&q=$1 [L,QSA]
Eg. http://localhost/admin?poll/new
After the ? should be the paramater q, the the query would be uri=admin&q=poll/new
Any idea on how I could do this?
Thanks.
Well, it happens that your problem is more simple than the link I gave you as you do not want any analysis on the query string content.
If you use this single line:
RewriteRule ^admin index.php?uri=admin&q= [L,QSA]
Where QSA mean append the query string to the result. You will obtain an internal redirection to:
index.php?uri=admin&q=&poll/new
Which is not OK, this is because the way you use argument (admin?poll/new) is not the standard way. So it seems we'll need to capture the query string content and put it by hand on the rewriteRule. This should work (if you need it only for /admin url):
RewriteCond %{REQUEST_FILENAME} admin [NC]
RewriteCond %{QUERY_STRING} (.*) [NC]
RewriteRule .* index.php?uri=admin&q=%1 [L]
Where %1 is the first parenthesis match in the RewriteCond :(.*), meaning everything in the query string, the query string being anything after the question mark. So in fact this allows admin?poll/new but also admin?poll/new&foo=toto, giving index.php?uri=admin&q=poll/new&foo=bar

how do i get mod_rewrite to execute the RewriteRules if and only if there is no parameter q= in the url?

the below code works to deal with my url structures but i need the rules not to work if there is parameter q= in the url.
i tried to set up a rewritecond (in the commented out line below) without success,
please help! thanks :)
Options +FollowSymlinks
RewriteEngine on
# RewriteCond %{QUERY_STRING} q!=(.*)
RewriteRule ^FSD/([^/]+)/([^/]+)/ /index.php?service=$1&type=FSD [NC]
RewriteRule ^ECD/([^/]+)/([^/]+)/ /index.php?service=$1&type=ECD [NC]
RewriteRule ^([^/]+)/([^/]+)/ /index.php?category=$1&subcategory=$2 [NC]
RewriteRule ^([^/]+)/ /index.php?category=$1 [NC]
Your RewriteCond's test pattern is a little off, but you got the right idea:
RewriteCond %{QUERY_STRING} !(\A|&)q=.*
Note that the RewriteCond only applies to the next RewriteRule, so if you want to perform this exclusion for all four of your rules, you'll need to copy the RewriteCond above each of them.

RewriteRule to disregard a url variable

I have some pages indexed by Google, for example:
/product.html?affiliateid=142
I want a rewrite rule to 301 redirect to the same page if there's an affiliateid=xxx
So far I have this:
RewriteCond %{QUERY_STRING} ^affiliateid=[0-9]+$
RewriteRule ^$ /test.html$ [L,R=301]
But its not working, I need to get rid of the variable and get the page name somehow.
You need to specify an empty query in your substitution URL to have the original requested query not appended to the new URL:
RewriteCond %{QUERY_STRING} ^affiliateid=[0-9]+$
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
And if you want to preserve any other query parameter, try this:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)affiliateid=[0-9]+(&+(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%4 [L,R=301]
Found it:
RewriteCond %{QUERY_STRING} ^affiliateid=([0-9]+)$
RewriteRule ^(.*)$ /$1? [L,NC,R=301]
It's that question mark here /$1? that tells the rule to end the rewrite at the query string