Pattern match 301, retaining one querystring parameter - .htaccess

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]

Related

Htaccess 301 redirect with query string params

This is so elementary that you have to ask why it is so complex to implement.
Simply need to redirect one url to another on the same domain, the url contains query string aparams though:
I tried:
RewriteRule ^article.php?section=exclusive&id=543 articles/view/4639 [R=301,L]
I get page cannot be found - the redirect is not happening. I have other re-directs in the same htaccess which work. The query string params are causing problems, and escaping them also does not help.
Note the id's of the two urls are not the same and therefore I don't want a clever re-write rule which converts from one url to another with the query strings mapped to new params. I just want a fast and dirty mapping from A to B.
UPDATE
I tried this it redirects but it is adding my old query string to the end of the new url. How can I prevent that?
RewriteCond %{REQUEST_URI} ^/article\.php$
RewriteCond %{QUERY_STRING} ^section=exclusive
RewriteRule ^$ http://www.onlinegooner.com/articles/view/3000 [R=301,L]
Input url is: mydomain.com/article.php?section=exclusive
You may use this rule:
RewriteCond %{QUERY_STRING} ^section=exclusive [NC]
RewriteRule ^article\.php$ /articles/view/3000? [R=301,L,NC]
? in the target will strip off previous query string.

.htaccess redirect query string value to another key

What i'm trying to do is rewrite the following pattern
http://example.org/path?articleid=5657
to this pattern:
http://example.org/path?p=5657
Essentially it comes down to changing the key to the url parameter, i have searched extensively with no clear example of how to do this exact thing using only htaccess rewrite rules.
i've tried this general approach with no luck
RewriteCond %{QUERY_STRING} ^articleid=([0-9]*)$
RewriteRule ^articleid=([0-9]*)$ /?p=$1 [R=301,L]
You can't match against the query string in the pattern of a rule, you need to match against the URI:
RewriteCond %{QUERY_STRING} ^articleid=([0-9]*)$
RewriteRule ^path$ /path?p=$1 [R=301,L]

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.

restricting input of GET url on htaccess rewrite url

My original url is : www.site.com/report.cgi?d=2012-05
Requested URL: www.site.com/report-2012-05.cgi
My Htaccess Code:*
RewriteRule ^report([^/]*)\.cgi$ /report.php?d=$1 [L]
I want to restrict the request parameter to just XXXX-XX number format in GET url.
How can I do this ?
I didn't really understand your question, except you want to modify the URL format placing the parameter value in a different position.
The best way to do it is by capturing the query string like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} d=(.*)
The value inside the round brackets is the parameter value (2012-05), which can be back referenced with %1. For example:
RewriteRule .* report-%1.cgi [L]
Will rewrite the URL with /report-2012-05.cgi
Hope this helps.
I think you need to remove .cgi from your rewrite rule
For www.site.com/report-xxxx-xx
RewriteRule ^report-([^/]*)$ /report.cgi?d=$1 [L]<br>
For www.site.com/xxxx-xx
RewriteEngine On
RewriteRule ^([^/]*)$ /report.cgi?d=$1 [L]

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

Resources