Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

php - Mod rewrite using .htaccess

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...