Forum Moderators: phranque

Message Too Old, No Replies

Why does my page not display?

         

chrisguk

7:27 pm on Mar 27, 2012 (gmt 0)

10+ Year Member



I have 5 web pages at the root of my folder called for example:

index.php
about.php
contact.php
order.php
home.php
about/profiles.php

All of the pages are called by the index.php using GET method like so:

<?php
$page = isset($_GET['p']) ? $_GET['p'] : 'home';

switch($page) {


/*----------------------- PAGES -----------------------------------*/

case 'about':
$title = 'about';
$keyword = 'some keywords';
$description = 'some description';
break;

case 'contact':
$title = 'contact';
$keyword = 'some keywords';
$description = 'some description';
break;

case 'order':
$title = 'order';
$keyword = 'some keywords';
$description = 'some description';
break;

case 'about/profiles':
$title = 'order';
$keyword = 'some keywords';
$description = 'some description';
break;

default:
$title = 'home';
$keyword = 'some keywords';
$description = 'some description';
break;

}
include('include/header.php');

include(''.$page.'.php');

include('include/footer.php');
?>


My links in the webpages look like this:


<a href="<? echo $url; ?>/about">About</a>
<a href="<? echo $url; ?>/about/profiles">Profiles</a>
<a href="<? echo $url; ?>/home">Home</a>
<a href="<? echo $url; ?>/contact">Contact</a>
<a href="<? echo $url; ?>/order">Order</a>
<a href="<? echo $url; ?>/about/profiles">Profiles</a>


I have rewritten my URLs with MOD_rewrite in a .htaccess file which you can see below:

RewriteEngine On
Rewritebase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^robots\.txt$
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]


Everything works fine when I navigate through contact, order and home. But when I select "about" it gives me the following message:

Index of /about
[ICO] Name Last modified Size Description
[DIR] Parent Directory -
[ ] profiles.php 26-Mar-2012 17:15 1.3K
Apache/2.2.20 (Ubuntu) Server at mysite Port 80

I have a few links on the about.php page also that point to [mysite...]

lucy24

9:55 pm on Mar 27, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For starters:

This filename
about/profiles.php

does not fit this pattern
RewriteRule ^([^/.]+)/?$

(I deleted the escape-slash because you don't need it inside grouping brackets in mod_rewrite.)

In the Condition
RewriteCond $1 !^robots\.txt$

you don't need $1. You can say %{REQUEST_URI}. Never capture if you can get the same information by non-capturing means; it just makes more work for the server and increases the chance of error.

The Rule itself needs to be more narrowly constrained. As written, it would catch every single request-- images, stylesheets, javascript, everything. There is probably a lot more, but that will do to start with.

chrisguk

10:17 pm on Mar 27, 2012 (gmt 0)

10+ Year Member



Hi there,

So I made those changes like below:

RewriteEngine On
Rewritebase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^robots\.txt$
RewriteRule ^([^/]+)/? index.php?p=$1 [L]


I am very new on this so would you be able to advise on how I get the right pattern to take care of files at the root for example about.php and home.php and take care of files stored in a directory/folder like about/profiles.php?

lucy24

10:38 pm on Mar 28, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have 5 web pages at the root of my folder called for example:

index.php
about.php
contact.php
order.php
home.php
about/profiles.php

:: counting on fingers ::

Oops. Ran out of fingers there ;) Further along, you've got

:: detour here to ask moderators to confirm that this is strictly an Apache question and not a PHP question ::
<a href="<? echo $url; ?>/about">About</a>
<a href="<? echo $url; ?>/about/profiles">Profiles</a>
<a href="<? echo $url; ?>/home">Home</a>
<a href="<? echo $url; ?>/contact">Contact</a>
<a href="<? echo $url; ?>/order">Order</a>
<a href="<? echo $url; ?>/about/profiles">Profiles</a>

Double whoops. You seem to have two Profiles. Is something missing?

By amazing coincidence, I have only just run up some boilerplate on the redirect-to-rewrite two-step, which I assume is what you're trying to do. Take a detour over here [webmasterworld.com]. Note the complete absence of cut-and-paste code. The goal is to get the concept so you can write your own.

:: pause here to twiddle thumbs while you go next door and read slowly ::

