peter_anderson Posted December 29, 2009 Share Posted December 29, 2009 Hi, I tried doing a search first, but no results that fixed it were found. I'm using Options +FollowSymLinks RewriteEngine on RewriteRule ^([a-z])) /index.php?action=$1 RewriteRule ^([a-z])/([a-z]) /index.php?action=$1&id=$2 To rewrite. My url structure is: /support/kb/___/____ which is: /support/kb/index.php?action=____&id_____ the id variable is numbers or letters, and the action is letters only. The .htaccess is in the /support/kb directory. Can anyone help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/186597-multiple-variables-mod_rewrite/ Share on other sites More sharing options...
cags Posted December 29, 2009 Share Posted December 29, 2009 I always find a good way to learn/practice mod_rewrites is to add the [R] modifier. That way you can see if you page is being directed and where to. I'd guess you're being redirected to a link relative to your site root or the RewriteRule is matching the ^ from the root of the site (I always forget exactly how it works). Give it a go and see what you get in your browser. If it doesn't redirect at all you know it's the start that is wrong, if it redirects to the wrong place you know it's the end that's wrong. Options +FollowSymLinks RewriteEngine on RewriteRule ^([a-z]) /index.php?action=$1 [R] RewriteRule ^([a-z])/([a-z]) /index.php?action=$1&id=$2 [R] Quote Link to comment https://forums.phpfreaks.com/topic/186597-multiple-variables-mod_rewrite/#findComment-985506 Share on other sites More sharing options...
peter_anderson Posted December 29, 2009 Author Share Posted December 29, 2009 I always find a good way to learn/practice mod_rewrites is to add the [R] modifier. That way you can see if you page is being directed and where to. I'd guess you're being redirected to a link relative to your site root or the RewriteRule is matching the ^ from the root of the site (I always forget exactly how it works). Give it a go and see what you get in your browser. If it doesn't redirect at all you know it's the start that is wrong, if it redirects to the wrong place you know it's the end that's wrong. Options +FollowSymLinks RewriteEngine on RewriteRule ^([a-z]) /index.php?action=$1 [R] RewriteRule ^([a-z])/([a-z]) /index.php?action=$1&id=$2 [R] I put the relevant path, and did a redirect, but it's taking me: /support/kb/login -> /support/kb/index.php?action=i with a redirect loop error in Chrome. Options +FollowSymLinks RewriteEngine on RewriteRule ^([a-z]) /support/kb/index.php?action=$1 [R] RewriteRule ^([a-z])/([a-z]) /support/kb/index.php?action=$1&id=$2 [R] Quote Link to comment https://forums.phpfreaks.com/topic/186597-multiple-variables-mod_rewrite/#findComment-985509 Share on other sites More sharing options...
cags Posted December 29, 2009 Share Posted December 29, 2009 Ahh, you should be using the + modifier for 1 or more letters, what does this give you? RewriteEngine on RewriteRule ^([a-z]+)/?$ /index.php?action=$1 [R] RewriteRule ^([a-z]+)/([a-z]+)/?$ /index.php?action=$1&id=$2 [R] Quote Link to comment https://forums.phpfreaks.com/topic/186597-multiple-variables-mod_rewrite/#findComment-985510 Share on other sites More sharing options...
peter_anderson Posted December 29, 2009 Author Share Posted December 29, 2009 Ahh, you should be using the + modifier for 1 or more letters, what does this give you? RewriteEngine on RewriteRule ^([a-z]+)/?$ /index.php?action=$1 [R] RewriteRule ^([a-z]+)/([a-z]+)/?$ /index.php?action=$1&id=$2 [R] Thanks, that takes me to /support/index.php?action=..., which is fine. The only problem, is the second variable (which is an id field) does not redirect. It contains letters, numbers and dashes (-). Any ideas how I would do this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/186597-multiple-variables-mod_rewrite/#findComment-985517 Share on other sites More sharing options...
cags Posted December 29, 2009 Share Posted December 29, 2009 RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /index.php?action=$1&id=$2 [R] Quote Link to comment https://forums.phpfreaks.com/topic/186597-multiple-variables-mod_rewrite/#findComment-985520 Share on other sites More sharing options...
peter_anderson Posted December 29, 2009 Author Share Posted December 29, 2009 RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /index.php?action=$1&id=$2 [R] Thanks so much! It works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/186597-multiple-variables-mod_rewrite/#findComment-985526 Share on other sites More sharing options...
cags Posted December 29, 2009 Share Posted December 29, 2009 Excellent. Obviously you just need to remove the R to stop the address bar from changing. You might actually wish to replace the are with [NC], at the moment your code will only work with lowercase letters. Alternatively you could add in A-Z to the character classes. Another thing you may wish to consider is using. Is... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f If you place this before the RewriteRules it will only perform the action if the specified file/directory doesn't exist. That way if you need a few fixed directories in the folder they will still work rather than redirect. For example you could have an /support/kb/images folder that is used. Quote Link to comment https://forums.phpfreaks.com/topic/186597-multiple-variables-mod_rewrite/#findComment-985527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.