Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewriting something without an extension

         

SNOWmanx

2:36 am on Apr 15, 2006 (gmt 0)

10+ Year Member



I have a page that's like...

www.domain.com/services.php

but I want it to be

www.domain.com/services

When I try
RewriteRule ^(.*)$ $1.php
I just get a 500 error.

jdMorgan

2:59 am on Apr 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's probably because there is nothing in the ruleset to prevent recursion: test -> test.php -> test.php.php -> test.php.php.php, etc. Examine your server error log to find out.

Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !\.php$
RewriteRule ^(.+)$ $1.php

should fix it, if that is the problem.

You may also want to add additional exclusions so that image files, CSS, and special files like robots.txt are not rewritten, or use the more general solution:


Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 !(\.¦/$)
RewriteRule ^(.+)$ $1.php

Here, the URL-path is not rewritten if it contains a "." or ends with a "/" or is blank. Replace the broken vertical pipe "¦" character with a solid pipe before use; Posting on this forum modifies the pipe character.

Jim

SNOWmanx

3:24 am on Apr 15, 2006 (gmt 0)

10+ Year Member



If you don't mind my asking, could you explain exactly what that means in English? I've been doing a little bit of... cut and paste, then learn type of thing.

Also, that script ends everything (and I mean everything) with a .php extension. So then an www.domain.com/image.gif becomes www.domain.com/image.gif.php

Tastatura

4:58 am on Apr 15, 2006 (gmt 0)

10+ Year Member



I can sympathize that Mod_rewrite and regular expressions can be a bit daunting (and I am not good at it, at least at the moment)

Also, that script ends everything (and I mean everything) with a .php extension. So then an www.domain.com/image.gif becomes www.domain.com/image.gif.php

but jdMorgan did say

You may also want to add additional exclusions so that image files, CSS, and special files like robots.txt are not rewritten

for example, if you add this to your rewrite condition (replace ¦ with solid line ; this forum automatically changes them)

(jpe?g¦gif¦png¦js¦css)

it would exclude jpg, jpeg, gif and png image files, as well as java script and css files (or more precisely files that end in those extensions)

I would higly recommend that you read this forum library [webmasterworld.com] as well as Apache url rewrite guide [httpd.apache.org]

HTH

SNOWmanx

6:38 am on Apr 15, 2006 (gmt 0)

10+ Year Member



Yes, it was the broken line that was bothering me. It's fixed now.