Forum Moderators: phranque
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.
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]
# Rewrite URL-paths not containing a filetype (".") or slash to <URL-path>.php
RewriteRule ^([^./]+)$ $1.php [L]
Jim
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?
# 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]