htaccess rewrite rule another parameter - .htaccess

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

Related

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 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.

is this the right way to apply a single rewrite rule to multiple pages?

I'm trying to use .htaccess but i'm a bit lost at this point. I was wondering how would you do
a rewriting for multiple pages.
RewriteRule ^your-order/$ /page1.php,page2.php,page3.php [L]
or should i just do this:
RewriteRule ^your-order/$ /page1.php [L]
RewriteRule ^your-order/$ /page2.php [L]
RewriteRule ^your-order/$ /page3.php [L]
also i was wondering if rewriterule would still execute if the page has a parameter:
URL: page1.php?test=hello
RewriteRule ^your-order/$ /page1.php [L]
I am assuming from your question about the query string params that you actually have the concept of the rewrites backward. The first expression is the submitted URL (pageN.php) and the second one is where it should be redirected or rewritten (your-order). In that case, you need only one rule.
Unless you need to take special action if a query string parameter is present (like go to a different page entirely), you don't need to match the query string. Query strings are matched in separate RewriteCond conditions rather than in the RewriteRule.
RewriteEngine On
# Rewrite page1, page2, page3 to your-order
# Add as many other pages as necessary separated by |
# The QSA appends any additional querystring to your-order
RewriteRule ^(page1|page2|page3)\.php your-order [L,QSA]
If your pages are actually named with the number at the end (which I doubt), you could use this expression instead:
RewriteRule ^page[0-9]+\.php your-order [L,QSA]
In either case, if you want the end user's browser to be redirected to the your-order URL, rather than an internal and invisible rewrite, change [L,QSA] to [L,R,QSA]

.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