473,545 Members | 721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
--
Jun 2 '08 #1
11 3350
Á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.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #2
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
--
Jun 2 '08 #3
On 28 Apr, 11:54, "Álvaro G. Vicario"
<alvaroNOSPAMTH A...@demogracia .comwrote:
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.
Jun 2 '08 #4
Á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.conf iguration - available on Google
Groups.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #5
Á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.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #6
Jerry Stuckle <js*******@attg lobal.netwrote:
>
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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 2 '08 #7
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.con figuration" 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
--
Jun 2 '08 #8
Á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
Jun 2 '08 #9
..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
Jun 2 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2509
by: Joshua | last post by:
Is there a way to set up Apache to invoke a PHP equest when a file (a image for example) is downloaded? Say, then, that http://www.foo.com/downloads/app1.zip is downloaded, Id like to have Apache in turn invoke a PHP script which may be used to write a log. Thanks, Joshua
4
2564
by: Phil Powell | last post by:
http://www.sitepoint.com/article/910/2] How can it be possible to use Apache's mod_rewrite module in such a way as to dynamically feed it variable information into the "RewriteRule" option? I am having to get info from a db call and then feed it into RewriteRule since the rule will be, to a degree, dynamic. Is this what I would be doing;...
0
2042
by: Stefan Blobner | last post by:
Hi, I'm searching for someone who's using HP-UX 11.x with Apache and PHP with the OCI8 extension (for accessing Oracle). I'm having some issues while running this and would like to exchange some information.
0
1929
by: Frank | last post by:
Hey all, I can't seem to get javascript running in my XSL document. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <!--The component and its script are in the xalan namespace and define...
3
18319
by: Joseph S. | last post by:
Hi, I am trying to install PHP 5.0.4 on Apache 2.0.54 on WinXP Pro SP2 as a cgi binary. Apache2 directory is c:/Apache2 htdocs is c:/Apache2/htdocs php is installed in c:/php This contains php.ini, php-cgi.exe and php5ts.dll and other files required for the module method of php installation are also present,
6
7960
by: MaiyaHolliday | last post by:
Hello, I've recently installed apache on a new computer, and cannot figure out why my site will not process any includes. (it was working on my old one) There are no errors on the page such as "include not found", rather much of the page is blank where the included menus, etc would be, and the CSS is not attached. thanks so much!!! ...
3
4927
by: joe jacob | last post by:
I configured apache to execute python scripts using mod_python handler. I followed below mentioned steps to configure apache. 1. In http.conf I added <Directory "D:/softwares/Apache2.2/htdocs"> AddHandler mod_python .py PythonHandler mptest PythonDebug On </Directory>
7
1731
by: Dale | last post by:
again, i know this is OT...just move along to the next post if it bugs you. :) i had been trying to have this: project.66.204.32.110 from the client browser, map to a virtual host where the document root would be:
6
7583
by: josequinonesii | last post by:
I've searched, I've read, I've tested and re-read numerous post but to no avail yet... Quite simply, the settings I've applied to my httpd.conf, httpd-vhost.conf and my hosts files simply does not work. Please review my work and let me know if you see that one little thing that is throwing me for a loop. HTTPD.CONF # # This is the main...
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7805
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.