4

I am trying to Rewrite my mode using this info[copy from a website], but it's not working .. what's wrong with it and plz give a correct answer..

The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123

RewriteEngine on
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]

Another example, rewrite from:

www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/

RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2
9
  • Well, how are you using it in your site? What do current URLs look like etc? Copying code from a site and expecting us to fix it isn't how SO works. Please provide info about your setup and what you've tried Commented Oct 23, 2013 at 11:02
  • i am trying in local wampserver, but nothing happend when i visit localhost/abc/product.php?id=23 , it's not changed Commented Oct 23, 2013 at 11:04
  • Have you enabled the ModRewrite module in apache for WAMP? Commented Oct 23, 2013 at 11:04
  • yaa i did , i enabled it Commented Oct 23, 2013 at 11:05
  • did you restart WAMP after enabling? Just trying to eliminate any minor issues causing it Commented Oct 23, 2013 at 11:08

2 Answers 2

4

You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.

Quite simply when you browse to:

 localhost/abc/product.php?id=23

The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL

URL Transformation

http://www.yoursite.com/product/123         <- URL that user goes to
http://www.yoursite.com/product.php?id=123  <- Rewritten URL that the server sees

RewriteRule(s) explanined

A rewrite rule is broken down into three parts (not including the RewriteRule part...)

  1. The regex that it matches against the url
  2. The url that it transforms into
  3. Additional options

Given your rule:

RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]

The regex is: ^product/([^/.]+)/?$
The new url : product.php?id=$1
The options : [L]

This means that the user browses to the nice url http://www.yoursite.com/product/123 (and all of your links point to the nice URLs) and then the server matches against the regex and transforms it to the new URL that only the server sees.

Effectively, this means that you have two URLs pointing to the same page... The nice URL and the not-nice URL both of which will take you to the same place. The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.


Regex

The reason why http://mysite.com/product/image.jpg is not being redirected is because of the regex that you use in your RewriteRule.

Explanation

^product/([^/.]+)/?$
  • ^ => Start of string
  • product/ => Matches the literal string product/
  • ([^/.]+) => A capture group matching one or more characters up until the next / or .
  • /?$ => Matches an optional / followed by the end of the string

Given the URL:

http://mysite.com/product/image.jpg

Your regex matches product/image but then it encounters . which stops the matching... As that isn't the end of string $ the rule is invalidated and thus the transform never happens.

You can fix this by changing your character class [^/.] to just [^/] or - my preferred method - to remove the character class and simply use .+ (seeing as your capturing everything to the end of the string anyway

RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/(.+)/?$ product.php?id=$1 [L]
Sign up to request clarification or add additional context in comments.

6 Comments

thanx all working,,, damn i missed this conecpt... thanx you saved my work
Can you give an example of a file (css/image) that isn't working?
all working well , i just need to use change abc/product.php?id=121 to abc/121 and css and images also linked in both types of URLs.. thanx
but how i do this mysite.com/abc/product.php?id=mypic.jpg into mysite.com/abc/mypic.jpg
Okay, to clarify, I'm assuming you're using a script to generate the image? So you want users to go to: http://mysite.com/abc/image.jpg and then the script to see http://mysite.com/product.php?id=image.jpg?
|
0

Try

RewriteRule ^product/([a-zA-Z0-9_]+)/?$ product.php?id=$1 [L]

4 Comments

i used this but still it's not working.. :( :'( localhost/f/product.php?id=121 , nothing happened
Okay but what isnt working? Are you getting an error? Is it displaying the page? Are you sure you are getting item information based on the $id variable?
no error, page load as it should.. it's like this code has no effect at all, no error, no change , where should i put this .htaccess file in abc folder or root of localhost
in abc/ folder. But you shouldnt be going to localhost/abc/product.php?id=121 you should be going to localhost/abc/product/121

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.