RewriteRule that preserves GET parameters - .htaccess

What is wrong with this rewrite rule?
RewriteRule ^api/(.+)$ api/index.php?url=$1 [L]
I simply want "index.php?url=" to be added after api/ and before the rest of the get parameters.
api/image/upload&arg1=1&text=lorem+ipsum
to
api/index.php?url=image/upload&arg1=1&text=lorem+ipsum
What is wrong with (.+) to get everything after api/?

The regex on the RewriteRule is only run against the path part of the URL, not the query parameters. Fortunately there is the [QSA] flag to preserve existing query parameters.

Are you doing something to stop infinite recursion?
RewriteRule ^api/(.+)$ api/index.php?url=$1 [R=301,L]
or some equivalent

I think you must write your domain name before the regex stuff. Like this:
RewriteRule ^(.+).com/api/(.*)$ "$1.com/api/index.php?url=$2" [R=301,L]

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.

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

.htaccess questions

Say I have the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} name=value [NC]
RewriteRule ^image01.gif$ http://www.domain.tld/images/partner/image01.gif [NC,QSA]
RewriteCond %{HTTP_COOKIE} name=value [NC]
RewriteRule ^image02.gif$ http://www.domain.tld/images/partner/image02.gif [NC,QSA]
What do NC and QSA mean?
Also, instead of repeating the same RewriteCond twice is there to use it just once and have it apply to both RewriteRules?
Finally, if the above .htaccess is located at http://www.domain.tld/images/ why doesn't a RewriteRule like this work?:
RewriteRule ^image02.gif$ /images/partner/image02.gif [NC,QSA]
Or maybe this?:
RewriteRule ^image02.gif$ partner/image02.gif [NC,QSA]
The square bracket options are documented in the RewriteRule manual page:
'nocase|NC' (no case):
This makes the Pattern case-insensitive, ignoring difference
between 'A-Z' and 'a-z' when Pattern is matched against the current URL.
'qsappend|QSA' (query string append):
This flag forces the rewrite engine to append a query string part
of the substitution string to the
existing string, instead of replacing
it. Use this when you want to add more
data to the query string via a rewrite
rule.
As far as I know, the RewriteCond directives affect the RewriteRule they precede. If you were setting rules in the main confing file you could write the common directives in a file and include it several times but that's not an option in .htaccess files, sorry.
Your directive works for me, although you probably mean this:
RewriteRule ^image02\.gif$ /images/partner/image02.gif [NC,QSA]
How are you testing it exactly?
NC is for No Case, meaning it can be upper or lower case and it will take you to the same page.
QSA is for query string append. Not really sure on this one, however a quick search http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html sheds a bit more light on this one.

How can I forward a query string using htaccess?

I am using this, at present, to rewrite URLS:
RewriteEngine on
RewriteRule ^([^/?\.]+)$ /page.php?name=$1 [NC]
So mysite.com/home gets rewritten to mysite.com/page.php?name=home
How can I make it also rewrite mysite.com/home?param=value to mysite.com/page.php?name=home&param=value? Ideally, I'd like this to work for any name/value querystring pairs.
Am I missing something obvious?
Check out the "capturing variables" section at http://corz.org/serv/tricks/htaccess2.php
It says that if you add [QSA] to the end of the rewrite rule, it should pass variables through the rewrite.
This made it work:
RewriteEngine on
RewriteRule ^([^/?\.]+)$ /page.php?name=$1&%{QUERY_STRING} [NC]

A simple htaccess redirect

How would I write a redirect rule that would let me use both http://www.site.com/rss and http://www.site.com/anydirectory/rss rather than http://www.site.com/rss.xml and http://www.site.com/anydirectory/rss.xml ?
I think I'm close, but for some reason, it's a Monday.
RewriteRule ^(.*)/rss.\xml/$ $1/rss [L]
I think this is what you want (you have them reversed):
RewriteRule ^(.+/)?rss$ $1rss.xml [L]
This is a guess, but it looks like you intended to escape the . and that is done with \. instead of .\
RewriteRule ^(.*)/rss\.xml$ $1/rss [L]
It sounds like you have the XML files, and you want to make URIs without the XML extension work.
# translate /rss to /rss.xml
RewriteRule ^rss$ /rss.xml
# translate /anydirectory/rss to /anydirectory/rss.xml
RewriteRule ^(.+)/rss$ /$1/rss.xml
The code you tried suggests the opposite.
# translate /rss.xml to /rss
RewriteRule ^rss\.xml$ /rss
# translate /anydirectory/rss to /anydirectory/rss.xml
RewriteRule ^(.+)/rss\.xml$ /$1/rss

Resources