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

Apache Web Server Forum

Having trouble dropping file extension

 5:01 pm on Apr 26, 2007 (gmt 0)

I want to drop the file extension so that the URL reads:

This is my present code which works with .html

<a href="http://local.example.com/info/',$id,'.html" title="Learn more about ',$cli["name"],'">',$cli["name"],'</a>

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^info/([^/]+).html$ info.php?id=$1 [L]

The htaccess file and info.php are in the same folder [local.example.com...]

I've tried dropping the .html in the link and in the rewrite rule and get error page not found.

I've tried a ton of examples and get the same error. So I'm wondering if I need to set something in the httpd to allow no file extensions?

Also, I have a virtual directory set up for this site on my local machine which is where I am experiencing the problem if that makes any difference.




 5:19 pm on Apr 26, 2007 (gmt 0)

The regex pattern is incorrect for the application you describe, and should be more like:

RewriteRule ^info/([^.]+)\.html$ info.php?id=$1 [L]

You can't just "try examples" because no two sites and their URL-sets are ever going to be exactly the same, the possible regex combinations are infinite, and you'll likely never find one that is "just right." I suspect you'll be much happier if you look up what all those "hat, backslash, and dollar" characters actually mean -- one character at a time if necessary. That is the path to both happiness and success... :)

For more information, see the concise regular-expressions tutorial cited in our forum charter [webmasterworld.com] or one of the many others available on-line.

I have found that writing regular expressions patterns is actually far easier than reading them. But unfortunately, you have to read them to learn and understand them. So there is a distinct feeling of "struggling to the top of the mountain but then gliding back down" when it finally clicks...



 5:40 pm on Apr 26, 2007 (gmt 0)

This worked for me for removing the .html
RewriteRule ^info/([^.]+)$ info.php?id=$1 [L]

I know I was not doing myself any favors by trying to use others code. 6 months ago I was getting pretty good at doing the regexs but it's hard to remember all that stuff when you don't work with it on a regular basis.

What will help me in remembering is to make some notes in my file of exactly why the regex was written the way that it was. Reading it each time I'm in that file will help embed it into my brain. So to make atonements for my lazyness today, I promise to look them up and make those notes!

Thanks so much JD.



 6:00 pm on Apr 26, 2007 (gmt 0)

This worked for me for removing the .html
RewriteRule ^info/([^.]+)$ info.php?id=$1 [L]

I don't see how that could work.

The pattern says, "Match any URL-path that starts with "info/", followed by one or more characters not equal to a literal period (and store this part into $1), followed immediately by the end of the URL-path. Thus, there is no provision in this pattern for the existence of a period or the "html" that follows it.

Unless you have another rule or Redirect directive that is modifying these .html URL-paths, the code I posted should work, and the the quoted code should not...



 7:07 pm on Apr 26, 2007 (gmt 0)

This rule

RewriteRule ^info/([^.]+)\.html$ info.php?id=$1 [L]

along with this link
<a href="http://local.example.com/folder/info/',$id,'" title="Learn more about ',$cli["name"],'">',$cli["name"],'</a>

does rewrite the url correctly but does not pass the variable correctly. When echo'ed the variable is info/7 instead of just 7. Adding the .html back into the link passes the variable correctly, but does not give me the wanted outcome of removing the .html

Removing the .html from the link and \.html from the rewrite rule gives me exaclty what I want.

This is my main .htaccess file in www.example.com/

RewriteEngine on
RewriteRule ^folder1(.*)$ http://.example.com$1 [R=301,L]
RewriteRule ^folder2(.*) http://www.example.com/folder2$1 [R=301,L]
#We discussed the following rule before and though, not exactly what you recommend, this was the only thing that worked for me.
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
#I know the following rules need work but I may be removing this section from the site and will deal with them when I make that decision.
RewriteRule ^folder3/([^/]+).html$ file3.php?$1 [L]
RewriteRule ^folder4/([^/]+).html$ file3.php?$1 [L]
RewriteRule ^folder5/([^/]+).html$ file3.php?$1 [L]
RewriteRule ^folder6/([^/]+).html$ file3.php?$1 [L]


 7:41 pm on Apr 26, 2007 (gmt 0)

RewriteRule ^info/([^.]+)\.html$ info.php?id=$1 [L]
along with this link
does rewrite the url correctly but does not pass the variable correctly. When echo'ed the variable is info/7 instead of just 7.

If that is the case then either you have a broken installation of mod_rewrite or the regex library it's using (provided by your operating system), or you've got some other applicable rule executing first (for example in a higher-level .htaccess file, in httpd.conf, or in conf.d), or you've got AcceptPathInfo turned on.

As described in the mod_rewrite documentation, the $1 through $9 variables take the value of ("back-reference") the first through ninth parenthesized sub-expressions in the RewriteRule pattern. Since "info/" falls outside the parentheses in the rule, it's not possible that "info/7" is being passed into the variable you are actually echoing -- or it's not possible with a normal, working installation, anyway.

If you are using AcceptPathInfo, then you shouldn't need a RewriteRule to extract the parameter at all -- you could do that in the script itself.

Sorry to sound pedantic; On the one hand, one could say, "If it works, it works," but on the other, you may end up with some unexpected and unexplained behaviour if the cause of this anomaly is not identified.



 8:46 pm on Apr 26, 2007 (gmt 0)

Anomly is the story of my life, weird stuff is always happening to me.

I'm going to leave the rewrite rule as it is for now while I look into what else could be wrong.

I often wonder about my setup on this laptop as I had an extremly difficult time getting Apache, PHP and MySQL working this time around.

I'll start new threads as I work through this.

What a great learning experience this will be for me.
Thanks again,

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