rewrite rule, no clue - .htaccess

I would like to redirect
pins?board=10000011
to
view_photos.php?album=10000011
10000011 being an id that is dynamic, therefore the no clue in the title of this question, do not know how to grab that and use inside a rewrite rule.
have tried with:
RewriteRule ^([A-Za-z-0-9-_.]+)/pins?board= $1/view_photos.php?album=$2
it seems I obviously don't know exactly what I'm doing so I'm asking for help.

You can't match against the query string in a rewrite rule. You need to use the %{QUERY_STRING} var in a rewrite condition:
RewriteCond %{QUERY_STRING} ^board=(.*)$
RewriteRule ^/?([A-Za-z-0-9-_.]+)/pins$ /$1/view_photos.php?album=%1 [L,R=301]

Related

Query string in htaccess rule doesn't work

I've a problem with the rule below:
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^[ROOT]/([0-9]+)_(.*)$ https://www.[DOMAIN].[EXT]/[ROOT]/$1-$2?page=([0-9]+) [R=301,L]
I need to redirect the old qs parameter (p) to the new one (page), but the result is this:
https://www.[DOMAIN].[EXT]/[ROOT]/[ID]-[STRING]?page=([0-9]+)
I mean: if I've ?p=4 I need to go to ?page=4
I don't understand what's wrong
Thank you all

htacces rewrite - extend url without modifying query

I want to change:
/?q=bla
to
/search?q=bla
I have placed rule like:
RewriteRule ^search?q=(.*)$ /?q=$1 [L]
but it doesn't work, I would really appreciate some help, thanks
You can't match the query string using the pattern inside a RewriteRule. You need to match against the %{QUERY_STRING} var inside a RewriteCond:
RewriteCond %{QUERY_STRING} ^q=
RewriteRule ^search$ / [L,QSA]
Technically, you don't need the QSA flag, since query strings get appended automatically.

Pattern match 301, retaining one querystring parameter

Been scratching my head over this one. I have a URL format I need to redirect and only need one part of the querystring. Example URL:
tiles?shape=&colour=&finish=&material=&price=&o=60&tile=50-double-roman-antique-red-03-granular
All I need from this is the value of the tile parameter (50-double-roman-antique-red-03-granular) so I can then redirect it to:
tiles/detail/50-double-roman-antique-red-03-granular
Any suggestions for a rewrite rule?
Thanks
This works on my box. We're including the ? after the rewrite string to prevent the behavior or QSA or query string append which is the default behavior if no query string is passed. If you forget the ? then it creates a loop.
RewriteCond %{QUERY_STRING} tile=([^&]+)&?.*$
RewriteRule (.*) /tiles/detail/%1? [L]
This off-topic answer gave me what I was looking for:
RewriteCond %{QUERY_STRING} &tile=([0-9a-zA-Z-]+)
RewriteCond %{REQUEST_URI} !tiles/detail
RewriteRule tiles /tiles/detail/%1? [R=301]

Rewrite trouble in htaccess

So I have a kind of strange trouble. I have a rewrite rule from
mysite.com/brand.php?brand=example&ref=ex1
to
mysite.com/brand/example/ex1
And I managed to do that, but there is also another problem: when you type this:
mysite.com/brand/example/ex1.html
or
mysite.com/brand/example/ex1.php
I get this:
mysite.com/brand/example/ex1?ref=ex1.php&brand=example
Can anyone tell why I get this redirect and how to get rid of it?
I tried this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)php$ /brand/$1/$2
or this:
RedirectMatch ^brand/([A-Za-z0-9-\.\_]+)/([A-Za-z0-9-\.\_]+)\.php$ /brand/$1/$2
but it simply doesn't work.
Please, anyone help me!
I would use the mod_rewrite for this:
RewriteEngine On
RewriteCond %{REQUEST_URI} =/brand.php
RewriteCond %{QUERY_STRING} ^brand=([^&]+&ref=(.+)$
RewriteRule .* /brand/%1/%2? [L,R]
The RewriteRule is applied only when both RewriteCond are valid. The first RewriteCond compares the request URL to be /brand.php, the second matches regular expression against the query string and stores the matches in %1 and %2 respectively.
In the RewriteRule is the URL rewritten using the matched parts from query string.

mod_rewrite remove query string on top of a rewrite rule

I have the following rewrite rule:
RewriteRule ^(.*)-task-(.*)\.html$ /index.php/task/name/$2\-task\-$1 [L]
When I tried to open:
/heru-task-number-1.html
It is working fine. HOwever, when there is a query string appended to it:
/heru-task-number-1.html?whatever=value
It is actually not calling the correct rewrite. Thus, I wonder how can I make sure so that both:
/heru-task-number-1.html
AND
/heru-task-number-1.html?whatever=value
are actually calling the same thing that is:
/index.php/task/name/$2\-task\-$1
I have tried to do this but to no avail.
RewriteRule ^(.*)-task-(.*)\.html\?(.*)$ /index.php/task/name/$2\-task\-$1 [L]
Thank you for your help or feedback on this.
This is fixed by inserting the following code at the top of htaccess:
RewriteCond %{QUERY_STRING} (^|&)fb_comment_id=
RewriteRule ^(.*)$ /$1? [L,R=301]
basically, what it does is that it will remove any extra query string that has fb_comment_id and redirect 301 to the one without query string.
Thank you #oddant and #Gerben for helping out!

Resources