PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Using Apache's mod_rewrite to hide scripts extension
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Using Apache's mod_rewrite to hide scripts extension

Réponse
 
LinkBack Outils de la discussion
Vieux 28/04/2008, 09h46   #1
Álvaro G. Vicario
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Using Apache's mod_rewrite to hide scripts extension

With Apache's mod_rewrite module I can keep *.php files and load them as
*.html files:

# Make http//example.com/foo/bar.html load /home/site/foo/bar.php
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]

But, is there any elegant way to prevent direct access to
"http//example.com/foo/bar.php"? No matter the "L" flag, all *.html
requests get parsed again with any *.php rule I write, even though the
rewriting does not force a browser redirect and even if I use the NS
flag :-?

I still can restrict it in PHP itself (I can think of checking the value
of $_SERVER['REQUEST_URI']), but I'd like to understand why L and NS do
not work as I expected.


Sorry if this is kinda offtopic but my news server does not provide any
Apache group.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
  Réponse avec citation
Vieux 28/04/2008, 11h25   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

Álvaro G. Vicario wrote:
> With Apache's mod_rewrite module I can keep *.php files and load them as
> *.html files:
>
> # Make http//example.com/foo/bar.html load /home/site/foo/bar.php
> RewriteEngine On
> RewriteRule ^(.*)\.html$ $1.php [L]
>
> But, is there any elegant way to prevent direct access to
> "http//example.com/foo/bar.php"? No matter the "L" flag, all *.html
> requests get parsed again with any *.php rule I write, even though the
> rewriting does not force a browser redirect and even if I use the NS
> flag :-?
>
> I still can restrict it in PHP itself (I can think of checking the value
> of $_SERVER['REQUEST_URI']), but I'd like to understand why L and NS do
> not work as I expected.
>
>
> Sorry if this is kinda offtopic but my news server does not provide any
> Apache group.
>
>


You can always get the Apache newsgroups through Google Groups. And
I've found most news servers will add groups if you ask them nicely -
they just need to know the newsgroup is wanted.

As for your question - it is NOT a good idea to parse all .html files as
..php, anyway. It puts an unnecessary load on the server. Just keep php
code in .php files. You'll find it works much better.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 28/04/2008, 11h54   #3
Álvaro G. Vicario
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

Jerry Stuckle escribió:
> As for your question - it is NOT a good idea to parse all .html files as
> .php, anyway. It puts an unnecessary load on the server. Just keep php
> code in .php files. You'll find it works much better.


There aren't any plain HTML files in the site: they all use PHP to
compose the page including headers, footers and the like (plus headers
for client-side caching: site contents are not actually dynamic).
Currently, I code in *.html files and get them all parsed as PHP but
it's annoying because I have to tell my text editor to use PHP syntax
highlighting for *.html files... but only for that project! Furthermore,
if I ever add a plain HTML file I can't distinguish it easily.

I thought mod_rewrite could be an elegant solution but I'm disappointed
with my lack of understanding of its internals.

