views:

83

answers:

4

I am updating a php app, which currently does not use url rewriting. The aim is to hide file extensions. The general structure of the website is as follows

root/
  index.php
  login.php
  page1.php
  page2.php
  page3.php
  page4.php
  page5.php
  page6.php
  page7.php
  page8.php
subfolder/
  index.php
  validpage.php
images/
css/

The subfolder folder in the above structure is unique in that it is the only subfolder containing php files.All of the other files are in the root folder.

I have been through these questions http://stackoverflow.com/questions/788188/mod-rewrite-and-php and http://stackoverflow.com/questions/1868154/mod-rewrite-or-php-router and http://stackoverflow.com/questions/265898/mod-rewrite-php-and-the-htaccess-file

Updated htaccess. thanks to @thomasmalt

RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -l 
RewriteRule ^.*$ - [NC,L]


# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s 

# add .php to the url     
RewriteRule ^(.*)$ $1.php

Problem now is I get a 500 instead of a 404 error, when I try to access a non existent page like localhost/inex.php. The Apache error log gives me the following errors. (I accessed localhost/index which loaded the contents of index.php , followed by which I loaded localhost/inex which gave me a 500 error.)

[Sat Sep 25 14:44:26 2010] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/index
[Sat Sep 25 14:44:36 2010] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

Update: found the answer to my question here. The question is resolved.

+1  A: 
  1. It send you to root because you have RewriteBase / what defining default base ... If you want to stay in subfolder just make another .htaccess in subfolder with RewriteBase /subfolder/ or remove it
  2. you can make condition like you want

    RewriteRule ^(([0-9]+)-)?page(-([0-9]+))?/?$ index.php?page=$2&id=$4 [L]

this take you from browser www.abcd.com/2-page-3 to: www.abcd.com/index.php?page=2&id=3

  1. Code in index.php no need contain enything
jatt
thanks for the reply. I read something in the other similar questions on stackoverflow, which said that it is better to let php handle the redirection, while redirecting every page to index.php. and I have updated the question.
abel
+1  A: 

From experience, it's harder than it sounds to retro-fit mod_rewrite into an existing site.

This isn't because mod_rewrite is hard -- actually it's relatively easy to set it up and get your new URLs working. The reason it's hard is because your existing site will have all the old URLs hard-coded into it, so you'll have to go through your code changing them all to point to the new URL format - this can be especially tricky if your code builds up URL strings dynamically. You'll need to do lots of testing to make sure you've picked up every possible variation and that they all still work.

Also worth mentioning that search engines and external sites will have historic links in to your site using the old URLs, so you can't get rid of them completely.

I know that doesn't answer your question, but I believe these are important things to consider before you start the project, because for an existing site it isn't the quick-fix that some people make it out to be.

Spudley
+2  A: 

I'm not totally sure what you're trying to achieve, and i suspect, no disrespect intended, that you need to understand both mod_rewrite, and the way the webserver and php parses and executes files. I'm not sure how to answer without shooting over or under your level of understanding, but I'll try.

I can tell you what you're current .htaccess is doing: (pseudocode)

if REQUEST_FILENAME does not exist then
    rewrite request to /index.php/<the request>

Since index.php is your php script that is executed and everything put in $1 is ignored. That's why index.php is executed whatever you do.

What you could try to put in your .htaccess is this:

RewriteCond %{REQUEST_FILENAME} !-s 
RewriteRule ^(.*)$ $1.php

That should take the request and try to append .php to the end of it. I haven't tested it unfortunately, but you should give it a try. The result should be something like this:

if request is "/news/article" 
    translate it to "/news/article.php"

And you should read the mod_rewrite documentation twice a day for a month, and buy the O'Reilly book about Regular Expressions.

And if you have administrator access to the development server you should look into

RewriteLogLevel
RewriteLog
thomasmalt
thanks @thomasmalt. I appreciate it. I have tried reading http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html but haven't completely understood much of it. :) and thanks for the pseudocode.loved it.
abel
@thomasmalt the rule works like a charm. however it does not handle folder names very well. if i try to access localhost/ or localhost/subfolder/ i get an 500 error. anything i can do for that.
abel
@abel yeah. The easiest thing you can do with your current setup is to put RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d [OR]RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -lRewriteRule ^.*$ - [NC,L]at the start of your mod_rewrite configuration (each cond and rule on its own line)
thomasmalt
It tells mod_rewrite to pass through (not change) any requests where a file (-f) a directory (-d) or symbolic link with a valid tarket (-l) exists.
thomasmalt
worked well. i have updated the .htaccess file . there is a problem though. i get a 500 error instead of a 404 error, while trying to access a non existent file like index.phpa. see original post.
abel
+3  A: 

Apache has an option: Options +MultiViews

If you enable this, you don't need to place the file-extention in the url.

VeeWee
Oh. Excellent answer. I'm voting you up like a rocket :) -- http://httpd.apache.org/docs/2.2/content-negotiation.html
thomasmalt
will this work on a Hostgator shared hosting?
abel
Only one way to figure this out: Try it on Hostgator.
VeeWee