htacces rewrite - extend url without modifying query - .htaccess

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.

Related

How to rewrite query string to hash in the URL?

I am looking to rewrite:
www.mysite.com/services/category1/?content=service1
to
www.mysite.com/services/category1/#tab-service1
I have this specific rewrite that I want to generalize for all the possible URLs on the site:
RewriteCond %{QUERY_STRING} content=service1
RewriteRule ^services/category1 services/category1/#tab-service1? [NE,R,L]
I tried the following but it didn't work :( -
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?content=(.*)$ services/category1/#tab-$1 [NE,R,L]
Thanks.
You rule looks fine except for 1 thing
QUERYSTRING content=service1
isn't part of match in RewriteRule's pattern so you can't test query string (Url part after the ?) in pattern of a RewriteRule. To test url query string we use %{QUERY_STRING} variable like the one you are already using.
RewriteCond %{QUERY_STRING} content=([a-zA-Z]+)
RewriteRule ^services/category1/?$ services/category1/#tab-%1 [NE,R,L]

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]

htaccess rewrite rule another parameter

I have this rule in my htaccess:
RewriteRule ^video_(.*)$ show-video2/?txtkey=$1 [L]
Sometimes it can happend that I need another parameter in GET, for example:
.../show-video2/?txtkey=122312421&anotherparam=1232
The problem is that with this RewriteRule i'm not able too see the second parameter in $_GET:
RewriteRule ^video_(.*)$ show-video2/?txtkey=$1 [L]
How I have to modify the RewirteRule in order to see the second parameter ( when there is it )?
Infact if a var_dump($_GET) I can only see the txtkey parameter...
Change your RewriteRule to:
RewriteRule ^video_(.*)$ show-video2/?txtkey=$1 [L,QSA]
The QSA is what you're after - it means query string append - and will let you capture the rest of your GET

.htaccess redirect with ignore query string

I would like to redirect without looking at the query string, and my redirect result no need append the query string as well, so I add a ? at the end of the RewriteRule.
I tried the following syntax, but the outcome just close to it.
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule ^exd\.asp$ http://www.example.com/index.php?r=p/consumer? [R=301,L]
and also, i tried to escape the first ?, which I need it, but still the same outcome.
RewriteRule ^exd\.asp$ http://www.example.com/index.php\?r=p/consumer? [R=301,L]
Outcome:
http://www.example.com/index.php?r=p/consumer%3f
I want to get ride of the %3f.
Thanks!
You don't need to append a ? at the end if you already have a query string in your target. Just do this:
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule ^exd\.asp$ http://www.example.com/index.php?r=p/consumer [R=301,L]
By default, query strings get appended, like this:
RewriteRule ^foo$ /bar [L]
You request /foo?blah and you get /bar?blah
However, if you have a ? in your target, query strings won't get appended unless you have the QSA, so:
RewriteRule ^foo1$ /bar? [L]
RewriteRule ^foo2$ /bar?q=2 [L]
You request /foo1?blah and you get /bar, you request /foo2?blah and you get /bar?q=2. If you include a QSA in the rewrite flags, then &blah gets appended to the end.

.htaccess redirect to a directory and retain $_GET variables?

I want to redirect from /orders/whatever/?n=4
to:
/orders/?shopURL=whatever&n=4
I managed to redirect to the correct directory but I'm losing the passed variables.
This is my current rule:
RewriteRule ^([\w\d]+)$ /orders/?shopURL=$1 [L]
If you want to enforce the existence of a query string, use:
RewriteCond %{QUERY_STRING} (\bn=\d+\b)
RewriteRule ^orders/([\w\d]+)/?$ /orders/?shopURL=$1&%1 [L]
Otherwise, you can use the following without a RewriteCond:
RewriteRule ^orders/([\w\d]+)/?$ /orders/?shopURL=$1 [L,QSA]
The QSA flag to RewriteRule will retain existing query string fields.
Your RewriteRule seems very generic, as though it would redirect almost every page.
I'm not that great with Regular Expressions so I'll leave those to you, or someone else. But you need something at the end of the rewrite rule to catch parameters, then append it at the end of the new URL.
RewriteRule ^([\w\d]+)/(somethinghere)$ /orders/?shopURL=$1&$2 [L]

Resources