Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.</p> <p>Quite simply when you browse to:</p> <pre><code> localhost/abc/product.php?id=23 </code></pre> <p>The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the <strong>wrong</strong> URL</p> <h2>URL Transformation</h2> <pre><code>http://www.yoursite.com/product/123 &lt;- URL that user goes to http://www.yoursite.com/product.php?id=123 &lt;- Rewritten URL that the server sees </code></pre> <h2>RewriteRule(s) explanined</h2> <p>A rewrite rule is broken down into <em>three</em> parts (not including the <code>RewriteRule</code> part...)</p> <ol> <li>The regex that it matches against the url</li> <li>The url that it transforms into</li> <li>Additional options</li> </ol> <p>Given your rule:</p> <pre><code>RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L] </code></pre> <p>The regex is: <code>^product/([^/.]+)/?$</code><br> The new url : <code>product.php?id=$1</code><br> The options : <code>[L]</code></p> <p>This means that the user browses to the <em>nice</em> url <code>http://www.yoursite.com/product/123</code> (and all of your links point to the <em>nice</em> URLs) and then the server matches against the regex and transforms it to the new URL that <strong>only the server sees</strong>.</p> <p>Effectively, this means that you have two URLs pointing to the same page... The <em>nice</em> URL and the <em>not-nice</em> URL both of which will take you to the same place. The difference is that the <em>not-nice</em> / <em>standard</em> URL is hidden from the general public and others pursuing your site.</p> <hr> <h2>Regex</h2> <p>The reason why <code>http://mysite.com/product/image.jpg</code> is not being redirected is because of the regex that you use in your <code>RewriteRule</code>.</p> <h3>Explanation</h3> <pre><code>^product/([^/.]+)/?$ </code></pre> <ul> <li><code>^</code> => Start of string</li> <li><code>product/</code> => Matches the literal string <code>product/</code></li> <li><code>([^/.]+)</code> => A capture group matching <em>one or more</em> characters up until the next <code>/</code> or <code>.</code></li> <li><code>/?$</code> => Matches an optional <code>/</code> followed by the end of the string</li> </ul> <p>Given the URL:</p> <pre><code>http://mysite.com/product/image.jpg </code></pre> <p>Your regex matches <code>product/image</code> but then it encounters <code>.</code> which stops the matching... As that isn't the end of string <code>$</code> the rule is invalidated and thus the transform never happens.</p> <p>You can fix this by changing your character class <code>[^/.]</code> to just <code>[^/]</code> or - my <em>preferred</em> method - to remove the character class and simply use <code>.+</code> (seeing as your capturing everything to the end of the string anyway</p> <pre><code>RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L] RewriteRule ^product/(.+)/?$ product.php?id=$1 [L] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload