0

I have this .htaccess

RewriteEngine On
RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z]+) index.php?category=$2&language=$1&id=$3
RewriteCond %{HTTP_USER_AGENT} libwww [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*)=http [NC]
RewriteRule ^(.*)$ – [F,L]

Options +FollowSymLinks 
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301] 

What I am trying to achive is to pass all variables but it seems that I cannot do that. I am quite new in .htaccess stuff so the code might be nonsense. However, I have managed to pass the category and language, but can pass the id.

Thanks

1
  • 1
    You have only two grouped sub-patterns – so you can’t expect to get three values out of them. Commented Sep 11, 2013 at 7:41

4 Answers 4

2

It would help to study up on Perl regular expressions a tad, which is the RegEx style .htaccess uses. Links to: Apache docs, RegEx info.

Namely, for your first RewriteRule, you only have two subgroups (the parenthetical parts) so $3 is always going to yield empty -- whereas $1 and $2 will match the respective left-to-right ordered groups. Here's a sample of how to match 3 subgroups given your RewriteRule:

RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z]+)/([0-9]+) index.php?category=$2&language=$1&id=$3

Notice the third parenthetical part that matches one or more digits -- that matched part will be replaced into the $3 backreference effectively subbing a numerical id in where you want it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that works fine, but the problem comes from the fact that I have ID only for the news category. the rule now expect to have id or something for all pages otherwise does not open them. Is there a way to make this rule applicable only if category is news?
Sure, you should make two rules to handle this. The first RewriteRule should be ^.../news/... ... to match news + id param first. Then, the second rule should be ^.../... to match just category + language without an id.
1

You can have 2 separate rule one without id and one with id:

RewriteRule ^([\da-z]+)/([\da-z]+)/(\d+)/?$ index.php?category=$2&language=$1&id=$3 [L,QSA,NC]
RewriteRule ^([\da-z]+)/([\da-z]+)/?$ index.php?category=$2&language=$1 [L,QSA,NC]

Comments

0

If your ID is a digit then use following rule:

RewriteRule ^([0-9A-Za-z]+)/([0-9A-Za-z]+)/(\d+) index.php?category=$2&language=$1&id=$3

Comments

0

Try this

RewriteRule ^index/([^/]*)/([^/]*)/([0-9]+)$ index.php?language=$1&category=$2&id=$3 [L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.