views:

79

answers:

1

Hi,

I'm working on a Rewriterule in order to have URLs like these: http://www.myhost.com/var1/var2/

RewriteRule ^(.*)\/(.*)\/$  index.php?var1=$1&var2=$2 [L]

What I would like to add is that when someone types myhost.com/var1/var2 (without the end slash), it still goes to the same page.

Is there a better way to do it than this?

RewriteRule ^(.*)\/(.*)\/$  index.php?var1=$1&var2=$2 [L]
RewriteRule ^(.*)\/(.*)$  index.php?var1=$1&var2=$2 [L]
A: 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)\/([^\/]*)\/?$  index.php?band=$1&song=$2 [L]

Note the ending ?

Seb
This is working but it broke the access to my CSS files (which are in myhost.com/folder1/folder2/). Any idea why?
Maxime
Yeah, because this is matching any 2 folder-deep access and passing it to index.php instead. You should add `RewriteCond %{REQUEST_FILENAME} !-f` and `RewriteCond %{REQUEST_FILENAME} !-d` in two different lines right before the RewriteRule, so existing files won't be affected by this rule.
Seb
Maxime
I'm not sure what you mean... with those rules, if the file exists it'll be served as-is. If not, the URL will be rewritten and handed over to index.php. Isn't that what you want?
Seb
Actually yes. But there's still a problem in that if I type /var1/var2/ (with slash) it'll be interpreted in my script as if I typed /var1var2/ (like they were concatenated). But it must be the question mark's fault.
Maxime
Ahhh I see... it's because you have `(.*)` and it's matching the inner slash. Replace those occurrences for this: `([^\/]*)`.
Seb
Perfect, thanks! Now do you think it's possible to redirect URLs like myhost.com/var1/ to another PHP file? I tried to place this before the RewriteRule with var1 and var2: [code]RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\/]*)\/?$ page2.php?var1=$1 [L][/code]But for some reason this page2.php takes ages to load...
Maxime
Not sure what may be happening. One thing to check would be to remove those `*` and replace them with `+`, so you only redirect if there's actually *something* in between the slashes. But regardless, consider having just one rewrite rule and handling all delegation in a master PHP file. Like rewriting `^(.*)+\/?$` to `index.php?q=$1`, then handling inside that index.php what that string has. This will make things easier for you to maintain in the future, and also gives you greater flexibility.
Seb
That's an idea that I may well try. It shouldn't be too complicated to see how the string is built and then to redirect it where I want. I'll try it a bit later today though. Thanks for your fast answers! There's just one last thing I would like to ask you: how do you write code in comments here?
Maxime
hehe np... to write code, enclose it in backticks `, as when you write inline code in the question. You can also write italics and bold, enclosing with one or two asterisks as well. Oh, and it seems you're new to SO: remember to mark an answer as correct if it fulfilled your question. Good luck!
Seb
`Thanks`, so far I've only used the four spaces thing.
Maxime