Apache mod_rewrite and PHP ?argument=something - apache

I'm trying to get all HTML and PHP files on my site to redirect through the index.php, so that they can have common frames applied to them, a templating solution I coded that I found to be quite elegant. Anywho, I'm having issues with my PHP files, specifically those that have arguments after them.
My regular rule for PHP files is the following:
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L]
This works fine for any pages that have no arguments, but you can see that any PHP documents that have
?argument=something
end up as:
index.php?page=path/to/page?argument=something&type=1
which is not a working solution at all. Now, what's bothering me here is the $ at the end of the rule, shouldn't that cause it to fail if there is anything after the .php?
Anywho, I tried rewriting the rule as:
RewriteRule ^(.+).php\?(.+)$ index.php?page=$1&type=1&$2 [NC,L]
but that simply doesn't trigger at all. It seems that the regex flavor used in mod_rewrite is far different than I'm used to working with, so I'm sure these are simple mistakes I've made, but I can't seem to find decent documentation for this flavor of regex other than the most basic of examples.
Can anyone show me what I'm doing wrong? Thanks.

Try qsa in your rule, which stands for "query string append" - mod_rewrite will then append any query string from the original URL to the rewritten URL
RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L,qsa]
RewriteRule doesn't match against the query string, which is why your second attempt did not work. Here's the relevant note from the manual
The Pattern will not be matched
against the query string. Instead, you
must use a RewriteCond with the
%{QUERY_STRING} variable. You can,
however, create URLs in the
substitution string, containing a
query string part. Simply use a
question mark inside the substitution
string, to indicate that the following
text should be re-injected into the
query string. When you want to erase
an existing query string, end the
substitution string with just a
question mark. To combine a new query
string with an old one, use the [QSA]
flag.

Related

mod_rewrite regex to match root level whole name page

I'm want to rewrite many pages following the simple schema:
old_page.php to content/new-page.php
I got a problem when apple.php must be rewritten to fruits/red-apple.php because I get into a rewriting loop, so i have to match domain/apple.php
I'm not sure if I can set up RewriteRule or I'll also need rewriteCond ..
You should show us what you're working with, but it sounds like your patterns are simply missing start and end of string anchors. E.g.:
RewriteBase /
RewriteRule ^apple\.php$ fruits/red-apple.php [NS,L]
You may be better off using RewriteMap if you have a lot of rules.

Apache rewrite rule for optional query parameter

Even after lengthy perusal, the Apache rewriterule documentation continues to confound me.
Currently I am using the following .htaccess URL rewrite rule:
RewriteRule ^([_0-9a-zA-Z-]+)\.html$ index.php?a=p&p=$1 [nc]
This rewrites something like
http://www.website.com/thepagename.html
into
http://www.website.com/index.php?a=p&p=thepagename
which works fine.
Now I need to modify this to allow for an optional query parameter that may or nay not be tacked on to the original (unrewritten) URL. E.g.:
http://www.website.com/thepagename.html?req=login
or even
http://www.website.com/thepagename.html?req=login&usr=johndoe
must be rewritten into:
http://www.website.com/index.php?a=p&p=thepagename&req=login
and
http://www.website.com/index.php?a=p&p=thepagename&req=login&user=johndoe
respectively, without breaking the original rewrite (i.e. without the optional query parameters tacked onto the unrewritten URL).
Try as I might, I cannot work out the correct syntax. Can anyone point me in the right direction?
Tnx!
// FvW
You only have to add ˋQSA` flag (Query String Append)
RewriteRule ^([_0-9a-zA-Z-]+)\.html$ index.php?a=p&p=$1 [L,QSA,NC]
More info (and examples) here

Redirect URL with correct syntax

I am having some difficulty trying to format my redirect correctly.
I have the following:
RedirectMatch 301 http://mooseburger.com/onlinestore/index.cgi?code=3&cat=7
https://www.mooseburgeronline.com/categories/Clown-Costumes-/Coats%2C-Jackets-%26-Vests/
I know it has something to do with the % or & signs. How do you escape them?
I believe the pattern of the RedirectMatch should be an absolute URL (like "/onlinestore/something") or an URL relative to the htaccess' location (like "onlinestore/something"). Also, the RedirectMatch pattern can't match against query strings, unfortunately. You'll need to use mod_rewrite for that, as Rekire suggested. The syntax would be something along these lines:
RewriteEngine on
RewriteCond %{QUERY_STRING} \bcode=(3)\b
RewriteCond %{QUERY_STRING} \bcat=(7)\b
RewriteRule ^onlinestore/index.cgi https://www.mooseburgeronline.com/categories/Clown-Costumes-/Coats%2C-Jackets-%26-Vests/? [NC]
This also strips off the original query string. I haven't tested it, but this should get you started.
Some links:
The docs are, hm, somewhat hard to read, but it's full of information. Make sure to check the control flow diagram, it helps a lot.
Query string cheat sheet
Regex tutorial for the intricacies of patterns.
And of course Google will give you hundreds of decent tutorials.

Using Apache's mod rewrite how to redirect depending the query string?

Using Apache's mod rewrite how to redirect foo.php?r=ok to boo.php (that is, depending the query string: if r = ok then redirect).
you can't use query string (the string after "?" character) as variable for rewrite, because query string is not a part of URL path.
you need to modify foo.php?r=ok to some string path like "foo.php/redirect/"
From the Apache manual discussing mod_rewrite:
If you wish to match against the
hostname, port, or query string, use a
RewriteCond with the %{HTTP_HOST},
%{SERVER_PORT}, or %{QUERY_STRING}
variables respectively.
Look closely at RewriteRules and RewriteConditions
EDIT: Added a working example
The RegEx use to write these is just pure heaven </sarcasm>
Any how... here's a simple rule (Apache2, Mac OS X 10.5.8)
RewriteCond $1 ^(foo\.php)
RewriteCond %{QUERY_STRING} (r=ok)
RewriteRule (foo\.php) /bar.php [R]
I tested this for as many obvious caveats that I could think of (ie r=ko, oof.php, foo.php?r=o), but couldn't break it.

Apache Rewrite - put parts of query string in replacement string

I'd like to rewrite:
www.example.com/file.html?username=john&number=1234
To:
www.example.com/users/john
But I can't figure out how to extract the "username" value from the query string. I've been Googling this all morning and reading the official docs but no luck. I need to solve this problem with a rewrite, rather than changing the application.
Any help much appreciated!
Rangi
RewriteCond %{QUERY_STRING} username=([^&]+)
RewriteRule /?file.html /users/%1
Going to http://example.com/file.html?username=foobar will then redirect you to http://example.com/users/foobar, add an [R] to the end if you need an external redirect.
Mostly the rewrites are done the other way around, it's rare to see someone who wants a querystring in 'outside' urls but doesn't have them internally. Or did I understand your question backwards?
Ok I've solved this using two rules, although not sure if I'm doing it the best way.
RewriteRule ^file.html xxx/%{QUERY_STRING} [L]
RewriteRule ^xxx/[^=]*=([^&]*) /users$1 [R=301,L]
The first rule makes the query string part of the URL, so the second rule can see it, and therefore match and rewrite parts of it. I used "xxx" but it could be anything.