If I completely omit extensions (http://example.com/foo/bar) the issue
is the same: I can't prevent http://example.com/foo/bar.php from loading.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
  Réponse avec citation
Vieux 28/04/2008, 12h17   #4
C. (http://symcbean.blogspot.com/)
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

On 28 Apr, 11:54, "Álvaro G. Vicario"
<alvaroNOSPAMTHA...@demogracia.com> wrote:
> Jerry Stuckle escribió:
>
> > As for your question - it is NOT a good idea to parse all .html files as
> > .php, anyway. It puts an unnecessary load on the server. Just keep php
> > code in .php files. You'll find it works much better.

>
> There aren't any plain HTML files in the site: they all use PHP to
> compose the page including headers, footers and the like (plus headers
> for client-side caching: site contents are not actually dynamic).
> Currently, I code in *.html files and get them all parsed as PHP but
> it's annoying because I have to tell my text editor to use PHP syntax
> highlighting for *.html files... but only for that project! Furthermore,
> if I ever add a plain HTML file I can't distinguish it easily.
>
> I thought mod_rewrite could be an elegant solution but I'm disappointed
> with my lack of understanding of its internals.
>
> If I completely omit extensions (http://example.com/foo/bar) the issue
> is the same: I can't preventhttp://example.com/foo/bar.phpfrom loading.
>
> --
> --http://alvaro.es- Álvaro G. Vicario - Burgos, Spain
> -- Mi sitio sobre programación web:http://bits.demogracia.com
> -- Mi web de humor al baño María:http://www.demogracia.com
> --


Wouldn't it be simpler to just rename all your files as .html and
setup the PHP handler to process them?

C.
  Réponse avec citation
Vieux 28/04/2008, 12h23   #5
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

Álvaro G. Vicario wrote:
> Jerry Stuckle escribió:
>> As for your question - it is NOT a good idea to parse all .html files
>> as .php, anyway. It puts an unnecessary load on the server. Just
>> keep php code in .php files. You'll find it works much better.

>
> There aren't any plain HTML files in the site: they all use PHP to
> compose the page including headers, footers and the like (plus headers
> for client-side caching: site contents are not actually dynamic).
> Currently, I code in *.html files and get them all parsed as PHP but
> it's annoying because I have to tell my text editor to use PHP syntax
> highlighting for *.html files... but only for that project! Furthermore,
> if I ever add a plain HTML file I can't distinguish it easily.
>
> I thought mod_rewrite could be an elegant solution but I'm disappointed
> with my lack of understanding of its internals.
>
> If I completely omit extensions (http://example.com/foo/bar) the issue
> is the same: I can't prevent http://example.com/foo/bar.php from loading.
>
>
>


And you will NEVER have any static pages on the site?

Why are you going to all of the trouble, anyway. PHP works fine as .php
files. You cause yourself and the server to run through all kinds of
hoops to parse them as .html files.

But if you insist, try alt.apache.configuration - available on Google
Groups.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 28/04/2008, 12h29   #6
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

Álvaro G. Vicario wrote:
> Jerry Stuckle escribió:
>> As for your question - it is NOT a good idea to parse all .html files
>> as .php, anyway. It puts an unnecessary load on the server. Just
>> keep php code in .php files. You'll find it works much better.

>
> There aren't any plain HTML files in the site: they all use PHP to
> compose the page including headers, footers and the like (plus headers
> for client-side caching: site contents are not actually dynamic).
> Currently, I code in *.html files and get them all parsed as PHP but
> it's annoying because I have to tell my text editor to use PHP syntax
> highlighting for *.html files... but only for that project! Furthermore,
> if I ever add a plain HTML file I can't distinguish it easily.
>
> I thought mod_rewrite could be an elegant solution but I'm disappointed
> with my lack of understanding of its internals.
>
> If I completely omit extensions (http://example.com/foo/bar) the issue
> is the same: I can't prevent http://example.com/foo/bar.php from loading.
>
>
>


Oops - I think I misread what you are doing (twice!).

If I understand this correctly now, you have your PHP code in .php
files, and have Apache set up to load those PHP files if the equivalent
..html is called. This would be OK.

But you don't want the .php files to be able to be loaded directly. I'm
not sure what difference it makes - does it cause a problem to load the
..php file?

From the PHP end, yes, you can restrict access. But if you want to do
it with mod_rewrite, you really need to be following up on an Apache
newsgroup. You'll get expert answers there.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 30/04/2008, 07h25   #7
Tim Roberts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

Jerry Stuckle <jstucklex@attglobal.net> wrote:
>
>Oops - I think I misread what you are doing (twice!).
>
>If I understand this correctly now, you have your PHP code in .php
>files, and have Apache set up to load those PHP files if the equivalent
>.html is called. This would be OK.
>...
>But you don't want the .php files to be able to be loaded directly. I'm
>not sure what difference it makes - does it cause a problem to load the
>.php file?
>
> From the PHP end, yes, you can restrict access. But if you want to do
>it with mod_rewrite, you really need to be following up on an Apache
>newsgroup. You'll get expert answers there.


mod_rewrite is one of those magical products that can be configured to do
practically anything (like Sendmail). And like most such products, it is
difficult to get right.

However, I think this will do what the original poster wanted:

RewriteEngine On
RewriteRule ^(.*)\.php$ $1.php [R=404,L]
RewriteRule ^(.*)\.html$ $1.php [L]

That says PHP references should redirect to a 404, and HTML references
should be rewritten to PHP.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
  Réponse avec citation
Vieux 30/04/2008, 08h18   #8
Álvaro G. Vicario
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

Tim Roberts escribió:
> RewriteEngine On
> RewriteRule ^(.*)\.php$ $1.php [R=404,L]
> RewriteRule ^(.*)\.html$ $1.php [L]
>
> That says PHP references should redirect to a 404, and HTML references
> should be rewritten to PHP.


It doesn't work either because when I request foo.html I'm silently
redirected to foo.php, rewriting starts again and first rule generates a
Not Found status code.

Thank you for your suggestion but it's really tricky!

I've asked my newsmaster if I can get "alt.apache.configuration" added
to the groups lists (Google Groups is okay but it does not allow e-mail
obfuscation) but I guess I'll implement this as is and I just won't care
about direct access to *.php files (or I'll check
$_SERVER['REQUEST_URI'] and generate the 404 status in PHP). Your
comparison of mod_write with Sendmail has scared me to the bone ;-)


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
  Réponse avec citation
Vieux 30/04/2008, 13h03   #9
Guillaume
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

Álvaro G. Vicario a écrit :
> Tim Roberts escribió:
>> [L]

> rewriting starts again


Or not, L flag stands for Last, it shouldn't start again and directly
serve the request, i.e. foo.php.

Regards,
--
Guillaume
  Réponse avec citation
Vieux 30/04/2008, 19h01   #10
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

..oO(Guillaume)

>Álvaro G. Vicario a écrit :
>> Tim Roberts escribió:
>>> [L]

>> rewriting starts again

>
>Or not, L flag stands for Last, it shouldn't start again and directly
>serve the request, i.e. foo.php.


The L flag means that no further rules are evaluated for the _current_
request, but if some rewriting action triggers an internal redirect,
then the entire process starts again from the first RewriteRule.

Micha
  Réponse avec citation
Vieux 01/05/2008, 23h16   #11
M. Strobel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

Ãlvaro G. Vicario schrieb:
> Tim Roberts escribió:
>> RewriteEngine On
>> RewriteRule ^(.*)\.php$ $1.php [R=404,L]
>> RewriteRule ^(.*)\.html$ $1.php [L]
>>
>> That says PHP references should redirect to a 404, and HTML references
>> should be rewritten to PHP.

>
> It doesn't work either because when I request foo.html I'm silently
> redirected to foo.php, rewriting starts again and first rule generates a
> Not Found status code.
>
> Thank you for your suggestion but it's really tricky!
>
> I've asked my newsmaster if I can get "alt.apache.configuration" added
> to the groups lists (Google Groups is okay but it does not allow e-mail
> obfuscation) but I guess I'll implement this as is and I just won't care
> about direct access to *.php files (or I'll check
> $_SERVER['REQUEST_URI'] and generate the 404 status in PHP). Your
> comparison of mod_write with Sendmail has scared me to the bone ;-)
>
>


If you want to deny execution of php scripts, you can put them outside
of the web space.

If your php script you want to protect is inside the web space, you can
deny access in the .htaccess file. This is handy for include-only
directories.

Max
  Réponse avec citation
Vieux 02/05/2008, 02h04   #12
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using Apache's mod_rewrite to hide scripts extension

M. Strobel wrote:
> Ãlvaro G. Vicario schrieb:
>> Tim Roberts escribió:
>>> RewriteEngine On
>>> RewriteRule ^(.*)\.php$ $1.php [R=404,L]
>>> RewriteRule ^(.*)\.html$ $1.php [L]
>>>
>>> That says PHP references should redirect to a 404, and HTML references
>>> should be rewritten to PHP.

>>
>> It doesn't work either because when I request foo.html I'm silently
>> redirected to foo.php, rewriting starts again and first rule generates
>> a Not Found status code.
>>
>> Thank you for your suggestion but it's really tricky!
>>
>> I've asked my newsmaster if I can get "alt.apache.configuration" added
>> to the groups lists (Google Groups is okay but it does not allow
>> e-mail obfuscation) but I guess I'll implement this as is and I just
>> won't care about direct access to *.php files (or I'll check
>> $_SERVER['REQUEST_URI'] and generate the 404 status in PHP). Your
>> comparison of mod_write with Sendmail has scared me to the bone ;-)
>>
>>

>
> If you want to deny execution of php scripts, you can put them outside
> of the web space.
>
> If your php script you want to protect is inside the web space, you can
> deny access in the .htaccess file. This is handy for include-only
> directories.
>
> Max
>


Which is not his question...

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h18.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,25477 seconds with 20 queries