RewriteCond

Hi, I need help to understand how RewriteCond works
RewriteRule ^([^/]+)/([^/]+) /index1.php?p=$1&NewsID=$2 [NC]
causes the url
mysite/news/NewsID=1
to display as
mysite/news/12 (where 12 is a record number)

How can I provide a condition which tests the value of $1
for example
if $1=news then
RewriteRule ^([^/]+)/([^/]+) /index1.php?p=$1&NewsID=$2 [NC]
elseif
$1=article then
RewriteRule ^([^/]+)/([^/]+) /index1.php?p=$1&ArticleID=$2 [NC]

also,
if there are less than 2 variables passed the rules are ignored?

thanks
joomlaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David S.Commented:
RewriteCond allows you to add conditions based on more than just the path and file name.

It can be used like this:

RewriteCond  %{REQUEST_URI} ^news/ [NC]
RewriteRule ^([^/]+)/([^/]+)? /index1.php?p=$1&NewsID=$2

RewriteCond  %{REQUEST_URI} ^article/ [NC]
RewriteRule ^([^/]+)/([^/]+)? /index1.php?p=$1&ArticleID=$2


However, in this case it seems that you don't really need it. This is a shorter alternative:

RewriteRule ^news(/([^/]+))? /index1.php?p=news&NewsID=$2 [NC]

RewriteRule ^article(/([^/]+))? /index1.php?p=article&ArticleID=$2 [NC]


> if there are less than 2 variables passed the rules are ignored?

That's because the second matching group was not set as optional. You can add a question mark after its right-parenthesis to make it optional.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
joomlaAuthor Commented:
Kravimir
thanks for the wonderful examples.
i successfully tested the 'shorter alternative'.... thank you.

In terms of the second questions
I tried
RewriteRule ^newslist(/([^/]+))? /index1.php?p=news_list[NC]
with the expectation that
mysite/news_list
would give me the desire result.

Can you point out my error
thanks again.
Michael
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.