PHP friendly url .htaccess - search page - php

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.

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]

mod rewrite rule not passing key values

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.

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.

.htaccess $_GET specification

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.

.htaccess with php hidden parameters and language selection

I can't seem to get my .htaccess file to route the urls to my site correctly. I have a number of languages people can choose from wanting URL's like:
http://www.domain.com/en/
http://www.domain.com/en/contact
But I can't seem to get the page 'contact' working when writing a rule to get the 'en' variable.
RewriteRule /([^/]+)/([0-9]+)/ index.php?language=$1
I use that to grab the language code but how could I get the contact page to work?
EDIT:
Apparently I needed some QSA option but now the language get variable grabs contact as the variable with the en
RewriteRule ^(.*)$ index.php?language=$1 [QSA,L]
With this rule the site:
http://www.domain.com/en/contact
Returns:
en/contact
EDIT2
What I am trying to accomplish is the directory structure:
/
/contact
/about
Having these folders in the root but grabbing and ignoring the /en/ language variable. So I don't need a second variable for &page=contact, I need it to route into the directory folder.
Try combining your two expressions, although you need to modify the second group - [0-9]+ will only match numbers, not words like contact. Try this:
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?language=$1 [QSA,L]
The QSA option allows a query string to be appended to the clean URL, perhaps something like this:
http://www.domain.com/en/contact?to=support&subject=Hello
In response to your comment, this expression should do the trick:
RewriteRule ^([^/]*)/([^/]+)/?$ $2/index.php?language=$1 [QSA,L]
In the rewritten rule, $2 holds contact, for example, and $1 holds en. The former is used as the directory, and the latter as an argument in the query string.

Categories