Forum Moderators: phranque

Message Too Old, No Replies

Simple Mod Rewrite - Has me confused

         

mattmcl1

3:50 pm on May 12, 2006 (gmt 0)

10+ Year Member



Hi - I am trying to use modrewrite so that any URL of the format:

[mydomain.com...] (must end in .html only)

is rewritten to:

[mydomain.com...] (note no .html)

Here is the mod-rewrite I am using

RewriteCond %{REQUEST_URI} ^/([a-zA-Z]+)\.html$
RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=301,L]

The problem that I am having is that $1 is passing the entire anything.html and not just anything

jdMorgan

3:57 pm on May 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll likely be happier with a simpler, internal rewrite:

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

More here: Changing dynamic to static URLs [webmasterworld.com].

Jim

mattmcl1

4:18 pm on May 12, 2006 (gmt 0)

10+ Year Member



Jim,

Thanks a ton! Appreciate it!

On another note - I can't find any reference on using text functions within mod-rewrite (including the link you provided). For example, I want to pass only the second level domain from the {HTTP_HOST} variable. I have already made sure that all domains will start www but I can't find if I can use a function somewhere.

Thanks,
Matt

jdMorgan

6:10 pm on May 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to spend a bit of time studying two things:

  • Regular expressions (a text-pattern-matching 'language' used by mod_rewrite, PHP, PERL, etc.)
  • Mod_rewrite back-references (to re-use matched subpattern contents from old URL in the new URL)

    For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

    Jim

  •