Jump to content

Multiple Variables mod_rewrite


peter_anderson

Recommended Posts

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.