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

Apache Web Server Forum

Mod rewrite trailing slash issue
Virtual URLs

 8:28 am on Jun 17, 2007 (gmt 0)

I've seen many examples of trailing slash mod rewrite rules on this site but none that seem to address my problem.

I use the following redirect rule for all URLs on my site so that they fit in with my CMS:

RewriteRule %{REQUEST_FILENAME} index.php [L,NC]
RewriteRule ^([^\.]+)(/)?$ index.php?x=$1 [L,NC]

Now, although domain.com/foo/bar may work, domain.com/foo/bar/ will 404.

How can I make it so that both work (or they rewrite to the non-trailing slash version in certain directories like files in "foo")



 3:04 pm on Jun 18, 2007 (gmt 0)

That code is badly-broken, such that I cannot tell what was intended -- whether these were supposed to be two different rules, or if the first line was supposed to be a RewriteCond. All I can suggest is this for the second line:

RewriteRule ^(([^/]+/)*(^./)+)/?$ index.php?x=$1 [L]

This will rewrite any requested URL containing any number of subdirectories (including zero) plus one subdirectory or extensionless filepath-part with or without a trailing slash to the index.php file with variable 'x' set to the entire path but dropping any trailing slash.

The [NC] flag is not needed, since no literal alphabetic characters are specified in the pattern.



 7:47 pm on Jun 19, 2007 (gmt 0)

Hmm that rule lead to my pages giving a 404. I don't think the top rule should be a rewritecond because if I change it, I get internal server errors.


 7:52 pm on Jun 19, 2007 (gmt 0)

Yeah, I typo'd it. It should be:

RewriteRule ^(([^/]+/)*([^./]+))/?$ index.php?x=$1 [L]

As for the first rule, RewriteRule can't examine %{REQUEST_FILENAME} like that. It expects a regular-expressions pattern there. See the Apache docs for more info.


[edited by: jdMorgan at 8:07 pm (utc) on June 19, 2007]


 8:10 pm on Jun 19, 2007 (gmt 0)

I see. I'm not all too familiar with regex, but right now it seems to be doing its job.

With that rewrite, it's near perfect, however I'm trying to mimmick what apache does with real files like if it's a directory it will add a / and if it's not it will takeaway the /.


 4:32 am on Jun 20, 2007 (gmt 0)

Apache does that append-slash function with existing directories (see Apache mod_dir), but since these are virtual files and/or directories and don't exist as 'real' files or directories, there is no basis for taking action at the server level. Only your index.php file can determine whether the content referred to by the virtual URL exists or does not exists, and as such it is the agent that should append or remove slashes -- If doing so changes the meaning of the URL in any way.

I'm not sure how it would, so I'm hiding behind the generalized/theoretical language here... :)

Simply put, only index.php can decide if the value for the "x" parameter is "good" and assign any meaning to it.


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