Paging URL rewriting not working in php using .htaccess - php

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.

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]

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

URL rewriting is not working

I would like to use URL rewriting to a website.I had placed an .htaccess file in the server and turned on rewrite mode on and it seems to be working except one issue that I'm having.
I have two php extension files namely category.php and products.php resp.
Here is my requirement, the category.php should be called when one condition is met and products.php should be called another condition is meet
.HTACCESS:
RewriteRule ([A-Za-z-_]+) category.php?arg=name
RewriteRule ^([A-Za-z-_'']+)/([0-9]+) product.php?arg=name&pid=id
So her are my website url
FOR CATEGORY PAGES
http://www.mysite.com/category1
FOR PRODUCT PAGES
http://www.mysite.com/product-name/101
So the problem is with second url rewrite condition i.e product page url.When i put the ur l in browser it goes to 404.Whereas 1 rewrite condition seemed to work.Please help to access the webpage in the above format.
Problem is that in regex's character class hyphen should be either or start or at end otherwise it needs to be escaped (it will represent range otherwise).
Both of our rules:
RewriteRule ([A-Za-z-_]+) category.php?arg=name
RewriteRule ^([A-Za-z-_'']+)/([0-9]+) product.php?arg=name&pid=id
are not using correct regex and will produce wrong results.
Replace your rules with this code:
RewriteRule ^([a-z_-]+)/?$ /category.php?arg=$1 [L,NC,QSA]
RewriteRule ^([a-z_'-]+)/([0-9]+)/?$ /product.php?arg=$1&pid=$2 [L,NC,QSA]
PS: I have made some more corrections in the rule to handle unexpected situations better.
You need to add boundaries to your regex and backreference your captured groupings:
RewriteRule ^([A-Za-z-_]+)$ category.php?arg=$1 [L]
RewriteRule ^([A-Za-z-_'']+)/([0-9]+)$ product.php?arg=$1&pid=$2 [L]

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.

rewriterule mixed regular query and clean URL?

I am trying to get a regular query appended to any clean URL to be parsed properly. I've spent the last 3 hours trying to attempt this to no avail.
/aa/bb/?f=ff
/aa/bb/cc/dd/ee/?f=ff&g=gg
should become
a=aa&b=bb&f=ff
a=aa&b=bb&c=cc&d=dd&e=ee&f=ff&g=gg
instead I get
a=aa&b=bb
a=aa&b=bb&c=cc&d=dd&e=ee
This is my htaccess line:
RewriteRule ^([a-z]+)/([a-z]+)/?([a-z0-9]+)?/?([a-z0-9]+)?/?([a-z]+)?/?\??(.*)?$ index.php?a=$1&b=$2&c=$3&d=$4&e=$5&$6 [NC,L]
If you are wondering why I want to do this, it is to accommodate some libraries which do not support clean URLs very well.
Use the QSA flag, eg
RewriteRule ^...$ foo.php [NC,L,QSA]

Categories