URL rewriting is not working - php

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]

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 RewriteEngine not working for secondary GET

I'm creating friendly links in my site, but I have a problem. I wrote this, but only the first line of code is working, and I'n not sure why:
RewriteRule ^user/([0-9a-zA-Z]+) user.php?user=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z]+)/options user.php?user=$1&options [NC,L]
When I go to test.com/user/tom everything is fine, but when I try test.com/user/tom/options it just loda the same page.
I have php script that should load different pages, for different $_GET, and its working if used with normal links.
The correct thing to do here is not to swap the lines around. Whilst that may fix your problem, it will also send user/tom/foo/bar to the first rule. The reason it does this is because the pattern is not closed with $. As such, your rules should read as follows:
RewriteRule ^user/([0-9a-zA-Z]+)$ user.php?user=$1 [NC,L]
RewriteRule ^user/([0-9a-zA-Z]+)/options$ user.php?user=$1&options [NC,L]
Tip: If you also have other pages for the user, you can simplify the second rule to include different segments by means of a capture:
RewriteRule ^user/([0-9a-zA-Z]+)/(options|other-page)$ user.php?user=$1&$2 [NC,L]
Also giving consideration to the fact that you are using the NC (no case) flag, you can drop the A-Z in your capture groups. You can also use \d for digits. Example [a-z\d]+

Redirect if URL match defined strings using .htaccess

I want to redirect a if URL match defined strings using .htaccess in a single line code
If URL is
http://example.com/mobiles/xyz/
http://example.com/computers/xyz/
http://example.com/books/xyz/
Redirect URL
http://example.com/comingsoon/
I tried this but i want to define string (e.g. books, mobiles, books) instead of (.*)
RewriteRule ^(.*)/xyz/?$ upcoming/index.html [NC,L]
Don't know if i've understand correctly the question, but this should do the trick:
RewriteRule ^mobiles|books|computers/xyz/?$ upcoming/index.html [NC,L]
Tested on https://regex101.com/

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