问题描述:

I had this setup:

images

image1.jpg

image2.jpg

header.html

about

index.php

image3.jpg

But going to MyWebsite.com/About gave it an extra slash on the end. I decided to go with the solution of creating a file called about in my home directory:

images

image1.jpg

image2.jpg

header.html

about

aboutfiles

image3.jpg

The problem is that now this file won't let me use .php:

<?php include('header.html');?>

It's not showing the header file. What can I do to make this work?

网友答案:

Your problem is that your web server does not recognize the filename "about" as a php document. You have three options

  1. Use a ".php" extension on your page document, such as "index.php" which will run your php scripting.
  2. Install a solution such as mod_rewrite that will translate urls such as /about to a file actual like "about.php".
  3. Adjust your servers mime-type for php documents. Learn more about MIME types here. https://developer.mozilla.org/en-US/docs/Properly_Configuring_Server_MIME_Types

The first solution is the simplest and easiest, in any document you use PHP, the extension should be .php

网友答案:

About the mywebsite.com/about issue, it's more an Apache configuration issue. You have to tell Apache that index.php should be an index file.

网友答案:

First, you can configure your Apache (or IIS) to use whatever Extensions to process PHP-Code. You can define .ThisIsAPHPFile as valid extension, if you want.

However, Directorys are always reflected with a trailing /: www.example.com/dir1/ (Browsers not always showing the trailing /) while files have an extension: www.example.com/dir1/index.html.

So, from what i see, you want to use www.example.com/about but showing the about-FILE ?

Therefore you can use rewrite Engines of your Webserver. Either have a look at mod-rewrite (http://httpd.apache.org/docs/current/en/mod/mod_rewrite.html), when using Apache, or (one possibility) ISAPI-Rewrite (http://www.isapirewrite.com/docs/), when using IIS.

网友答案:

If you edit your virtualhost directive in your apache conf file, you can add the following:

DefaultType application/x-httpd-php 

This will tell apache to send web paths that do not end in an extension to render using the php engine.

Therefore, /about would act as if it was about.php. Another potentially more useful approach is to name the file about.php on the server, and allow referencing it without the .php in the url. For this, you would configure it the opposite way.

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
相关阅读:
Top