Forum Moderators: phranque

Message Too Old, No Replies

clean url help

differentiating between files and directories

         

puddletown design

4:24 pm on Feb 21, 2007 (gmt 0)

10+ Year Member



This is going to be an easyone. I am really new to working with .htaccess files.

I have a site built in php, the php is only used to generate the markup. There is no database.

I am trying to create clean urls
(eg mysite.com/about.php becomes mysite.com/about)

by using this code in the htaccess file on the root directory


RewriteEngine On
RewriteRule ^([^\.\?]+)(\?.*)?$ $1.php

however i have run into a problem

i have a file called portfolio.php in the root directory and a subdirectory in the same folder called portfolio where each section of the portfolio is contained. so the desired effect would be mysite.com/portfolio/project1

the problem is now when i go to mysite.com/portfolio it tries to open the portfolio directory instead of the php file.

how can i fix this?

i thought about setting the DirectoryIndex of the portfolio directory to ../portfolio.php however that breaks my stylesheet and many other urls in included elements on the page.

jdMorgan

5:45 pm on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteEngine On
RewriteRule ^([^\.\?]+)(\?.*)?$ $1.php

What, in detail, is this rule intended to do? I'm asking because it appears that you are testing for a "?" -- a character that will never appear in the URL-path itself (it's part of the QUERY_STRING, which, if present, must be handled separately with a RewriteCond). Both the "?" and the "." are also escaped, which isn't necessary within [groups].

So, I can't really recommend anything with assurance, because I've no idea what URLs this pattern is intended to match and, more importantly, to NOT match...

Taking a guess, I'd say that this might work better:


# Enable mod_rewrite, disable MultiViews
Options +FollowSymLinks -MultiViews
# Turn on the rewrite engine
RewriteEngine on
# Rewrite URL-paths not ending with a filetype (".") or trailing slash (in any {sub}directory) to <URL-path>.php
RewriteRule ^(([^/]+/)*([^./]+))$ $1.php [L]

If you do not need or want to pass the subdirectory path to xyz.php, then you can simplify the rule to:

# Rewrite URL-paths not containing a filetype (".") or slash to <URL-path>.php
RewriteRule ^([^./]+)$ $1.php [L]

Note that in both cases, any appended query string (?foo=blah&zap=wow) will be passed through the rule unchanged -- It will be appended to xyz.php.

Jim

puddletown design

3:36 am on Feb 22, 2007 (gmt 0)

10+ Year Member



thanks for the help jdMorgan and dealing with someone who is a noob to these things, i really appreciate it. though, i don't think i explained my problem very well.

so this is my directory structure


public_html/
about.php
portfolio.php
portfolio/
project1.php
project2.php
contact.php

the htaccess rule i posted above i just cut and pasted and has worked. i have little experience with regular expressions so i don't fully understand the rule. all i want to do is remove the .php from the end of the uri.

my problem is there is a name conflict with
portfolio.php
portfolio/

so when i type mysite.com/portfolio i get a 403 because it is trying to read the directory instead of the file.

how do i make mysite.com/portfolio read the php file and not the directory?

phranque

6:38 am on Feb 22, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



# Enable mod_rewrite, disable MultiViews
Options +FollowSymLinks -MultiViews
# Turn on the rewrite engine
RewriteEngine on
# Rewrite URL-paths not ending with a filetype (".") or trailing slash (in any {sub}directory) to <URL-path>.php
RewriteRule ^(([^/]+/)*([^./]+))$ $1.php [L]

given your clarification i would try jdmorgan's solution and report the results...

puddletown design

8:49 am on Feb 22, 2007 (gmt 0)

10+ Year Member



I get the same results as the original rules. A 403 for mysite.com/portfolio

The browser tries the uri and gets nothing, then and adds the trailing slash, then reports a 403.