/    Sign up×
Community /Pin to ProfileBookmark

Can someone please explain this to me?

Hi, i’ve been wondering for a while how its done but have always put it down to being a different programming language.

Today i see it again in the Zend Framework website but now i know for sure that its PHP: URLs that end with no file name or directory e.g.

[url]http://framework.zend.com/behind-the-site[/url]

If you put ‘.php’ at the end then it’s not a file, if you put ‘/index.php’ then you find its not a directory – so what is it, how is it done?

Also, killing two birds with one stone, on my home server if i have a file at /dir/filename.php and i access it like so /dir/filename then it still works. I have not seen this before either and it does not appear to work on any other servers – what setting allows that to happen? (Apache servers on Linux btw)

Thanks in advance

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@Four_StaplesJun 07.2009 — It's called mod_rewrite...

http://www.workingwith.me.uk/articles/scripting/mod_rewrite

It's an Apache module.
Copy linkTweet thisAlerts:
@pavsidauthorJun 07.2009 — Ah that's what it's all about.. thanks!

Its not mod_write thats responsible for whats happening on my home server though is it?
Copy linkTweet thisAlerts:
@UltimaterJun 07.2009 — The .htaccess file in Zend Framework looks as follows:
<i>
</i>RewriteEngine On
RewriteCond &amp;#37;{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Here's a [b]Hello, World![/b] example:

http://www.webdeveloper.com/forum/showthread.php?p=982568#post982568
Copy linkTweet thisAlerts:
@pavsidauthorJun 07.2009 — Many thanks, some good reading there

What about my home server though? I don't have mod_rewrite installed so how is it that /dir/filename evaluates to /dir/filename.php? It's pretty nifty and would like to have it set on the production server thats the only reason im interested to know
Copy linkTweet thisAlerts:
@UltimaterJun 07.2009 — Locally, I use xampp installed at C:xampp.

To enable mod rewrite, open:

C:/xampp/apache/conf/httpd.conf

then find and uncomment the line:
LoadModule rewrite_module modules/mod_rewrite.so
After making the change, reboot the server so it uses the new conf.

For me, I open the xampp control panel C:/xampp/xampp-control.exe

Then I click [b]SCM...[/b] and find [b]Apache2.2[/b] in the list of services and click [b]restart[/b] the service. Afterwards mod_rewrite works.

Locally my .htaccess file actually reads:
<i>
</i>Options +FollowSymLinks

&lt;IfModule !mod_rewrite.c&gt;
ErrorDocument 403 '&lt;center&gt;&lt;h1&gt;500 Internal Server Error&lt;/h1&gt;&lt;blockquote&gt;&lt;code style="font-size:large;"&gt;&lt;strong style="color:red;"&gt;mod_rewrite.c&lt;/strong&gt; module &lt;em&gt;required&lt;/em&gt; by .htaccess!&lt;/code&gt;&lt;br&gt;&lt;br&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;ol style="text-align:left;"&gt;&lt;li&gt;&lt;code&gt;Open &lt;em&gt;httpd.config&lt;/em&gt;&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Add the line &lt;strong&gt;LoadModule rewrite_module modules/mod_rewrite.so&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;Save changes&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;Restart web server to reload config settings&lt;/code&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/blockquote&gt;&lt;/center&gt;'
Deny from all
# triggers above 403 Forbidden
&lt;/IfModule&gt;
&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On
RewriteCond &amp;#37;{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
&lt;/IfModule&gt;
Copy linkTweet thisAlerts:
@pavsidauthorJun 07.2009 — Ultimater, thanks for your comments however it's not how to install mod_rewrite that i'm asking, besides i use linux anyway so i guess install will be quite different, but thanks anyway.

What i was wondering was how come when i access, for example, [B]http://localhost/testfile[/B] does it display[B] http://localhost/testfile.php[/B] automatically? If i was to do the same on my hosting account server then i would get a file not found error

It can't be anything to do with mod_rewrite in this instance as i don't have it installed - it must be a setting somewhere that allows it..
Copy linkTweet thisAlerts:
@UltimaterJun 07.2009 — There is no such setting that can do that other than mod_rewrite.c that I am aware of... But it could be caused by a millon things...

Perhaps 404 requests are handled by CGI? Read your [b]httpd.config[/b] file.

A possible way would be:
<i>
</i>ErrorDocument 404 "/cgi-bin/missing_handler.pl"

or
<i>
</i>ErrorDocument 404 /missing.php


From such a file, it would be easy to read [i]getenv("REQUEST_URI")[/i] and redirect the user based on the requested name. It is also possible it is just directory indexing:
<i>
</i>&lt;IfModule dir_module&gt;
DirectoryIndex index.php index.php4 index.php3 index.cgi index.pl index.html index.htm index.shtml index.phtml
&lt;/IfModule&gt;

Or perhaps your server really does have mod_rewrite enabled and its using it but you didn't know to look in httpd.config rather than .htaccess?

Or...perhaps .... ehh... I'm not psychic. But if you wish to simulate an equivalent for the web, look into RewriteEngine.

For example, I'm using mod_rewrite while developing a forum software.

http://ultimater.net/members/avatars/username/Ultimater

is sent to http://ultimater.net/index.php then from there, it reads the MembersController.php and finds the avatarsAction method, then passes it the argument username set to "Ultimater" and pulls up my avatar from the database and outputs an image.
Copy linkTweet thisAlerts:
@pavsidauthorJun 07.2009 — Thanks a lot, pretty sure its not mod_rewrite as no such module coming up on phpinfo() - however, will investigate your other suggestions

Thanks again for your thorough response
Copy linkTweet thisAlerts:
@bokehJun 07.2009 — If you put '.php' at the end then it's not a file, if you put '/index.php' then you find its not a directory - so what is it, how is it done?[/QUOTE]Although this can be done with mod_rewrite that requires writing rewrite rules. On the other hand if you use MultiViews no rules are needed and you just omit the file extension from the URL.
Copy linkTweet thisAlerts:
@pavsidauthorJun 07.2009 — Although this can be done with mod_rewrite that requires writing rewrite rules. On the other hand if you use MultiViews no rules are needed and you just omit the file extension from the URL.[/QUOTE]

**Multiviews** that's the one, thanks bokeh, you've just eased my headache (believe it or not)!
Copy linkTweet thisAlerts:
@UltimaterJun 08.2009 — MultiViews[/QUOTE]
:eek: I've seen that before... I feel stupid.
×

Success!

Help @pavsid spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...