g1smd - 9:57 am on Feb 27, 2013 (gmt 0)
I actually have this coding <snip> to "create" extensionless urls.
Mod_rewrite cannot "create" anything. YOU "create" extensionless URLs by linking to them on the pages of your site. Mod_rewrite deals only with "requests" after a link is clicked.
Once a URL request is sent to the server, mod_rewrite rules can be used to alter the internal pointer ("rewrite" it) to point to a different internal location to that suggested by the URL request.
You click and browser asks for
GET /page HTTP/1.1
Without mod_rewrite, Apache would try to pull content from file at
Since that doesn't exist, it would return "404 not found".
With the mod_rewrite rule present, the internal pointer is instead modified ("rewritten") to say
/public_html/site_name/www/page.php just before Apache attempts to fetch content.
This in no way stops a user requesting the page as
www.example.com/page.php with extension hence leaving you with a Duplicate Content problem to solve.
To fix that, you need another rule to redirect external requests for
www.example.com/page and you do that with a
RewriteRule for the redirect and a preceding
THE_REQUEST to be sure this was an external request for
/page.php and not an internal request as a result of a previous rewrite.
You should have 4 rulesets by now:
index.php requests on any hostname to root ("/") or to folder ("/folder/") on
- redirect other
.php requests on any hostname to extensionless on
- redirect all requests for pages on
- rewrite extensionless requests to fetch content from
All 4 of the rules should be coded using
RewriteRule. The 3 redirects will each have a preceding
RewriteCond (testing different things each time). The rewrite is a single line of code.
Mod_rewrite (when coded as a rewrite) doesn't make URLs for content. It works exactly backwards to that. It instead "translates" URL requests to find the content inside the server at a different location to that suggested by the path part of the incoming URL request.
When mod_rewrite is coded as a redirect, it tells the browser to make a new request for a different URL. It does this by returning the status code "301" along with the details of the new URL that the browser should request in place of the one it had just asked for.