Now then.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^robots\.txt$
RewriteRule ^([^/\.]+)/?$

Hm, I smell cut-and-paste RewriteCond from somewhere. The !-f and !-d are last-resort conditions that are rarely necessary; you just have to write the Rule carefully.

Here you've excluded requests for robots.txt. This is very proper but it's in the wrong place. It goes in an envelope near the top of your htaccess, like this:
<Files "robots.txt">
Order Allow,Deny
Allow from all
</Files>


Since you have a small, fixed number of pages, you can simply list them in the Rule itself:

(index|about|contact|order|home|profiles)


This by itself lets you get rid of all three Conditions. The Rewrite will probably not have any Conditions at all. (This is the Gold Standard for mod_rewrite.) If you have read what I told you to read ;) you will see that the Redirect side of the two-step needs to have at least one Condition.

Notice that I changed "about/profiles" to simply "profiles"? This part of the Rule is only concerned with what the user sees, so you don't have to put the fake URL inside a directory just because that's where the real URL lives. Let your php script take care of that detail. Besides, it can be perilous to have files and directories with the same name ("about"). Do as I say. Not as I do.

chrisguk

5:03 pm on Apr 1, 2012 (gmt 0)

10+ Year Member



Hi Lucy,

Sorry it has taken me so long to reply but I was in the middle of moving to Germany.

So, yes you are right I placed profiles in twice and that is a mistake. Profiles.php is contained within the sub folder /about.

Unfortunately I dont only have a fixed number of pages. I have up to and not including the ones im writing still, around 40 - 50 pages.

The only real thing that I am trying to achieve is this:

The user selects the link on the home page like below:

http://www.example.com/about/

They navigate to the about page and they see a profile they would like to view, so they select; profiles:

http://www.example.com/about/profiles/

So that is how I want my URLs to look.

I have one index.php at the document root pulling the relevant pages in. And dont have an index.php at each folder level as there could be only maybe one or sometimes two files in the folder.

My original .htaccess file was written for my for my old website and I kinda just reused it and tried to develop my new site the same way.

Obviously I haven't managed to do this.

What my problem is, is that I have read Apache Docs over and over again and I still dont get it or what I am supposed to write to achieve my aim. I normally learn by demonstration and explanation if that makes sense.

I would appreciate if you are able to look at this again for me and guide me in novice terms what Im supposed to be doing?

Regards

Chris

g1smd

9:15 pm on Apr 1, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Move your included content to a /content/ folder.

If the requested URLs to be rewritten are folder-based as well as in root, use this pattern: ^(([^/]+/)*[^/.]+)$ to match extensionless requests both in root and in folder.

For "pages" do not end your URLs with a trailing slash. Trailing slash denotes a "folder".

Consider using an array to hold your titles and another for your meta descriptions.

Use extensionless URLs for your pages. You can set things so that any request for a URL with an extension is served by a real file and extensioness requests are rewritten to be dealt with by your PHP script.

Your PHP script MUST return 404 for non-valid requests.

lucy24

12:07 am on Apr 2, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can set things so that any request for a URL with an extension is served by a real file and extensioness requests are rewritten to be dealt with by your PHP script.

Recurring worry about the extensionless-URL approach: mod_rewrite tends to run before mod_dir.* Does that mean you have to put in the time-consuming !-d Condition to make sure you're not grabbing requests for bona fide directories that happened to arrive without trailing slash?


* I tested this experimentally on my real (shared hosting) page. In MAMP, mod_alias and mod_rewrite are the last two mods to load, which I think means they're the first to execute.

g1smd

6:36 am on Apr 2, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've not found that to be the case.

Request
example.com/about
and the 'about' page from the CMS is displayed retrieved from
/index.php?page=about
.

Request
example.com/images
and you're redirected to
example.com/images/
and a list of files in the 'image' folder is shown.

chrisguk

8:05 am on Apr 2, 2012 (gmt 0)

10+ Year Member



After some discussion I tested the process above and it worked fine, I have adopted a new structure to my folder and file system to train my brain to forget any relation between folder structure and URL output/requests.

I now want to improve my knowledge and learn how to make my own custom URLs. I guess I need to study Regex a little more first?