Forum Moderators: phranque

Message Too Old, No Replies

Redirecting example.com/foo

         

Ackergaul

9:22 am on Apr 14, 2010 (gmt 0)

10+ Year Member



Hi all,

I would like to do the following redirection:
example.com/ -> example.com/
example.com/foo.html -> example.com/index.php?site=foo
example.com/foo -> example.com/index.php?site=test&name=foo

Here come my rewrite rules:
RewriteRule (.*).html index.php?site=$1 [L]
RewriteRule (.+) index.php?site=test&name=$1

For some reason the first rewrite rule never applies and it redirects everything to index.php?site=test&name=$1.
Any advice?

Many thanks!

jdMorgan

1:25 pm on Apr 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't use the ambiguous, greedy, and promiscuous ".*" pattern when a more-specific pattern can be used.

Also, be aware that rule-processing in .htaccess is recursive -- mod_rewrite will be re-started after any rule is invoked. Therefore, your second rule will always be invoked -- either directly, or just after the first rule is invoked (and you will see "site=index.php" as the value passed into index.php as a result).

See also the subjects of regular-expressions "anchoring" and "character-escaping". There is a concise regex tutorial cited in our Apache Forum Charter, accessible through the link at the top of this page.

RewriteRule ^(.+)\.html$ index.php?site=$1 [L]
RewriteRule ^(([^/]+/)*[^./]+)$ index.php?site=test&name=$1 [L]

The second Rule's pattern matches "page URL-paths" with no periods in them and no trailing slashes, preceded by any number of subdirectory levels (including zero) which may contain periods. So example.com/foo->index.php?site=test&name=foo and example.com/foo.foo/bar->index.php?site=test&name=foo.foo/bar

You may not need the "allow and include subdirectory levels" or "allow periods in subdirectory paths" features, but I thought I'd might as well include them, as it's always easier to take away than to add...

Jim

Ackergaul

4:50 pm on Apr 14, 2010 (gmt 0)

10+ Year Member



Great stuff Jim.
Thanks for the help.