Old 05-09-2008, 11:54   #1 (permalink)
MrD
Senior Member
 
Join Date: Aug 2008
Posts: 109
mod_rewrite problems

Howdy. I'm having some mod_rewrite problems. I'm trying to make my company's site URL friendly. In doing so, I want each page to be http://site.com/page/

The problem isn't getting the site to do that, the problem is with my PHP scripting.

Code:
<?php $page = $_GET["p"]; if(!file_exists('content/'.$page.'.php') || $page == null) include("content/home.php"); else include("content/".$page.".php"); ?>

Now, that in and of itself is a great error detection setting. However, when mixed with this .htaccess file:
Code:
RewriteEngine on RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1 ErrorDocument 404 /index.php

I get problems accessing folders. Like for example, I want to get to http://site.com/proof/

it brings me to index.php. How should I get around this?
  Reply With Quote
Old 06-09-2008, 17:17   #2 (permalink)
feha
Design Destroyer
 
feha's Avatar
 
Join Date: Aug 2008
Location: Somewhere in Universe
Posts: 198
Hi, make sure first if you don't have a page named "proof" in your php or db ...

following code should work for you
Code:
DirectoryIndex index.php index.html RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)\.html$ index.php?p=$1 [L,QSA] RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1 ErrorDocument 404 index.php

This will work for as static pages, example
/mypage.html
or
/mypage
but
problems would be
/mypage/ (at least on my server ...)

Hope this helps
Quote:
I get problems accessing folders. Like for example, I want to get to http://site.com/proof/

it brings me to index.php. How should I get around this?
If that folder or file does not exist than it will redirect you to index.php as
Code:
ErrorDocument 404 /index.php
Tells to server to do that ...
__________________
SIGNATURE ?
  Reply With Quote
Old 07-09-2008, 12:49   #3 (permalink)
MikeMackay
Shun the non-believer
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Essex, UK
Posts: 1,788
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
Code:
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
Could be combined to:

Code:
RewriteRule ^([a-zA-Z0-9--]+)(/{0,1})$ index.php?p=$1

Basically it says that there could be a '/' appearing between 0 and 1 times (meaning that it may or may not be there).

- Mike
__________________
  Reply With Quote
Old 07-09-2008, 15:15   #4 (permalink)
MrD
Senior Member
 
Join Date: Aug 2008
Posts: 109
That's cool. But then what happens if I have http://site.com/proof/

And that directory has it's own index.php file? The mod_rewrite will only tell the server(?) that there is no proof.php file in /content/ so go to the homepage. Not that there is a /proof/ folder with it's own junk inside of it.
  Reply With Quote
Old 07-09-2008, 15:23   #5 (permalink)
feha
Design Destroyer
 
feha's Avatar
 
Join Date: Aug 2008
Location: Somewhere in Universe
Posts: 198
Hmm
Code:
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
The problem is in these regular expressions ...
I tried on my server and the problem was same, I could not reach my existing directory.
So the reg-ex must be changed ...
I'm not sure which one would work right.
But I'm sure the problem is the reg-ex, I can't spot it.
__________________
SIGNATURE ?

Last edited by feha : 07-09-2008 at 15:58.
  Reply With Quote
Old 08-09-2008, 10:18   #6 (permalink)
feha
Design Destroyer
 
feha's Avatar
 
Join Date: Aug 2008
Location: Somewhere in Universe
Posts: 198
Code:
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d d (is directory) Treats the String as a pathname and tests if it exists and is a directory. -f (is regular file) Treats the String as a pathname and tests if it exists and is a regular file.
So it should work ... but ...
__________________
SIGNATURE ?
  Reply With Quote
Old 08-09-2008, 13:05   #7 (permalink)
feha
Design Destroyer
 
feha's Avatar
 
Join Date: Aug 2008
Location: Somewhere in Universe
Posts: 198
I have tested this solution:
Code:
RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /index.php?p=$1 [L,QSA] ErrorDocument 404 index.php

This works well and finds a real directory if there is one ...
The problem is it works only with trailing slash ...
domain.com/page/ but not domain.com/page
must adjust the rule ...
__________________
SIGNATURE ?
  Reply With Quote
Old 09-09-2008, 04:47   #8 (permalink)
MikeMackay
Shun the non-believer
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Essex, UK
Posts: 1,788
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
How about:
Code:
RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)(/{0,1})$ /index.php?p=$1 [L,QSA] ErrorDocument 404 index.php

?
__________________
  Reply With Quote
Old 09-09-2008, 05:22   #9 (permalink)
Synook
What happened?
 
Synook's Avatar
 
Join Date: Jul 2008
Posts: 1,827
By the way {0,1} is equivalent to ?
__________________
Web developer • AspektasBlogTwitter


  Reply With Quote
Old 09-09-2008, 06:31   #10 (permalink)
MikeMackay
Shun the non-believer
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Essex, UK
Posts: 1,788
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
Quote:
Originally Posted by Synook
By the way {0,1} is equivalent to ?
It means that the aforementioned character(s) may occur 0 or 1 time(s).

In this example, it would match the string whether a forward slash is present (occurs 1 time) or not (occurs 0 times).

- Mike
__________________
  Reply With Quote
Old 09-09-2008, 08:03   #11 (permalink)
feha
Design Destroyer
 
feha's Avatar
 
Join Date: Aug 2008
Location: Somewhere in Universe
Posts: 198
Code:
? (generally) means zero or one times .. * means zero or one times .. + means one or more times ..
__________________
SIGNATURE ?
  Reply With Quote
Old 09-09-2008, 08:35   #12 (permalink)
feha
Design Destroyer
 
feha's Avatar
 
Join Date: Aug 2008
Location: Somewhere in Universe
Posts: 198
and here you can find probably a solution for your problem
Url Routing with PHP - Part One : phpaddiction
__________________
SIGNATURE ?
  Reply With Quote
Old 10-09-2008, 00:01   #13 (permalink)
Synook
What happened?
 
Synook's Avatar
 
Join Date: Jul 2008
Posts: 1,827
Quote:
Originally Posted by MikeMackay
It means that the aforementioned character(s) may occur 0 or 1 time(s).

In this example, it would match the string whether a forward slash is present (occurs 1 time) or not (occurs 0 times).

- Mike
LOL

I meant that in a regular expression the character sequence "{0,1}" is equivalent to the single character "?". For example, the two following expressions will match the same strings:
Code:
/a{0,1}/
Code:
/a?/
As feha said.
__________________
Web developer • AspektasBlogTwitter


  Reply With Quote
Old 10-09-2008, 04:34   #14 (permalink)
MikeMackay
Shun the non-believer
 
MikeMackay's Avatar
 
Join Date: Feb 2005
Location: Essex, UK
Posts: 1,788
Send a message via MSN to MikeMackay Send a message via Skype™ to MikeMackay
Ha ha ha, Ok I get ya now - but you can see how easily I mis-read that !

Either that I was just just being a plain idiot....
__________________
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive
Web Hosting by Heart Internet, vBulletin © 2000-2009 Jelsoft Enterprises Limited.
Search Engine Optimization by vBSEO 3.0.0 RC8
Web Hosting by Heart Internet