mod_rewrite not working for clean urls - php

I am using wamp server for my projects and have stuck in making the urls clean
currently my url address is like this
http://localhost/BookProjectFinal/booksdetails.php?pid=8
I want to make it like this
http://localhost/BookProjectFinal/booksdetails/8
What I am using in my .htaccess file is this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ booksdetails.php?pid=$1
RewriteRule ^([a-zA-Z0-9]+)/$ booksdetails.php?pid=$1
But its not working its returning the whole url. What the problem with it? Thanks

The RewriteBase parameter will tell mod_rewrite from which directory to start matching. This can be used for applications that live in subfolders of apache. You may want to set it to /BookProjectFinal/ here.
Your regexp is looking to match a complete alphanumeric URL optionally followed by a slash, which I'm not sure is what you want. Try something like:
RewriteRule bookdetails/([0-9]+) bookdetails.php?pid=$1
to match only the number. Here's a test:
RewriteEngine On
RewriteRule ^bookdetails/([0-9]+) bookdetails.php?a=b&pid=$1
Matches URLs of the form /bookdetails/8. Use print_r($_GET) to sanity check in bookdetails.php

Related

URL rewriting with spaces

I have a url like this :
www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe
I am trying to conver this like below with the help of .htaccess file :
www.qwerty.in/1/abcd-cafe
I am trying the below but unfortunately its not working . can anyone help
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ details.php?vendor_id=$1&name=$2
You character must allow space as well:
RewriteRule ^([\w-]+)/([\w-\s]+)$ details.php?vendor_id=$1&name=$2 [L,QSA]
I have used \w, which is same as [a-zA-Z0-9_].
The following rule will convert your URL from www.qwerty.in/details.php?vendor_id=1&name=abcd%20cafe to www.qwerty.in/1/abcd%20cafe:
RewriteRule ^([^/]*)/([^/]*)\.html$ /details.php?vendor_id=$1&name=$2 [L]
It it much more generalized however, and will convert the first two parameter values even if they are not vendor_id or name. If this is not the behavior you'd prefer, have a look at anubhava's answer.
Converting a space to underscore can be achieved using:
RewriteRule ^(.*)\s(.*)$ $1_$2 [N]
Also, don't forget to ensure the rewrite engine is online:
RewriteEngine On
If you want to further experiment with these rules, I'd recommend a website I recently discovered: http://www.generateit.net/mod-rewrite/

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]

Detect Single Segment RewriteRule apache

I'm stuck in a very basic thing, I'm try to redirect a single segment url to a page
RewriteRule /([A-Za-z0-9]+)$ index.php?c=profile&val=$1 [NC,L]
this works fine for URLs like
sitename.com/myurl
But the problem is its also valid for URLS like
sitename.com/myurl/something
or
sitename.com/myurl/something/x/y/z
I want it to only work for single segment after hostname, if I use it with ^ sign at the start it don't work at all
Any help would be greatly appreciated.
Thanks
You still have to use ^, but must leave out the leading /
RewriteRule ^([A-Za-z0-9]+)$ index.php?c=profile&val=$1 [NC,L]
For compatibility with older mod_rewrite versions you often see idioms like ^/?. But current Apaches mod_rewrite strips the leading slash already (from the REQUEST_URI which RewriteRules operate from primarily).

mod_rewrite accept only dashes & alphanumeric, otherwise throw 404

I'm using mod_rewrite to transfer the end of the URL to my php script which will act as a microcms. My url will look something like this:
mysite.com/articles/some-article-about-css
That will pass the following to my PHP script index.php:
index.php?action=read&article=some-article-about-css
I have questions:
I'm unsure how the best way to only accept alphanumeric w/ dashes using mod_rewrite. I found this rule on the internet ((?:[a-z]+)?(?:[0-9]+)?-?)+ and it apparently doesn't allow double dashes which is even better, but its long and confusing. Is there another (shorter, faster) rule I can use?
I'd like to verify that this rule is valid for what I'm trying to accomplish. I'm not very good with mod_rewrite:
RewriteRule ^/articles/((?:[a-z]+)?(?:[0-9]+)?-?)+ /index.php?action=read&article=$1
What rule can I use so that if the url after /articles/ is not a valid alphanumeric /w dashes url, automatically throw a 404 rather than passing to my script?
RewriteRule ^/articles/([a-zA-Z0-9-]*) /index.php?action=read&article=$1 [QSA,L]
RewriteRule ^/articles/ - [R=404]
For alphanumeric and dashes only
You dont need that complex regex i believe. Try this
RewriteRule ^articles/([^a-zA-Z0-9-]*) /index.php?page=$1 [L]
RewriteRule ^articles(.*) [R=404]

Can I use PHP and/or htaccess to rewrite a URL but not redirect?

Is there a way I can use PHP (and/or .htaccess) to rewrite the URL of a page.
For example if a user goes to www.mysite.com/french they are actually accessing the page which is www.mysite.com/index.php?lang=fr
But not a redirect.
You want to use mod_rewrite and an .htaccess file to achieve this.
RewriteEngine On
RewriteBase /
RewriteRule ^french/(.*)$ /index.php?lang=fr [L,QSA]
Yes, using Apache mod_rewrite and appropriate rules in an .htaccess file.
The docs on mod_rewrite are here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
on the Apache site you can find several examples of URL rewriting flavors, by the way it's enough to use something like this in an .htaccess file:
RewriteEngine on
RewriteRule ^french(/|)$ /index.php?lang=fr [flag]
Where [flag] can be one of the following:
[PT]
[L,PT]
[QSA]
[L,QSA]
You may want to have a look at the PT (passthrough) flag docs or RewriteRule flags docs.
Also, pay attention to what your links are pointing to: in fact, the RewriteRule first argument is a regular expression that will be used to match the URLs to be rewritten. At the moment,
^french(/|)$
matches "french", right after the domain name,followed either by a slash (/) or nothing (that's the meaning of (/|) ); that is, it'll match www.mysite.com/french and www.mysite.com/french/ but nothing else. If you need to parse query string arguments, or subpaths, then you may need a more complex regex.

Categories