Forum Moderators: phranque
Please excuse my newbiness as I am trying to learn this. So this works for www.example.com/home but when www.example.com/home/ is entered, it doesnt work.
I added another line thinking that it would work (the last line #Add Slash) but it is not
Am I supposed to write another RewriteCond inorder to do a command that would add the trailing /?
HTACCESS:
# Check whether requested URL-path exists as a file when ".php" is appended to it
RewriteCond %{REQUEST_FILENAME}\.php -f
# Rewrite URL-paths which do not contain a period by appending ".php" (if the file exists)
RewriteRule ^([^.]+)$ /$1.php [L]
# Add Slash
RewriteRule ^([^/]+)$ /$1.php?name=$1 [L]
Delete your "added" line, and modify the last rule with this to make it accept an optional trailing slash in the request:
# Rewrite URL-paths which do not contain a period by appending ".php" (if the file exists)
RewriteRule ^([^.]+[b])/?$[/b] /$1.php [L]
Jim
php_value register_globals 0
#
RewriteEngine on
RewriteBase /
#
# Check whether requested URL-path exists as a file when ".php" is appended to it
RewriteCond %{REQUEST_FILENAME}\.php -f
# Rewrite URL-paths which do not contain a period by appending ".php" (if the file exists)
RewriteRule ^([^.]+)/?$ /$1.php [L]
Original is home.php
However when I go to www.example.com/home it works but when I enter www.example.com/home/ I get a 404 error that says:
The requested URL /home/.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The trailing slash is important because I have other links that goes to www.example.com/home/example
I will read the document again and do a search. Thanks Jim
This should work on most servers:
# Check whether requested URL-path exists as a file when ".php" is appended to it
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite URL-paths which do not contain a period by appending ".php" (if the file exists)
RewriteRule ^([^.]+)/?$ /$1.php [L]
Do not add "\" to escape characters in the literal arguments to RewriteCond -- The left-side argument to RewriteCond is not a regular expressions pattern. The right-side argument may be a regex pattern, but never the left -- it is always taken as a literal.
Jim
I decided to use this which works with either www.example.com/phpexample or www.example.com/phpexample/:
RewriteEngine on
RewriteBase /
#
# Check whether requested URL-path exists as a file when ".php" is appended to it
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite URL-paths which do not contain a period by appending ".php" (if the file exists)
RewriteRule ^([^.]+)/?$ /$1.php [L]
It works like a charm. However, calling anything beyond that is not working:
example : www.example.com/phpexample/morexample
I get an error that says: The requested URL
phpexample/moreexample was not found on this server
the htaccess is reading it as a "url" when it should be reading from the phphexample.php
If not, the rule won't rewrite requests for /phpexample/moreexample URL-paths, and you'll get the result you report.
If you are trying to rewrite phpexample/moreexample to phpexample.php, discarding the /morexample path-part, or if you need to do something with the /moreexample path-part (such as pass it as a named parameter to /phpexample.php), then you need to say so.
mod_rewrite will rewrite URLs corresponding to the exact pattern and conditions given, nothing more, nothing less. The problem is not with the code implementation per-se, but rather that the requirements are not fully specified.
Jim
What I am attempting to do is to pass it as a named parameter to /phpexample.php
For instance, I have a filed called category.php that allows me to create catagories such as "Cars"
When I hide the .php via htaccess www.example.com/category or www.example.com/category/ works great
However, www.example.com/category/cars do not work
cars.php is not a file...it is a category name created from category.php
Basically, it doesnt seem to work with more than one /test/ parameter (another example: /image/user/bob.jpg)doesnt work
An example map (not intended to be "correct" but rather to provide an example):
/phpexample/ -> /phpexample.php
/phpexample/moreexample -> /phpexample.php?category=moreexample
/category/cars/ -> /category.php?category=cars
/image/fred/bob.jpg -> image.php?user=fred&image=bob.jpg
Note that my example "image" URL breaks the restriction I described on the URLs in my first code post.
This list does not need to be repetitive, but it needs to be comprehensive of all 'classes' of requested URLs. It is important to make clear what all of the variables are, and what their acceptable 'types' are: letters, numbers, hyphens, underscores, -- what they can contain and what they cannot contain.
It is equally important to specify what URLs *should not* be rewritten, examples being /robots.txt, /w3c/p3p.xml, etc.
Since the scope of this thread has apparently grown enormously, we can provide examples that you can work from, but as stated in our charter, we can't write the code for you here. That's not our purpose.
Jim