I have a few different mod-rewrite rules working, but this last one refuses to pass the 3 parameters to my script (index.php)
I do get the p=value, but the id, rid and chk vars don't even get defined...
A URL might look like this . . . .
http://www.domain.com/pagename.htm?id=29&rid=174&chk=a9cdca614135bbef2fb1f2bedf171f61
The rule...
RewriteRule ^/pagename\.htm\?id\=([0-9]+)&rid\=([0-9]+)&chk\=([a-f0-9]{32})$ /index.php?p=pagename&id=$1&rid=$2&chk=$3 [L]
Output of print_r($_REQUEST);
Array ( [p] => pagename )
I simply can not understand why this does not work..
Like #MarcB said, rewrite rules don't include the query string and you have to use a RewriteCond to check it. This is what would work for you based on your example above:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)&rid=([0-9]+)&chk=([a-f0-9]{32})$
RewriteRule ^pagename.htm index.php?p=pagename&id=%1&rid=%2&chk=%3 [L]
Or like I said above, you can also use the QSA flag like:
RewriteRule ^(pagename).html index.php?p=$1 [L,QSA]
And that will append any additional query string on to index.php, but doesn't validate it (which should REALLY not be done in mod_rewrite). It also allows you to add additional parameters, doesn't require a change of the rules to accommodate them and won't break if the key/values are in different order or case. Note: I added parenthesis around pagename and used the match $1 in the rewritten url. This is so it is easier to change the page name for multiple rules because you don't have to change it in two places.
Related
My .htaccess is something like this
RewriteRule ^search/([A-Za-z0-9-]+)$ search.php?keyword=$1 [L]
and it correctly displays results for example.org/search/tomato
but now i'd like to pass variables to it, but when i do it like
example.org/search/tomato?color=green
it doesn't work.
what's the common practice to solve this problem?
Use:
RewriteRule ^search/([A-Za-z0-9-]+)$ search.php?keyword=$1 [QSA,L]
QSA|qsappend When the replacement URI contains a query string, the
default behavior of RewriteRule is to discard the existing query
string, and replace it with the newly generated one. Using the [QSA]
flag causes the query strings to be combined.
I have .htaccess code to maintain my URLs:
RewriteRule ^2014/?(?:([^/]+)/?|)(?:([^/]+)/?|)$ /data/2014/index.php?section=$1&subsection=$2 [L]
I need to modify it for cases, when i run form with get method. It makes output as (for ex.)
myweb.com/2014/about/?person=1&page=2
which I want URL to understand, I mean to get in the end hidden
myweb.com/data/2014/index.php?section=about&person=1&page=2
Thank you for any help.
You need the QSA flag:
RewriteRule ^2014/...$ /data/...&subsection=$2 [L,QSA]
^^^ here
That will append / combine the original query string to the rewritten url.
I have the following .htaccess Rewrite rule below which works for converting virtual directories to parameters, for example:
www.example.com/usa/ny/nyc gets interpreted by PHP as www.example.com/index.php?path=usa/ny/nyc.
What I can't seem to figure out is how I would change my regex below to handle parameters of the virtual directories themselves. For example, I want:
www.example.com/usa/ny/nyc/?display=off&settings=none to be seen by PHP as www.example.com/index.php?path=usa/ny/nyc¶m=display:off,settings:none.
What makes it extra tricky is that the parameters won't always be those two options I used in the example above, they will change dynamically. Any ideas or suggestions of how to go about accomplishing this?
RewriteRule ^/?([a-zA-Z_\-/]+)$ index.php?path=$1 [L]
Assuming you want to pass the query string unmodified, you can use the [QSA] (query string append) option like so:
RewriteRule /(.+)$ /index.php?path=$1 [L,QSA]
You can find the documentation for the QSA option here. From the docs:
With the [QSA] flag, a request for /pages/123?one=two will be mapped
to /page.php?page=123&one=two. Without the [QSA] flag, that same
request will be mapped to /page.php?page=123 - that is, the existing
query string will be discarded.
So, your PHP script will see all the parameters as standard _$_GET parameters, rather than needing to do any other modification.
If you would prefer to treat the result more like a typical path element, you can use the following:
RewriteRule /(.+)$ /index.php/$1 [L,QSA]
In the above case, your query string will still be appended, however you will need to handle the path explicitly using $_SERVER['PATH_INFO'].
my .htaccess file works fine with the following url
http://www.mywebsite.com/product/category/Girls-Clothes
.htaccess file:
RewriteEngine On
RewriteRule ^product/category/([a-zA-Z0-9-/]+)$ /product/category/category.php?cid=$1
RewriteRule ^product/category/([a-zA-Z0-9-/]+)/$ /product/category/category.php?cid=$1
but when i use page number along with friendly url, it would not work
http://www.mywebsite.com/product/category/Girls-Clothes?pno=2
i have tow variables cid and pno, CID is mentioned in .htaccess but when i wtore "pno" its gives me sql error.
RewriteRule ^uk/category/([a-zA-Z0-9-/]+)$ /uk/category/category.php?cid=$1?pno=$1
RewriteRule ^uk/category/([a-zA-Z0-9-/]+)/$ /uk/category/category.php?cid=$1?pno=$1
please let me know where i am doing wrong
With your current attempt, you are replacing the value of pno with the same value in cid, "Girls-Clothes", and I suspect that isn't what you want.
Just use the QSA flag to append the existing query string onto the request, so the pno= is passed through the rewrite along with the cid parameter you added.
RewriteRule ^uk/category/([a-zA-Z0-9-]+)/?$ /uk/category/category.php?cid=$1 [L,QSA]
Notice also I reduced it to one line by appending /? and removing / from the [] to make the trailing slash optional.
I have the following rule:
RewriteRule ^(image/[0-9]*/(.*))?$ image.php?prettyUrl=true&nav=$1 [QSA,NC,L]
This is supposed to replace
http://mysite.com/image/12345/imagename.png => http://mysite.com/image.php?pretyUrl=true&nav=image/12345/imagename.png
For the particular case where the pretty url contains '/image/' the $_GET array is empty. If I use a different name like 'images' or 'asdfg' the $_GET array will contain both pretyUrl and nav.
Any idea why?
Thanks!
If the rewrite rule must work for every URL in image/, (and the /1234/ digits part is not required), then this may be what you are looking for:
RewriteRule ^(image(/.*)?)$ image.php?prettyUrl=true&nav=$1 [QSA,NC,L]
If you can phrase your question a bit more specifically, a better solution can be refined.