.htaccess - Forcing the rewriting of URLs by hiding .php AND .htm(l) extensions and redirecting to extensionless URLs -
here configuration:
- http://domain.com (obviously fictitious name...) hosted on server running apache mod_rewrite enabled
 - folder named "foo": (located @ http://domain.com/foo/ , in want put .htaccess file) containing 3 types of files .php, .htm, .html 
- .php files (for sake of example refer 1 of them: phpfile.php)
 - .htm files (for sake of example refer 1 of them: htmfile.htm)
 - .html files (for sake of example refer 1 of them: htmlfile.html)
 
 - within foo folder, no file has equivalent extension or without extension (ie eg neither phpfile.htm nor phpfile.html nor phpfile exist in foo, php.file.php exist)
 
here trying achieve:
- when entering http://domain.com/foo/phpfile.php or http://domain.com/foo/htmfile.htm or http://domain.com/foo/htmlfile.html in browser's address bar , hitting "enter": 
- i redirected 301 http://domain.com/foo/phpfile or http://domain.com/foo/htmfile or http://domain.com/foo/htmlfile (depending on file i've chosen), latters urls displayed in address bar
 
 - when entering http://domain.com/foo/phpfile or http://domain.com/foo/htmfile or http://domain.com/foo/htmlfile in browser's address bar , hitting "enter": 
- i don't redirected, nothing changes in address bar instead server serves me phpfile.php or htmfile.htm or htmlfile.html, depending on 1 requested
 
 
i have been trying hard on this, , sofar i've came content .htaccess file (located in "foo" folder), unfortunately working in last of 2 cases, in interested (ie serving "phpfile.php" when request "phpfile", serving "htmfile.htm" when request "htmfile" or serving "htmlfile.html" when request "htmlfile"), , ignoring 301 redirections:
options +followsymlinks rewriteengine on rewritebase /foo  # redirect permanently requests existing .php files # extensionless files (non present on server) # phpfile.php gets redirected phpfile rewritecond %{request_filename} !-d rewritecond %{request_uri} \.php$ rewritecond %{request_filename}\.php -f  rewriterule (.*)\.php$ $1   [r=301,nc]  # redirect permanently requests existing .htm(l) files # extensionless files (non present on server) # htmfile.htm gets redirected htmfile # htmlfile.html gets redirected htmlfile rewritecond %{request_filename} !-d rewritecond %{request_uri} \.html?$ rewritecond %{request_filename}\.html? -f    rewriterule (.*)\.html?$ $1 [r=301,nc]  # matching requests non existing extensionless files # existing equivalent on server # domain.com/foo/phpfile display # contents of domain.com/foo/phpfile.php, rewritecond %{request_filename}\.php -f  rewriterule (.*)$ $1.php    [l]  # domain.com/foo/htmlfile display # contents of domain.com/foo/htmlfile.html, rewritecond %{request_filename}\.html -f     rewriterule (.*)$ $1.html   [l]  # domain.com/foo/htmfile display # contents of domain.com/foo/htmfile.htm, rewritecond %{request_filename}\.htm -f  rewriterule (.*)$ $1.htm    [l]   thank in advance help/ advice.
there's logic flaw in first 2 rules in it's php or html file exists. uri check in effect duplicate of rewrite rule pattern , !f implies !-d. can fold these single rule:
rewritecond %{request_filename} -f rewriterule ^(.*?)\.(php|html?)$        $1   [r=301,nc]   the last 2 ok, i'd swap order if html requests more common php
why multiviews doesn't help
options +multiviews implements concept known content negotiation, , in doing apache invokes subquery parse filename root name.  1 of things scan directory known filename.extension combinations in case if xxx.php exists , request xxx substitute xxx.php , internal redirection, causes first rule fire, removing .php extension , causes error see.  
so (i) need disable multiviews, , (ii) ditto subqueries; (iii) detect , prevent retry loops. 1 solution want:
options +followsymlinks  -multiviews rewriteengine on rewritebase /foo  rewritecond %{env:redirect_end}  =1 rewriterule ^ - [l,ns]  rewritecond %{request_filename} -f rewriterule ^(.*?)\.(php|html?)$        $1   [r=301,nc,ns]  rewritecond %{request_filename}\.html -f rewriterule (.*)$ $1.html   [l,e=end:1,ns]  rewritecond %{request_filename}\.htm -f rewriterule (.*)$ $1.htm    [l,e=end:1,ns]  rewritecond %{request_filename}\.php -f rewriterule (.*)$ $1.php    [l,e=end:1,ns]      
Comments
Post a Comment