.htaccess $_GET specification - php

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.

Related

Simple pretty urls with .htaccess

I have a one page blog php website.
Content is dynamicaly loaded based on get parameters. I would like to use my htaccess to make pretty urls. I have these urls:
website.com/index.php?category=review&page=1
And I would like to have this:
website.com/category/review/page/1
And I also use article as get parameter. So I would like to change this:
website.com/index.php?article=12345-name-of-article
To this:
website.com/article/12345-name-of-article
I am totally new to htaccess, so any help would be appreciated.
I tried this rewrite rule:
RewriteRule ^article/([0-9a-zA-Z_-]+)$ index.php?article=$i [NC,L].
It worked somehow, but php script does not recognize url parameters. So it does not work.
Thank you very much!
You need to use QSA - 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. :
RewriteEngine On
RewriteRule ^article/([\w-]+)(?:\.html|/)?$ index.php?article=$1 [NC,QSA,L]
RewriteRule ^category/([\w-]+)/page/([\w-]+)(?:\.html|/)?$ index.php?category=$1&page=$2 [NC,QSA,L]

Passing variables to a well format URL

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.

Paging URL rewriting not working in php using .htaccess

I am using this rule in .htaccess :-
RewriteRule ^online-sale on-sale.php
RewriteRule ^online-sale/page-([0-9]+)$ on-sales.php?page=$1
First rule is working fine. for eg. if i call http://www.sitename/online-sale than page is opening successfully. When i am calling http://www.sitename/online-sale/page-2 than page is opening fine, but I can't access $_REQUEST["page"] value on this page.
Can anyone suggest me what is the problem? Is it possible or not?
Thanks in advance.
You need to use anchor $ in first rule to avoid matching for paging URL as well:
RewriteRule ^online-sale/?$ on-sale.php [L]
RewriteRule ^online-sale/page-([0-9]+)/?$ on-sale.php?page=$1 [L,QSA]
It is also advisable to use L and QSA flags.
QSA (Query String Append) flag preserves existing query parameters while adding a new one.

Passing $_GET variables to virtual directories via .htaccess

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&param=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'].

PHP friendly url .htaccess - search page

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.

Categories