homepage Welcome to WebmasterWorld Guest from 54.211.47.170
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Adding Trailing Slash
dwarfham




msg:3216931
 4:04 am on Jan 12, 2007 (gmt 0)

Hello

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]

 

jdMorgan




msg:3217277
 2:30 pm on Jan 12, 2007 (gmt 0)

Looks like you were going backwards with that rewrite, and had the slash on the wrong end of the URL, too.

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]

I'd also like to encourage you to check out the Apache forum section of the WebmasterWorld library [webmasterworld.com], and to search this site [webmasterworld.com] for answers. We've covered many subjects many times, and the previous threads [google.com] will likely save you having to wait around for an answer.

Jim

dwarfham




msg:3217307
 3:01 pm on Jan 12, 2007 (gmt 0)

I tried the following:

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

jdMorgan




msg:3217344
 3:36 pm on Jan 12, 2007 (gmt 0)

Oh yeah -- missed that. You can't use %{REQUEST_FILENAME} because of that, as there's no way to remove the trailing slash from it if already present.

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]

Note that the trailing slash, if present, is not passed to the file-exists check because it lies outside the parentheses that create the $1 back-reference.

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

dwarfham




msg:3217405
 4:07 pm on Jan 12, 2007 (gmt 0)

Hey 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

jdMorgan




msg:3217495
 5:13 pm on Jan 12, 2007 (gmt 0)

Does /phpexample/moreexample.php exist?

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

dwarfham




msg:3217516
 5:43 pm on Jan 12, 2007 (gmt 0)

Yes

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

jdMorgan




msg:3217981
 1:47 am on Jan 13, 2007 (gmt 0)

Please provide a "map" of examples of all types of requested URLs, the php scripts they should map to, and the calling parameters (name/value pairs) to be passed to the php scripts.

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved