Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite on root level (dynamic URL)

         

vygbuyhnmj

1:00 am on Mar 10, 2007 (gmt 0)

10+ Year Member



#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?mode=$1 [L]

When www.example.com/hello is called, it should actually grab this instead:
www.example.com/index.php?mode=$1

I have been able to get the code to work when there is a folder above the root of the site (www.example.com/folder), but I can't seem to get it to work when it is at the root of the site (www.example.com).

Any ideas on this one?

jdMorgan

5:15 pm on Mar 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem may simply be that there is nothing to prevent this code from rewriting a request for "index.php?mode=$1" to itself repeatedly; Use a RewriteCond to prevent that. Also, if this were my code, I'd write it like this to allow for any number of subdirectories (and the optional trailing slash):

Options +FollowSymLinks [b]-MultiViews[/b]
RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^(([^/]+/)*[^/]+)/?$ [b]/i[/b]ndex.php?mode=$1 [L]

If you really want to restrict the directory-paths and or the "filename" to only [a-zA-Z0-9-] characters, then you can substitute either or both subpatterns above with [a-z0-9-] and use the [NC] flag to make the comparison case-insensitive:

RewriteRule ^(([a-z0-9-]+/)*[a-z0-9-]+)/?$ /index.php?mode=$1 [[b]NC[/b],L]

Jim

vygbuyhnmj

4:01 am on Mar 11, 2007 (gmt 0)

10+ Year Member



Using that code, prevents my CSS files from loading...

<link href="/templates/black/global.css" rel="stylesheet" type="text/css" />
<link href="/templates/black/main.css" rel="stylesheet" type="text/css" />

I think it is trying to rewrite EVERYTHING after the root of the site. Basically, when I open example.com/templates/black/main.css it rewrites it to index.php?something=/templates/black/main.css

Any suggestions here?

Thanks a lot for your help, I wish I knew a little more about .htaccess and mod_rewrite, heh :)

jdMorgan

4:20 am on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I think it is trying to rewrite EVERYTHING after the root of the site.

Yes, it would. Now what exactly do you want to rewrite, and what exactly do you not want to rewrite? Defining things both ways greatly reduces the possible coding combinations, and also prevent problems in interpretation that lead to unexpected results such as this.

It is defining the desired function, not coding it, that is the hard part... :)

Jim

vygbuyhnmj

4:28 am on Mar 11, 2007 (gmt 0)

10+ Year Member



The index.php page has numerous variables, such as: index.php?mode=search&sec=bands&genre=Rock&order=createDate_DESC

These should be rewritten, however, I just want to start simple and rewrite the mode variable first.

index.php?mode=login should bring up the login page.

However, /images/image.gif should NOT be rewritten.

Everything worked fine when I tried to do a rewrite above the home directory, however I ran into problems when trying to do it below the home directory.

jdMorgan

3:56 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, well as a stranger, I have no idea how to describe the URLs you do and don't want to rewrite. As such, there's no way I can do anything but guess at the more likely of several thousand possibilities. Since mod_rewrite requires a precise description of the URLs it is to rewrite, there's no way I can be sure of my answers. URLs need to be described in terms of their characteristics, as directly-testable by mod_rewrite... For example, "Rewrite only top-level-directory URLs which contain no filetype extension" would be a useful description, and could be coded thus:

Options +FollowSymLinks -MultiViews
RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^([^/.]+)/?$ /index.php?mode=$1 [L]

So that might do what you want or it might not; Since I'm just guessing, my chances of being of much help are rather low.

Jim

vygbuyhnmj

4:52 pm on Mar 11, 2007 (gmt 0)

10+ Year Member



I'll figure out what all of the variables are that need to finally be rewritten and post back. It's weird that there are no issues when rewriting to a folder after root, but when there is no folder, many issues come up.

As usual, thanks for the help :)