Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite - php to htm

what do I with the "old" php file?

         

skipfactor

10:47 pm on Dec 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks to some great posts I've managed to convert my largest site from asp to php and mod_rewrite in a matter of days. I will never do another site on an MS server--this stuff is pure magic to a Windows(X!) guy. That said, I now know about enough to be dangerous:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder/page\.htm$ /folder/page.php [L]
RewriteRule ^id(.*)\.htm$ /detail.php?id=$1 [L]

This works for me but can I redirect those that actually link to or try to type in 'page.php' to 'page.htm'? (my efforts created a loop). In other words, how do I properly hide 'page.php' and 'detail.php?' to users and search engines?

jdMorgan

12:24 am on Dec 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Avoid the use of the ambiguous and therefore inefficient ".*" pattern whenever possible. Try this. If it works (in the context of your site's page-naming requirements), it'll be much faster:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder/page\.htm$ /folder/page.php [L]
RewriteRule ^id([^.]+)\.htm$ /detail.php?id=$1 [L]

In order to avoid the looping problem, you have to be aware of a fact that's not real obvious from reading the documentation: mod_rewrite in an .htaccess context behaves recursively. In other words, you have to write the code so that it won't loop if it is re-invoked after it runs the first time. This is what happens, in fact, because the server needs to check to make sure that a newly-rewritten URL is not subject to access restrictions or further rewrites. It's a common misconception that the [L] flag stops mod_rewrite processing for the current request. In fact, it only stops mod_rewrite processing for this current pass through mod_rewrite for the current request.

So, to avoid the loop you've experienced, it is necessary to look at the original client request, so that your rules aren't fooled into rewriting a previously-rewritten URL.


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /page\.php\ HTTP/
RewriteRule ^page\.php$ http://www.example.com/page.html [R=301,L]

Because this code examines the original client request, it won't interact with your other rewrites above.
THE_REQUEST is the entire request header sent by the browser, for example:

GET /page.php HTTP/1.1

Jim

skipfactor

4:09 pm on Dec 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Both of your suggestions worked, thanks! It takes me about a day to somewhat understand one line of code but I'm giddy with these new-to-me toys.

Apache vs. Windows reminds me of what an old skateboard friend said the first time we went snowboarding: "This is everything I wanted to do on a skateboard but couldn't".

Thanks again Jim.

jdMorgan

4:45 pm on Dec 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have or anticipate having future needs for more rewrites, then by all means, dig into the documentation with the code and application above as a working example; Use what you know about what the code accomplishes on your server to help you understand how the code functions, and do this now. This good solid example that you can 'watch as it works' will give you an advantage when compared to just studying the documentation as a dry academic exercise.

You will find that writing mod_rewrite code is much, much easier than reading it. If you have a clearly-defined requirement, it's easy to come up with the code. But if you are looking at code, and trying to figure out what the requirement was, it's pretty tough.

The hard part about answering questions in this forum is to figure out:
1) What the poster wants to do
2) How the poster's files relate to his URLs, and how those files are organized in directories
3) How the poster's server is configured (different hosting companies do it differently)

The actual coding part is comparitively easy...

Jim